php_gearman_job.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * Gearman PHP Extension
  3. *
  4. * Copyright (C) 2008 James M. Luedke <contact@jamesluedke.com>,
  5. * Eric Day <eday@oddments.org>
  6. * All rights reserved.
  7. *
  8. * Use and distribution licensed under the PHP license. See
  9. * the LICENSE file in this directory for full text.
  10. */
  11. #include "php_gearman_job.h"
  12. inline gearman_job_obj *gearman_job_fetch_object(zend_object *obj) {
  13. return (gearman_job_obj *)((char*)(obj) - XtOffsetOf(gearman_job_obj, std));
  14. }
  15. /* {{{ proto object GearmanJob::__destruct()
  16. cleans up GearmanJob object */
  17. PHP_METHOD(GearmanJob, __destruct) {
  18. gearman_job_obj *intern = Z_GEARMAN_JOB_P(getThis());
  19. if (!intern) {
  20. return;
  21. }
  22. if (intern->flags & GEARMAN_JOB_OBJ_CREATED) {
  23. gearman_job_free(intern->job);
  24. intern->flags &= ~GEARMAN_JOB_OBJ_CREATED;
  25. }
  26. }
  27. /* }}} */
  28. zend_object *gearman_job_obj_new(zend_class_entry *ce) {
  29. gearman_job_obj *intern = ecalloc(1,
  30. sizeof(gearman_job_obj) +
  31. zend_object_properties_size(ce));
  32. zend_object_std_init(&(intern->std), ce);
  33. object_properties_init(&intern->std, ce);
  34. intern->std.handlers = &gearman_job_obj_handlers;
  35. return &intern->std;
  36. }
  37. /* {{{ proto int gearman_job_return_code()
  38. get last gearman_return_t */
  39. PHP_FUNCTION(gearman_job_return_code) {
  40. gearman_job_obj *obj;
  41. zval *zobj;
  42. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_job_ce) == FAILURE) {
  43. RETURN_NULL();
  44. }
  45. obj = Z_GEARMAN_JOB_P(zobj);
  46. RETURN_LONG(obj->ret);
  47. }
  48. /* }}} */
  49. /* {{{ proto bool gearman_job_set_return(int gearman_return_t)
  50. This function will set a return value of a job */
  51. PHP_FUNCTION(gearman_job_set_return) {
  52. zval *zobj;
  53. gearman_job_obj *obj;
  54. gearman_return_t ret;
  55. zend_long ret_val;
  56. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Ol", &zobj, gearman_job_ce, &ret_val) == FAILURE) {
  57. RETURN_NULL();
  58. }
  59. obj = Z_GEARMAN_JOB_P(zobj);
  60. ret = ret_val;
  61. /* make sure its a valid gearman_return_t */
  62. if (ret < GEARMAN_SUCCESS || ret > GEARMAN_MAX_RETURN) {
  63. php_error_docref(NULL, E_WARNING,
  64. "Invalid gearman_return_t: %d", ret);
  65. RETURN_FALSE;
  66. }
  67. obj->ret = ret;
  68. RETURN_TRUE;
  69. }
  70. /* }}} */
  71. /* {{{ proto bool gearman_job_send_data(object job, string data)
  72. Send data for a running job. */
  73. PHP_FUNCTION(gearman_job_send_data) {
  74. zval *zobj;
  75. gearman_job_obj *obj;
  76. char *data;
  77. size_t data_len;
  78. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &zobj, gearman_job_ce,
  79. &data, &data_len) == FAILURE) {
  80. RETURN_NULL();
  81. }
  82. obj = Z_GEARMAN_JOB_P(zobj);
  83. /* make sure worker initialized a job */
  84. if (obj->job == NULL) {
  85. RETURN_FALSE;
  86. }
  87. obj->ret = gearman_job_send_data(obj->job, data, data_len);
  88. if (obj->ret != GEARMAN_SUCCESS && obj->ret != GEARMAN_IO_WAIT) {
  89. php_error_docref(NULL, E_WARNING, "%s",
  90. gearman_job_error(obj->job));
  91. RETURN_FALSE;
  92. }
  93. RETURN_TRUE;
  94. }
  95. /* }}} */
  96. /* {{{ proto bool gearman_job_send_warning(object job, string warning)
  97. Send warning for a running job. */
  98. PHP_FUNCTION(gearman_job_send_warning) {
  99. zval *zobj;
  100. gearman_job_obj *obj;
  101. char *warning = NULL;
  102. size_t warning_len = 0;
  103. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &zobj, gearman_job_ce,
  104. &warning, &warning_len) == FAILURE) {
  105. RETURN_FALSE;
  106. }
  107. obj = Z_GEARMAN_JOB_P(zobj);
  108. /* make sure worker initialized a job */
  109. if (obj->job == NULL) {
  110. RETURN_FALSE;
  111. }
  112. obj->ret = gearman_job_send_warning(obj->job, (void *) warning,
  113. (size_t) warning_len);
  114. if (obj->ret != GEARMAN_SUCCESS && obj->ret != GEARMAN_IO_WAIT) {
  115. php_error_docref(NULL, E_WARNING, "%s",
  116. gearman_job_error(obj->job));
  117. RETURN_FALSE;
  118. }
  119. RETURN_TRUE;
  120. }
  121. /* }}} */
  122. /* {{{ proto bool gearman_job_send_status(object job, int numerator, int denominator)
  123. Send status information for a running job. */
  124. PHP_FUNCTION(gearman_job_send_status) {
  125. zval *zobj;
  126. gearman_job_obj *obj;
  127. zend_long numerator, denominator;
  128. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Oll", &zobj, gearman_job_ce,
  129. &numerator, &denominator) == FAILURE) {
  130. RETURN_FALSE;
  131. }
  132. obj = Z_GEARMAN_JOB_P(zobj);
  133. obj->ret = gearman_job_send_status(obj->job, (uint32_t)numerator,
  134. (uint32_t)denominator);
  135. if (obj->ret != GEARMAN_SUCCESS && obj->ret != GEARMAN_IO_WAIT) {
  136. php_error_docref(NULL, E_WARNING, "%s",
  137. gearman_job_error(obj->job));
  138. RETURN_FALSE;
  139. }
  140. RETURN_TRUE;
  141. }
  142. /* }}} */
  143. /* {{{ proto bool gearman_job_send_complete(object job, string result)
  144. Send result and complete status for a job. */
  145. PHP_FUNCTION(gearman_job_send_complete) {
  146. zval *zobj;
  147. gearman_job_obj *obj;
  148. char *result;
  149. size_t result_len;
  150. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &zobj, gearman_job_ce,
  151. &result, &result_len) == FAILURE) {
  152. RETURN_FALSE;
  153. }
  154. obj = Z_GEARMAN_JOB_P(zobj);
  155. obj->ret = gearman_job_send_complete(obj->job, result, result_len);
  156. if (obj->ret != GEARMAN_SUCCESS && obj->ret != GEARMAN_IO_WAIT) {
  157. php_error_docref(NULL, E_WARNING, "%s",
  158. gearman_job_error(obj->job));
  159. RETURN_FALSE;
  160. }
  161. RETURN_TRUE;
  162. }
  163. /* }}} */
  164. /* {{{ proto bool gearman_job_send_exception(object job, string exception)
  165. Send exception for a running job. */
  166. PHP_FUNCTION(gearman_job_send_exception) {
  167. zval *zobj;
  168. gearman_job_obj *obj;
  169. char *exception;
  170. size_t exception_len;
  171. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &zobj, gearman_job_ce,
  172. &exception, &exception_len) == FAILURE) {
  173. RETURN_FALSE;
  174. }
  175. obj = Z_GEARMAN_JOB_P(zobj);
  176. obj->ret= gearman_job_send_exception(obj->job, exception, exception_len);
  177. if (obj->ret != GEARMAN_SUCCESS && obj->ret != GEARMAN_IO_WAIT) {
  178. php_error_docref(NULL, E_WARNING, "%s",
  179. gearman_job_error(obj->job));
  180. RETURN_FALSE;
  181. }
  182. RETURN_TRUE;
  183. }
  184. /* }}} */
  185. /* {{{ proto bool gearman_job_send_fail(object job)
  186. Send fail status for a job. */
  187. PHP_FUNCTION(gearman_job_send_fail) {
  188. zval *zobj;
  189. gearman_job_obj *obj;
  190. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_job_ce) == FAILURE) {
  191. RETURN_FALSE;
  192. }
  193. obj = Z_GEARMAN_JOB_P(zobj);
  194. obj->ret = gearman_job_send_fail(obj->job);
  195. if (obj->ret != GEARMAN_SUCCESS && obj->ret != GEARMAN_IO_WAIT) {
  196. php_error_docref(NULL, E_WARNING, "%s",
  197. gearman_job_error(obj->job));
  198. RETURN_FALSE;
  199. }
  200. RETURN_TRUE;
  201. }
  202. /* }}} */
  203. /* {{{ proto false|string gearman_job_handle(object job)
  204. Return job handle. */
  205. PHP_FUNCTION(gearman_job_handle) {
  206. zval *zobj;
  207. gearman_job_obj *obj;
  208. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_job_ce) == FAILURE) {
  209. RETURN_NULL();
  210. }
  211. obj = Z_GEARMAN_JOB_P(zobj);
  212. /* make sure worker initialized a job */
  213. if (obj->job == NULL) {
  214. RETURN_FALSE;
  215. }
  216. RETURN_STRING((char *)gearman_job_handle(obj->job));
  217. }
  218. /* }}} */
  219. /* {{{ proto false|string gearman_job_function_name(object job)
  220. Return the function name associated with a job. */
  221. PHP_FUNCTION(gearman_job_function_name) {
  222. zval *zobj;
  223. gearman_job_obj *obj;
  224. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_job_ce) == FAILURE) {
  225. RETURN_NULL();
  226. }
  227. obj = Z_GEARMAN_JOB_P(zobj);
  228. /* make sure worker initialized a job */
  229. if (obj->job == NULL) {
  230. RETURN_FALSE;
  231. }
  232. RETURN_STRING((char *)gearman_job_function_name(obj->job));
  233. }
  234. /* }}} */
  235. /* {{{ proto false|string gearman_job_unique(object job)
  236. Get the unique ID associated with a job. */
  237. PHP_FUNCTION(gearman_job_unique) {
  238. zval *zobj;
  239. gearman_job_obj *obj;
  240. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_job_ce) == FAILURE) {
  241. RETURN_NULL();
  242. }
  243. obj = Z_GEARMAN_JOB_P(zobj);
  244. /* make sure worker initialized a job */
  245. if (obj->job == NULL) {
  246. RETURN_FALSE;
  247. }
  248. RETURN_STRING((char *)gearman_job_unique(obj->job));
  249. }
  250. /* }}} */
  251. /* {{{ proto string gearman_job_workload(object job)
  252. Returns the workload for a job. */
  253. PHP_FUNCTION(gearman_job_workload) {
  254. zval *zobj;
  255. gearman_job_obj *obj;
  256. const uint8_t *workload;
  257. size_t workload_len;
  258. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_job_ce) == FAILURE) {
  259. RETURN_NULL();
  260. }
  261. obj = Z_GEARMAN_JOB_P(zobj);
  262. workload = gearman_job_workload(obj->job);
  263. workload_len = gearman_job_workload_size(obj->job);
  264. RETURN_STRINGL((char *)workload, (long) workload_len);
  265. }
  266. /* }}} */
  267. /* {{{ proto int gearman_job_workload_size(object job)
  268. Returns size of the workload for a job. */
  269. PHP_FUNCTION(gearman_job_workload_size) {
  270. zval *zobj;
  271. gearman_job_obj *obj;
  272. size_t workload_len;
  273. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_job_ce) == FAILURE) {
  274. RETURN_NULL();
  275. }
  276. obj = Z_GEARMAN_JOB_P(zobj);
  277. workload_len = gearman_job_workload_size(obj->job);
  278. RETURN_LONG((long) workload_len);
  279. }
  280. /* }}} */