php_gearman_worker.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  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_worker.h"
  12. gearman_worker_obj *gearman_worker_fetch_object(zend_object *obj) {
  13. return (gearman_worker_obj *)((char*)(obj) - XtOffsetOf(gearman_worker_obj, std));
  14. }
  15. /* {{{ proto object gearman_worker_ctor()
  16. Initialize a worker object. */
  17. static void gearman_worker_ctor(INTERNAL_FUNCTION_PARAMETERS) {
  18. gearman_worker_obj *worker;
  19. if (zend_parse_parameters_none() == FAILURE) {
  20. return;
  21. }
  22. worker = Z_GEARMAN_WORKER_P(return_value);
  23. if (gearman_worker_create(&(worker->worker)) == NULL) {
  24. zval_dtor(return_value);
  25. GEARMAN_EXCEPTION("Memory allocation failure", 0);
  26. }
  27. worker->flags |= GEARMAN_WORKER_OBJ_CREATED;
  28. gearman_worker_set_workload_malloc_fn(&(worker->worker), _php_malloc, NULL);
  29. gearman_worker_set_workload_free_fn(&(worker->worker), _php_free, NULL);
  30. }
  31. /* }}} */
  32. /* {{{ proto object gearman_worker_create()
  33. Returns a worker object */
  34. PHP_FUNCTION(gearman_worker_create) {
  35. if (object_init_ex(return_value, gearman_worker_ce) != SUCCESS) {
  36. php_error_docref(NULL, E_WARNING,
  37. "GearmanWorker Object creation failure.");
  38. RETURN_FALSE;
  39. }
  40. gearman_worker_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU);
  41. }
  42. /* }}} */
  43. /* {{{ proto object GearmanWorker::__construct()
  44. Returns a worker object */
  45. PHP_METHOD(GearmanWorker, __construct) {
  46. return_value = getThis();
  47. gearman_worker_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU);
  48. }
  49. /* }}} */
  50. /* {{{ proto object GearmanWorker::__destruct()
  51. Destroys a worker object */
  52. PHP_METHOD(GearmanWorker, __destruct) {
  53. gearman_worker_obj *intern = Z_GEARMAN_WORKER_P(getThis());
  54. if (!intern) {
  55. return;
  56. }
  57. if (intern->flags & GEARMAN_WORKER_OBJ_CREATED) {
  58. gearman_worker_free(&(intern->worker));
  59. intern->flags &= ~GEARMAN_WORKER_OBJ_CREATED;
  60. }
  61. zval_dtor(&intern->cb_list);
  62. }
  63. /* }}} */
  64. static inline void cb_list_dtor(zval *zv) {
  65. gearman_worker_cb_obj *worker_cb = Z_PTR_P(zv);
  66. zval_dtor(&worker_cb->zname);
  67. zval_dtor(&worker_cb->zdata);
  68. zval_dtor(&worker_cb->zcall);
  69. efree(worker_cb);
  70. }
  71. zend_object *gearman_worker_obj_new(zend_class_entry *ce) {
  72. gearman_worker_obj *intern = ecalloc(1,
  73. sizeof(gearman_worker_obj) +
  74. zend_object_properties_size(ce));
  75. zend_object_std_init(&(intern->std), ce);
  76. object_properties_init(&intern->std, ce);
  77. array_init(&intern->cb_list);
  78. zend_hash_init(Z_ARRVAL(intern->cb_list), 0, NULL, cb_list_dtor, 0);
  79. intern->std.handlers = &gearman_worker_obj_handlers;
  80. return &intern->std;
  81. }
  82. /* {{{ proto int gearman_worker_return_code()
  83. get last gearman_return_t */
  84. PHP_FUNCTION(gearman_worker_return_code) {
  85. gearman_worker_obj *obj;
  86. zval *zobj;
  87. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_worker_ce) == FAILURE) {
  88. RETURN_NULL();
  89. }
  90. obj = Z_GEARMAN_WORKER_P(zobj);
  91. RETURN_LONG(obj->ret);
  92. }
  93. /* }}} */
  94. /* {{{ proto string|false gearman_worker_error(object worker)
  95. Return an error string for the last error encountered. */
  96. PHP_FUNCTION(gearman_worker_error) {
  97. char *error;
  98. zval *zobj;
  99. gearman_worker_obj *obj;
  100. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_worker_ce) == FAILURE) {
  101. RETURN_FALSE;
  102. }
  103. obj = Z_GEARMAN_WORKER_P(zobj);
  104. error = (char *)gearman_worker_error(&(obj->worker));
  105. if (error) {
  106. RETURN_STRING(error);
  107. }
  108. RETURN_FALSE;
  109. }
  110. /* }}} */
  111. /* {{{ proto int gearman_worker_errno(object worker)
  112. Value of errno in the case of a GEARMAN_ERRNO return value. */
  113. PHP_FUNCTION(gearman_worker_errno) {
  114. zval *zobj;
  115. gearman_worker_obj *obj;
  116. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_worker_ce) == FAILURE) {
  117. RETURN_FALSE;
  118. }
  119. obj = Z_GEARMAN_WORKER_P(zobj);
  120. RETURN_LONG(gearman_worker_errno(&(obj->worker)));
  121. }
  122. /* }}} */
  123. /* {{{ proto int gearman_worker_options(object worker)
  124. Get options for a worker structure. */
  125. PHP_FUNCTION(gearman_worker_options) {
  126. zval *zobj;
  127. gearman_worker_obj *obj;
  128. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_worker_ce) == FAILURE) {
  129. RETURN_NULL();
  130. }
  131. obj = Z_GEARMAN_WORKER_P(zobj);
  132. RETURN_LONG(gearman_worker_options(&(obj->worker)));
  133. }
  134. /* }}} */
  135. /* {{{ proto true gearman_worker_set_options(object worker, constant option)
  136. Set options for a worker structure. */
  137. PHP_FUNCTION(gearman_worker_set_options) {
  138. zval *zobj;
  139. gearman_worker_obj *obj;
  140. zend_long options;
  141. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Ol", &zobj, gearman_worker_ce, &options) == FAILURE) {
  142. RETURN_NULL();
  143. }
  144. obj = Z_GEARMAN_WORKER_P(zobj);
  145. gearman_worker_set_options(&(obj->worker), options);
  146. RETURN_TRUE;
  147. }
  148. /* }}} */
  149. /* {{{ proto true gearman_worker_add_options(object worker, constant option)
  150. Set options for a worker structure. */
  151. PHP_FUNCTION(gearman_worker_add_options) {
  152. zval *zobj;
  153. gearman_worker_obj *obj;
  154. zend_long options;
  155. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Ol", &zobj, gearman_worker_ce, &options) == FAILURE) {
  156. RETURN_NULL();
  157. }
  158. obj = Z_GEARMAN_WORKER_P(zobj);
  159. gearman_worker_add_options(&(obj->worker), options);
  160. RETURN_TRUE;
  161. }
  162. /* }}} */
  163. /* {{{ proto true gearman_worker_remove_options(object worker, constant option)
  164. Set options for a worker structure. */
  165. PHP_FUNCTION(gearman_worker_remove_options) {
  166. zval *zobj;
  167. gearman_worker_obj *obj;
  168. zend_long options;
  169. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Ol", &zobj, gearman_worker_ce, &options) == FAILURE) {
  170. RETURN_NULL();
  171. }
  172. obj = Z_GEARMAN_WORKER_P(zobj);
  173. gearman_worker_remove_options(&(obj->worker), options);
  174. RETURN_TRUE;
  175. }
  176. /* }}} */
  177. /* {{{ proto int gearman_worker_timeout(object worker)
  178. Get timeout for a worker structure. */
  179. PHP_FUNCTION(gearman_worker_timeout) {
  180. zval *zobj;
  181. gearman_worker_obj *obj;
  182. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_worker_ce) == FAILURE) {
  183. RETURN_NULL();
  184. }
  185. obj = Z_GEARMAN_WORKER_P(zobj);
  186. RETURN_LONG(gearman_worker_timeout(&(obj->worker)));
  187. }
  188. /* }}} */
  189. /* {{{ proto true gearman_worker_set_timeout(object worker, constant timeout)
  190. Set timeout for a worker structure. */
  191. PHP_FUNCTION(gearman_worker_set_timeout) {
  192. zval *zobj;
  193. gearman_worker_obj *obj;
  194. zend_long timeout;
  195. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Ol", &zobj, gearman_worker_ce, &timeout) == FAILURE) {
  196. RETURN_FALSE;
  197. }
  198. obj = Z_GEARMAN_WORKER_P(zobj);
  199. gearman_worker_set_timeout(&(obj->worker), timeout);
  200. RETURN_TRUE;
  201. }
  202. /* }}} */
  203. /* {{{ proto bool gearman_worker_set_id(object worker, string id)
  204. Set id for a worker structure. */
  205. PHP_FUNCTION(gearman_worker_set_id) {
  206. zval *zobj;
  207. gearman_worker_obj *obj;
  208. char *id;
  209. size_t id_len;
  210. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &zobj, gearman_worker_ce,
  211. &id, &id_len) == FAILURE) {
  212. RETURN_FALSE;
  213. }
  214. obj = Z_GEARMAN_WORKER_P(zobj);
  215. if(gearman_failed(gearman_worker_set_identifier(&(obj->worker), id, id_len))) {
  216. RETURN_FALSE;
  217. }
  218. RETURN_TRUE;
  219. }
  220. /* }}} */
  221. /* {{{ proto bool gearman_worker_add_server(object worker [, string host [, int port [, bool setupExceptionHandler = true]]])
  222. Add a job server to a worker. This goes into a list of servers than can be used to run tasks. No socket I/O happens here, it is just added to a list. */
  223. PHP_FUNCTION(gearman_worker_add_server) {
  224. zval *zobj;
  225. gearman_worker_obj *obj;
  226. char *host = NULL;
  227. size_t host_len = 0;
  228. zend_long port = 0;
  229. zend_bool setupExceptionHandler = 1;
  230. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O|slb", &zobj,
  231. gearman_worker_ce,
  232. &host, &host_len,
  233. &port,
  234. &setupExceptionHandler
  235. ) == FAILURE) {
  236. RETURN_FALSE;
  237. }
  238. obj = Z_GEARMAN_WORKER_P(zobj);
  239. obj->ret = gearman_worker_add_server(&obj->worker, host, port);
  240. if (obj->ret != GEARMAN_SUCCESS) {
  241. php_error_docref(NULL, E_WARNING, "%s",
  242. gearman_worker_error(&obj->worker));
  243. RETURN_FALSE;
  244. }
  245. if (setupExceptionHandler && !gearman_worker_set_server_option(&(obj->worker), "exceptions", (sizeof("exceptions") - 1))) {
  246. GEARMAN_EXCEPTION("Failed to set exception option", 0);
  247. }
  248. RETURN_TRUE;
  249. }
  250. /* }}} */
  251. /* {{{ proto bool gearman_worker_add_servers(object worker [, string servers [, bool setupExceptionHandler = true]])
  252. Add a list of job servers to a worker. This goes into a list of servers that can be used to run tasks. No socket I/O happens here, it is just added to a list. */
  253. PHP_FUNCTION(gearman_worker_add_servers) {
  254. zval *zobj;
  255. gearman_worker_obj *obj;
  256. char *servers = NULL;
  257. size_t servers_len = 0;
  258. zend_bool setupExceptionHandler = 1;
  259. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O|sb", &zobj,
  260. gearman_worker_ce,
  261. &servers,
  262. &servers_len,
  263. &setupExceptionHandler
  264. ) == FAILURE) {
  265. RETURN_FALSE;
  266. }
  267. obj = Z_GEARMAN_WORKER_P(zobj);
  268. obj->ret = gearman_worker_add_servers(&obj->worker, servers);
  269. if (obj->ret != GEARMAN_SUCCESS) {
  270. php_error_docref(NULL, E_WARNING, "%s",
  271. gearman_worker_error(&obj->worker));
  272. RETURN_FALSE;
  273. }
  274. if (setupExceptionHandler && !gearman_worker_set_server_option(&(obj->worker), "exceptions", (sizeof("exceptions") - 1))) {
  275. GEARMAN_EXCEPTION("Failed to set exception option", 0);
  276. }
  277. RETURN_TRUE;
  278. }
  279. /* }}} */
  280. /* {{{ proto bool gearman_worker_wait(object worker)
  281. Wait for I/O activity on all connections in a worker. */
  282. PHP_FUNCTION(gearman_worker_wait) {
  283. zval *zobj;
  284. gearman_worker_obj *obj;
  285. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_worker_ce) == FAILURE) {
  286. RETURN_FALSE;
  287. }
  288. obj = Z_GEARMAN_WORKER_P(zobj);
  289. obj->ret = gearman_worker_wait(&(obj->worker));
  290. if (! PHP_GEARMAN_CLIENT_RET_OK(obj->ret)) {
  291. if (obj->ret != GEARMAN_TIMEOUT) {
  292. php_error_docref(NULL, E_WARNING, "%s",
  293. gearman_worker_error(&(obj->worker)));
  294. }
  295. RETURN_FALSE;
  296. }
  297. RETURN_TRUE;
  298. }
  299. /* }}} */
  300. /* {{{ proto bool gearman_worker_register(object worker, string function [, int timeout ])
  301. Register function with job servers with an optional timeout. The timeout specifies how many seconds the server will wait before marking a job as failed. If timeout is zero, there is no timeout. */
  302. PHP_FUNCTION(gearman_worker_register) {
  303. zval *zobj;
  304. gearman_worker_obj *obj;
  305. char *function_name;
  306. size_t function_name_len;
  307. zend_long timeout = 0;
  308. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os|l", &zobj, gearman_worker_ce,
  309. &function_name, &function_name_len,
  310. &timeout
  311. ) == FAILURE) {
  312. RETURN_FALSE;
  313. }
  314. obj = Z_GEARMAN_WORKER_P(zobj);
  315. obj->ret = gearman_worker_register(&(obj->worker), function_name, timeout);
  316. if (obj->ret != GEARMAN_SUCCESS) {
  317. php_error_docref(NULL, E_WARNING, "%s",
  318. gearman_worker_error(&(obj->worker)));
  319. RETURN_FALSE;
  320. }
  321. RETURN_TRUE;
  322. }
  323. /* }}} */
  324. /* {{{ proto bool gearman_worker_unregister(object worker, string function)
  325. Unregister function with job servers. */
  326. PHP_FUNCTION(gearman_worker_unregister) {
  327. zval *zobj;
  328. gearman_worker_obj *obj;
  329. char *function_name;
  330. size_t function_name_len;
  331. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &zobj, gearman_worker_ce,
  332. &function_name, &function_name_len
  333. ) == FAILURE) {
  334. RETURN_FALSE;
  335. }
  336. obj = Z_GEARMAN_WORKER_P(zobj);
  337. obj->ret = gearman_worker_unregister(&(obj->worker), function_name);
  338. if (obj->ret != GEARMAN_SUCCESS) {
  339. php_error_docref(NULL, E_WARNING, "%s",
  340. gearman_worker_error(&(obj->worker)));
  341. RETURN_FALSE;
  342. }
  343. RETURN_TRUE;
  344. }
  345. /* }}} */
  346. /* {{{ proto bool gearman_worker_unregister_all(object worker)
  347. Unregister all functions with job servers. */
  348. PHP_FUNCTION(gearman_worker_unregister_all) {
  349. zval *zobj;
  350. gearman_worker_obj *obj;
  351. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_worker_ce) == FAILURE) {
  352. RETURN_FALSE;
  353. }
  354. obj = Z_GEARMAN_WORKER_P(zobj);
  355. obj->ret= gearman_worker_unregister_all(&(obj->worker));
  356. if (obj->ret != GEARMAN_SUCCESS) {
  357. php_error_docref(NULL, E_WARNING, "%s",
  358. gearman_worker_error(&(obj->worker)));
  359. RETURN_FALSE;
  360. }
  361. RETURN_TRUE;
  362. }
  363. /* }}} */
  364. /* {{{ proto false|object gearman_worker_grab_job(obect worker)
  365. Get a job from one of the job servers.
  366. Note: EXPERIMENTAL - This is undocumented on php.net and needs a test*/
  367. PHP_FUNCTION(gearman_worker_grab_job) {
  368. zval *zobj;
  369. gearman_worker_obj *obj;
  370. gearman_job_obj *job;
  371. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_worker_ce) == FAILURE) {
  372. RETURN_FALSE;
  373. }
  374. obj = Z_GEARMAN_WORKER_P(zobj);
  375. object_init_ex(return_value, gearman_job_ce);
  376. job = Z_GEARMAN_JOB_P(return_value);
  377. job->job = gearman_worker_grab_job(&(obj->worker), NULL, &obj->ret);
  378. if (obj->ret != GEARMAN_SUCCESS && obj->ret != GEARMAN_IO_WAIT) {
  379. php_error_docref(NULL, E_WARNING, "%s",
  380. gearman_worker_error(&(obj->worker)));
  381. zval_dtor(return_value);
  382. RETURN_FALSE;
  383. }
  384. job->flags |= GEARMAN_JOB_OBJ_CREATED;
  385. }
  386. /* *job is passed in via gearman, need to convert that into a zval that
  387. * is accessable in the user_defined php callback function */
  388. static void *_php_worker_function_callback(gearman_job_st *job,
  389. void *context,
  390. size_t *result_size,
  391. gearman_return_t *ret_ptr) {
  392. zval zjob, message;
  393. gearman_job_obj *jobj;
  394. gearman_worker_cb_obj *worker_cb = (gearman_worker_cb_obj *)context;
  395. char *result = NULL;
  396. uint32_t param_count;
  397. /* cb vars */
  398. zval argv[2], retval;
  399. /* first create our job object that will be passed to the callback */
  400. if (object_init_ex(&zjob, gearman_job_ce) != SUCCESS) {
  401. php_error_docref(NULL, E_WARNING, "Failed to create gearman_job_ce object.");
  402. return result;
  403. }
  404. jobj = Z_GEARMAN_JOB_P(&zjob);
  405. jobj->job = job;
  406. ZVAL_COPY_VALUE(&argv[0], &zjob);
  407. if (Z_ISUNDEF(worker_cb->zdata)) {
  408. param_count = 1;
  409. ZVAL_NULL(&argv[1]);
  410. } else {
  411. ZVAL_COPY(&argv[1], &worker_cb->zdata);
  412. param_count = 2;
  413. }
  414. jobj->ret = GEARMAN_SUCCESS;
  415. if (call_user_function(EG(function_table), NULL, &worker_cb->zcall, &retval, param_count, argv) != SUCCESS) {
  416. php_error_docref(NULL,
  417. E_WARNING,
  418. "Could not call the function %s",
  419. ( Z_ISUNDEF(worker_cb->zcall) || Z_TYPE(worker_cb->zcall) != IS_STRING) ? "[undefined]" : Z_STRVAL(worker_cb->zcall)
  420. );
  421. jobj->ret = GEARMAN_WORK_FAIL;
  422. }
  423. *ret_ptr = jobj->ret;
  424. if (EG(exception)) {
  425. *ret_ptr = GEARMAN_WORK_EXCEPTION;
  426. ZVAL_STRING(&message, "Unable to add worker function");
  427. jobj->ret = gearman_job_send_exception(jobj->job, Z_STRVAL(message), Z_STRLEN(message));
  428. if (jobj->ret != GEARMAN_SUCCESS && jobj->ret != GEARMAN_IO_WAIT) {
  429. php_error_docref(NULL, E_WARNING, "Unable to add worker function: %s",
  430. gearman_job_error(jobj->job));
  431. }
  432. }
  433. if (Z_ISUNDEF(retval)) {
  434. result = NULL;
  435. *result_size = 0;
  436. } else {
  437. if (Z_TYPE(retval) != IS_STRING) {
  438. convert_to_string(&retval);
  439. }
  440. result = estrndup(Z_STRVAL(retval), Z_STRLEN(retval));
  441. *result_size = Z_STRLEN(retval);
  442. zval_dtor(&retval);
  443. }
  444. if (!Z_ISUNDEF(argv[0])) {
  445. zval_dtor(&argv[0]);
  446. }
  447. if (!Z_ISUNDEF(argv[1])) {
  448. zval_dtor(&argv[1]);
  449. }
  450. return result;
  451. }
  452. /* }}} */
  453. /* {{{ proto bool gearman_worker_add_function(object worker, zval function_name, zval callback [, zval data [, int timeout]])
  454. Register and add callback function for worker. */
  455. PHP_FUNCTION(gearman_worker_add_function) {
  456. zval *zobj = NULL;
  457. gearman_worker_obj *obj;
  458. gearman_worker_cb_obj *worker_cb;
  459. zval *zname, *zcall, *zdata = NULL;
  460. zend_long timeout = 0;
  461. zend_string *callable = NULL;
  462. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Ozz|zl", &zobj, gearman_worker_ce,
  463. &zname,
  464. &zcall,
  465. &zdata,
  466. &timeout
  467. ) == FAILURE) {
  468. RETURN_FALSE;
  469. }
  470. obj = Z_GEARMAN_WORKER_P(zobj);
  471. /* check that the function name is a string */
  472. if (Z_TYPE_P(zname) != IS_STRING) {
  473. php_error_docref(NULL, E_WARNING, "Function name must be a string");
  474. RETURN_FALSE;
  475. }
  476. /* check that the function can be called */
  477. if (!zend_is_callable(zcall, 0, &callable)) {
  478. php_error_docref(NULL, E_WARNING, "Function '%s' is not a valid callback", ZSTR_VAL(callable));
  479. zend_string_release(callable);
  480. RETURN_FALSE;
  481. }
  482. zend_string_release(callable);
  483. /* create a new worker cb */
  484. worker_cb = emalloc(sizeof(gearman_worker_cb_obj));
  485. // Name of the callback function
  486. ZVAL_COPY(&worker_cb->zname, zname);
  487. // Reference to the callback function
  488. ZVAL_COPY(&worker_cb->zcall, zcall);
  489. // Additional data passed along to the callback function
  490. if (zdata) {
  491. ZVAL_COPY(&worker_cb->zdata,zdata);
  492. } else {
  493. ZVAL_NULL(&worker_cb->zdata);
  494. }
  495. // Add the worker_cb to the list
  496. zend_hash_next_index_insert_ptr(Z_ARRVAL(obj->cb_list), worker_cb);
  497. /* add the function */
  498. /* NOTE: _php_worker_function_callback is a wrapper that calls
  499. * the function defined by gearman_worker_add_function */
  500. obj->ret = gearman_worker_add_function(&(obj->worker),
  501. Z_STRVAL(worker_cb->zname),
  502. (uint32_t)timeout,
  503. _php_worker_function_callback,
  504. (void *)worker_cb
  505. );
  506. if (obj->ret != GEARMAN_SUCCESS) {
  507. php_error_docref(NULL, E_WARNING, "Unable to add function to Gearman Worker: %s %s",
  508. gearman_worker_error(&(obj->worker)), gearman_strerror(obj->ret));
  509. RETURN_FALSE;
  510. }
  511. RETURN_TRUE;
  512. }
  513. /* }}} */
  514. /* {{{ proto bool gearman_worker_work(object worker)
  515. Wait for a job and call the appropriate callback function when it gets one. */
  516. PHP_FUNCTION(gearman_worker_work) {
  517. zval *zobj = NULL;
  518. gearman_worker_obj *obj;
  519. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_worker_ce) == FAILURE) {
  520. RETURN_FALSE;
  521. }
  522. obj = Z_GEARMAN_WORKER_P(zobj);
  523. obj->ret = gearman_worker_work(&(obj->worker));
  524. if (obj->ret != GEARMAN_SUCCESS && obj->ret != GEARMAN_IO_WAIT &&
  525. obj->ret != GEARMAN_WORK_FAIL && obj->ret != GEARMAN_TIMEOUT &&
  526. obj->ret != GEARMAN_WORK_EXCEPTION && obj->ret != GEARMAN_NO_JOBS) {
  527. php_error_docref(NULL, E_WARNING, "%s",
  528. gearman_worker_error(&(obj->worker)));
  529. RETURN_FALSE;
  530. }
  531. if (obj->ret != GEARMAN_SUCCESS) {
  532. RETURN_FALSE;
  533. }
  534. RETURN_TRUE;
  535. }
  536. /* }}} */
  537. /* {{{ proto bool gearman_worker_ping(object worker, string data)
  538. Send data to all job servers to see if they echo it back. */
  539. PHP_FUNCTION(gearman_worker_ping) {
  540. zval *zobj;
  541. gearman_worker_obj *obj;
  542. char *workload;
  543. size_t workload_len;
  544. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &zobj, gearman_worker_ce,
  545. &workload, &workload_len) == FAILURE) {
  546. RETURN_FALSE;
  547. }
  548. obj = Z_GEARMAN_WORKER_P(zobj);
  549. obj->ret = gearman_worker_echo(&(obj->worker), workload, (size_t)workload_len);
  550. if (obj->ret != GEARMAN_SUCCESS && obj->ret != GEARMAN_IO_WAIT) {
  551. php_error_docref(NULL, E_WARNING, "%s",
  552. gearman_worker_error(&(obj->worker)));
  553. RETURN_FALSE;
  554. }
  555. RETURN_TRUE;
  556. }
  557. /* }}} */
  558. /* {{{ proto bool GearmanWorker::enableExceptionHandler()
  559. Enable exception handling to be used by exception callback function
  560. GearmanWorker::enableExceptionHandler */
  561. PHP_FUNCTION(gearman_worker_enable_exception_handler) {
  562. gearman_worker_obj *obj;
  563. zval *zobj;
  564. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_worker_ce) == FAILURE) {
  565. RETURN_FALSE;
  566. }
  567. obj = Z_GEARMAN_WORKER_P(zobj);
  568. if (!gearman_worker_set_server_option(&(obj->worker), "exceptions", (sizeof("exceptions") - 1))) {
  569. GEARMAN_EXCEPTION("Failed to set exception option", 0);
  570. }
  571. RETURN_TRUE;
  572. }
  573. /* }}} */