forked from wcgallego/pecl-gearman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
php_gearman_worker.c
722 lines (590 loc) · 24.1 KB
/
php_gearman_worker.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
/*
* Gearman PHP Extension
*
* Copyright (C) 2008 James M. Luedke <[email protected]>,
* Eric Day <[email protected]>
* All rights reserved.
*
* Use and distribution licensed under the PHP license. See
* the LICENSE file in this directory for full text.
*/
#include "php_gearman_worker.h"
gearman_worker_obj *gearman_worker_fetch_object(zend_object *obj) {
return (gearman_worker_obj *)((char*)(obj) - XtOffsetOf(gearman_worker_obj, std));
}
/* {{{ proto object gearman_worker_ctor()
Initialize a worker object. */
static void gearman_worker_ctor(INTERNAL_FUNCTION_PARAMETERS) {
gearman_worker_obj *worker;
if (zend_parse_parameters_none() == FAILURE) {
return;
}
worker = Z_GEARMAN_WORKER_P(return_value);
if (gearman_worker_create(&(worker->worker)) == NULL) {
zval_dtor(return_value);
GEARMAN_EXCEPTION("Memory allocation failure", 0);
}
worker->flags |= GEARMAN_WORKER_OBJ_CREATED;
gearman_worker_set_workload_malloc_fn(&(worker->worker), _php_malloc, NULL);
gearman_worker_set_workload_free_fn(&(worker->worker), _php_free, NULL);
}
/* }}} */
/* {{{ proto object gearman_worker_create()
Returns a worker object */
PHP_FUNCTION(gearman_worker_create) {
if (object_init_ex(return_value, gearman_worker_ce) != SUCCESS) {
php_error_docref(NULL, E_WARNING,
"GearmanWorker Object creation failure.");
RETURN_FALSE;
}
gearman_worker_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU);
}
/* }}} */
/* {{{ proto object GearmanWorker::__construct()
Returns a worker object */
PHP_METHOD(GearmanWorker, __construct) {
return_value = getThis();
gearman_worker_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU);
}
/* }}} */
/* {{{ proto object GearmanWorker::__destruct()
Destroys a worker object */
PHP_METHOD(GearmanWorker, __destruct) {
gearman_worker_obj *intern = Z_GEARMAN_WORKER_P(getThis());
if (!intern) {
return;
}
if (intern->flags & GEARMAN_WORKER_OBJ_CREATED) {
gearman_worker_free(&(intern->worker));
}
zval_dtor(&intern->cb_list);
zend_object_std_dtor(&intern->std);
}
/* }}} */
static inline void cb_list_dtor(zval *zv) {
gearman_worker_cb_obj *worker_cb = Z_PTR_P(zv);
zval_dtor(&worker_cb->zname);
zval_dtor(&worker_cb->zdata);
zval_dtor(&worker_cb->zcall);
efree(worker_cb);
}
zend_object *gearman_worker_obj_new(zend_class_entry *ce) {
gearman_worker_obj *intern = ecalloc(1,
sizeof(gearman_worker_obj) +
zend_object_properties_size(ce));
zend_object_std_init(&(intern->std), ce);
object_properties_init(&intern->std, ce);
ZVAL_NEW_ARR(&intern->cb_list);
zend_hash_init(Z_ARRVAL(intern->cb_list), 0, NULL, cb_list_dtor, 0);
intern->std.handlers = &gearman_worker_obj_handlers;
return &intern->std;
}
/* {{{ proto int gearman_worker_return_code()
get last gearman_return_t */
PHP_FUNCTION(gearman_worker_return_code) {
gearman_worker_obj *obj;
zval *zobj;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_worker_ce) == FAILURE) {
RETURN_NULL();
}
obj = Z_GEARMAN_WORKER_P(zobj);
RETURN_LONG(obj->ret);
}
/* }}} */
/* {{{ proto string gearman_worker_error(object worker)
Return an error string for the last error encountered. */
PHP_FUNCTION(gearman_worker_error) {
char *error;
zval *zobj;
gearman_worker_obj *obj;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_worker_ce) == FAILURE) {
RETURN_FALSE;
}
obj = Z_GEARMAN_WORKER_P(zobj);
error = (char *)gearman_worker_error(&(obj->worker));
if (error) {
RETURN_STRING(error);
}
RETURN_FALSE;
}
/* }}} */
/* {{{ proto int gearman_worker_errno(object worker)
Value of errno in the case of a GEARMAN_ERRNO return value. */
PHP_FUNCTION(gearman_worker_errno) {
zval *zobj;
gearman_worker_obj *obj;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_worker_ce) == FAILURE) {
RETURN_FALSE;
}
obj = Z_GEARMAN_WORKER_P(zobj);
RETURN_LONG(gearman_worker_errno(&(obj->worker)))
}
/* }}} */
/* {{{ proto int gearman_worker_options(object worker)
Get options for a worker structure. */
PHP_FUNCTION(gearman_worker_options) {
zval *zobj;
gearman_worker_obj *obj;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_worker_ce) == FAILURE) {
RETURN_NULL();
}
obj = Z_GEARMAN_WORKER_P(zobj);
RETURN_LONG(gearman_worker_options(&(obj->worker)))
}
/* }}} */
/* {{{ proto void gearman_worker_set_options(object worker, constant option)
Set options for a worker structure. */
PHP_FUNCTION(gearman_worker_set_options) {
zval *zobj;
gearman_worker_obj *obj;
zend_long options;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Ol", &zobj, gearman_worker_ce, &options) == FAILURE) {
RETURN_NULL();
}
obj = Z_GEARMAN_WORKER_P(zobj);
gearman_worker_set_options(&(obj->worker), options);
RETURN_TRUE;
}
/* }}} */
/* {{{ proto void gearman_worker_add_options(object worker, constant option)
Set options for a worker structure. */
PHP_FUNCTION(gearman_worker_add_options) {
zval *zobj;
gearman_worker_obj *obj;
zend_long options;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Ol", &zobj, gearman_worker_ce, &options) == FAILURE) {
RETURN_NULL();
}
obj = Z_GEARMAN_WORKER_P(zobj);
gearman_worker_add_options(&(obj->worker), options);
RETURN_TRUE;
}
/* }}} */
/* {{{ proto void gearman_worker_remove_options(object worker, constant option)
Set options for a worker structure. */
PHP_FUNCTION(gearman_worker_remove_options) {
zval *zobj;
gearman_worker_obj *obj;
zend_long options;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Ol", &zobj, gearman_worker_ce, &options) == FAILURE) {
RETURN_NULL();
}
obj = Z_GEARMAN_WORKER_P(zobj);
gearman_worker_remove_options(&(obj->worker), options);
RETURN_TRUE;
}
/* }}} */
/* {{{ proto int gearman_worker_timeout(object worker)
Get timeout for a worker structure. */
PHP_FUNCTION(gearman_worker_timeout) {
zval *zobj;
gearman_worker_obj *obj;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_worker_ce) == FAILURE) {
RETURN_NULL();
}
obj = Z_GEARMAN_WORKER_P(zobj);
RETURN_LONG(gearman_worker_timeout(&(obj->worker)))
}
/* }}} */
/* {{{ proto void gearman_worker_set_timeout(object worker, constant timeout)
Set timeout for a worker structure. */
PHP_FUNCTION(gearman_worker_set_timeout) {
zval *zobj;
gearman_worker_obj *obj;
zend_long timeout;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Ol", &zobj, gearman_worker_ce, &timeout) == FAILURE) {
RETURN_FALSE;
}
obj = Z_GEARMAN_WORKER_P(zobj);
gearman_worker_set_timeout(&(obj->worker), timeout);
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool gearman_worker_set_ssl(object worker, bool ssl [, string ca_file [, string certificate [, string key_file ]]])
Set SSL for a worker structure. */
PHP_FUNCTION(gearman_worker_set_ssl) {
zend_bool ssl = 0;
char *ca_file = NULL;
char *certificate = NULL;
char *key_file = NULL;
size_t ca_file_len = 0;
size_t certificate_len = 0;
size_t key_file_len = 0;
gearman_worker_obj *obj;
zval *zobj;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O|bsss",
&zobj, gearman_client_ce,
&ssl, &ca_file, &ca_file_len,
&certificate, &certificate_len,
&key_file, &key_file_len) == FAILURE) {
RETURN_FALSE;
}
obj = Z_GEARMAN_WORKER_P(zobj);
if (ca_file == NULL || ca_file_len == (size_t) 0) {
cfg_get_string("gearman.ssl_ca_file", &ca_file);
}
if (certificate == NULL || certificate_len == (size_t) 0) {
cfg_get_string("gearman.ssl_certificate", &certificate);
}
if (key_file == NULL || key_file_len == (size_t) 0) {
cfg_get_string("gearman.ssl_key_file", &key_file);
}
gearman_worker_set_ssl(&(obj->worker), ssl, ca_file, certificate, key_file);
if (obj->ret != GEARMAN_SUCCESS) {
php_error_docref(NULL, E_WARNING, "%s",
gearman_worker_error(&obj->worker));
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto void gearman_worker_set_id(object worker, string id)
Set id for a worker structure. */
PHP_FUNCTION(gearman_worker_set_id) {
zval *zobj;
gearman_worker_obj *obj;
char *id;
size_t id_len;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &zobj, gearman_worker_ce,
&id, &id_len) == FAILURE) {
RETURN_FALSE;
}
obj = Z_GEARMAN_WORKER_P(zobj);
if(gearman_failed(gearman_worker_set_identifier(&(obj->worker), id, id_len))) {
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool gearman_worker_add_server(object worker [, string host [, int port ]])
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. */
PHP_FUNCTION(gearman_worker_add_server) {
zval *zobj;
gearman_worker_obj *obj;
char *host = NULL;
size_t host_len = 0;
zend_long port = 0;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O|sl", &zobj,
gearman_worker_ce,
&host, &host_len,
&port
) == FAILURE) {
RETURN_FALSE;
}
obj = Z_GEARMAN_WORKER_P(zobj);
obj->ret = gearman_worker_add_server(&obj->worker, host, port);
if (obj->ret != GEARMAN_SUCCESS) {
php_error_docref(NULL, E_WARNING, "%s",
gearman_worker_error(&obj->worker));
RETURN_FALSE;
}
/* if (! gearman_worker_set_server_option(&(obj->worker), "exceptions", (sizeof("exceptions") - 1))) { */
/* GEARMAN_EXCEPTION("Failed to set exception option", 0); */
/* } */
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool gearman_worker_add_servers(object worker [, string servers])
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. */
PHP_FUNCTION(gearman_worker_add_servers) {
zval *zobj;
gearman_worker_obj *obj;
char *servers = NULL;
size_t servers_len = 0;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &zobj,
gearman_worker_ce,
&servers, &servers_len
) == FAILURE) {
RETURN_FALSE;
}
obj = Z_GEARMAN_WORKER_P(zobj);
obj->ret = gearman_worker_add_servers(&obj->worker, servers);
if (obj->ret != GEARMAN_SUCCESS) {
php_error_docref(NULL, E_WARNING, "%s",
gearman_worker_error(&obj->worker));
RETURN_FALSE;
}
/* if (! gearman_worker_set_server_option(&(obj->worker), "exceptions", (sizeof("exceptions") - 1))) { */
/* GEARMAN_EXCEPTION("Failed to set exception option", 0); */
/* } */
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool gearman_worker_wait(object worker)
Wait for I/O activity on all connections in a worker. */
PHP_FUNCTION(gearman_worker_wait) {
zval *zobj;
gearman_worker_obj *obj;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_worker_ce) == FAILURE) {
RETURN_FALSE;
}
obj = Z_GEARMAN_WORKER_P(zobj);
obj->ret = gearman_worker_wait(&(obj->worker));
if (! PHP_GEARMAN_CLIENT_RET_OK(obj->ret)) {
if (obj->ret != GEARMAN_TIMEOUT) {
php_error_docref(NULL, E_WARNING, "%s",
gearman_worker_error(&(obj->worker)));
}
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool gearman_worker_register(object worker, string function [, int timeout ])
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. */
PHP_FUNCTION(gearman_worker_register) {
zval *zobj;
gearman_worker_obj *obj;
char *function_name;
size_t function_name_len;
zend_long timeout = 0;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os|l", &zobj, gearman_worker_ce,
&function_name, &function_name_len,
&timeout
) == FAILURE) {
RETURN_FALSE;
}
obj = Z_GEARMAN_WORKER_P(zobj);
obj->ret = gearman_worker_register(&(obj->worker), function_name, timeout);
if (obj->ret != GEARMAN_SUCCESS) {
php_error_docref(NULL, E_WARNING, "%s",
gearman_worker_error(&(obj->worker)));
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool gearman_worker_unregister(object worker, string function)
Unregister function with job servers. */
PHP_FUNCTION(gearman_worker_unregister) {
zval *zobj;
gearman_worker_obj *obj;
char *function_name;
size_t function_name_len;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &zobj, gearman_worker_ce,
&function_name, &function_name_len
) == FAILURE) {
RETURN_FALSE;
}
obj = Z_GEARMAN_WORKER_P(zobj);
obj->ret = gearman_worker_unregister(&(obj->worker), function_name);
if (obj->ret != GEARMAN_SUCCESS) {
php_error_docref(NULL, E_WARNING, "%s",
gearman_worker_error(&(obj->worker)));
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool gearman_worker_unregister_all(object worker)
Unregister all functions with job servers. */
PHP_FUNCTION(gearman_worker_unregister_all) {
zval *zobj;
gearman_worker_obj *obj;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_worker_ce) == FAILURE) {
RETURN_FALSE;
}
obj = Z_GEARMAN_WORKER_P(zobj);
obj->ret= gearman_worker_unregister_all(&(obj->worker));
if (obj->ret != GEARMAN_SUCCESS) {
php_error_docref(NULL, E_WARNING, "%s",
gearman_worker_error(&(obj->worker)));
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto object gearman_worker_grab_job(obect worker)
Get a job from one of the job servers.
Note: EXPERIMENTAL - This is undocumented on php.net and needs a test*/
PHP_FUNCTION(gearman_worker_grab_job) {
zval *zobj;
gearman_worker_obj *obj;
gearman_job_obj *job;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_worker_ce) == FAILURE) {
RETURN_FALSE;
}
obj = Z_GEARMAN_WORKER_P(zobj);
object_init_ex(return_value, gearman_job_ce);
job = Z_GEARMAN_JOB_P(return_value);
job->job = gearman_worker_grab_job(&(obj->worker), NULL, &obj->ret);
if (obj->ret != GEARMAN_SUCCESS && obj->ret != GEARMAN_IO_WAIT) {
php_error_docref(NULL, E_WARNING, "%s",
gearman_worker_error(&(obj->worker)));
zval_dtor(return_value);
RETURN_FALSE;
}
job->flags |= GEARMAN_JOB_OBJ_CREATED;
}
/* *job is passed in via gearman, need to convert that into a zval that
* is accessable in the user_defined php callback function */
static void *_php_worker_function_callback(gearman_job_st *job,
void *context,
size_t *result_size,
gearman_return_t *ret_ptr) {
zval zjob, message;
gearman_job_obj *jobj;
gearman_worker_cb_obj *worker_cb = (gearman_worker_cb_obj *)context;
char *result = NULL;
uint32_t param_count;
/* cb vars */
zval argv[2], retval;
/* first create our job object that will be passed to the callback */
if (object_init_ex(&zjob, gearman_job_ce) != SUCCESS) {
php_error_docref(NULL, E_WARNING, "Failed to create gearman_job_ce object.");
return result;
}
jobj = Z_GEARMAN_JOB_P(&zjob);
jobj->job = job;
ZVAL_COPY_VALUE(&argv[0], &zjob);
if (Z_ISUNDEF(worker_cb->zdata)) {
param_count = 1;
ZVAL_NULL(&argv[1]);
} else {
ZVAL_COPY(&argv[1], &worker_cb->zdata);
param_count = 2;
}
jobj->ret = GEARMAN_SUCCESS;
if (call_user_function_ex(EG(function_table), NULL, &worker_cb->zcall, &retval, param_count, argv, 0, NULL) != SUCCESS) {
php_error_docref(NULL,
E_WARNING,
"Could not call the function %s",
( Z_ISUNDEF(worker_cb->zcall) || Z_TYPE(worker_cb->zcall) != IS_STRING) ? "[undefined]" : Z_STRVAL(worker_cb->zcall)
);
jobj->ret = GEARMAN_WORK_FAIL;
}
*ret_ptr = jobj->ret;
if (EG(exception)) {
*ret_ptr = GEARMAN_WORK_EXCEPTION;
ZVAL_STRING(&message, "Unable to add worker function");
jobj->ret = gearman_job_send_exception(jobj->job, Z_STRVAL(message), Z_STRLEN(message));
if (jobj->ret != GEARMAN_SUCCESS && jobj->ret != GEARMAN_IO_WAIT) {
php_error_docref(NULL, E_WARNING, "Unable to add worker function: %s",
gearman_job_error(jobj->job));
}
}
if (Z_ISUNDEF(retval)) {
result = NULL;
*result_size = 0;
} else {
if (Z_TYPE(retval) != IS_STRING) {
convert_to_string(&retval);
}
result = estrndup(Z_STRVAL(retval), Z_STRLEN(retval));
*result_size = Z_STRLEN(retval);
zval_dtor(&retval);
}
if (!Z_ISUNDEF(argv[0])) {
zval_dtor(&argv[0]);
}
if (!Z_ISUNDEF(argv[1])) {
zval_dtor(&argv[1]);
}
return result;
}
/* }}} */
/* {{{ proto bool gearman_worker_add_function(object worker, zval function_name, zval callback [, zval data [, int timeout]])
Register and add callback function for worker. */
PHP_FUNCTION(gearman_worker_add_function) {
zval *zobj = NULL;
gearman_worker_obj *obj;
gearman_worker_cb_obj *worker_cb;
zval *zname, *zcall, *zdata = NULL;
zend_long timeout = 0;
zend_string *callable = NULL;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Ozz|zl", &zobj, gearman_worker_ce,
&zname,
&zcall,
&zdata,
&timeout
) == FAILURE) {
RETURN_FALSE;
}
obj = Z_GEARMAN_WORKER_P(zobj);
/* check that the function name is a string */
if (Z_TYPE_P(zname) != IS_STRING) {
php_error_docref(NULL, E_WARNING, "Function name must be a string");
RETURN_FALSE;
}
/* check that the function can be called */
if (!zend_is_callable(zcall, 0, &callable)) {
php_error_docref(NULL, E_WARNING, "Function '%s' is not a valid callback", ZSTR_VAL(callable));
zend_string_release(callable);
RETURN_FALSE;
}
zend_string_release(callable);
/* create a new worker cb */
worker_cb = emalloc(sizeof(gearman_worker_cb_obj));
// Name of the callback function
ZVAL_COPY(&worker_cb->zname, zname);
// Reference to the callback function
ZVAL_COPY(&worker_cb->zcall, zcall);
// Additional data passed along to the callback function
if (zdata) {
ZVAL_COPY(&worker_cb->zdata,zdata);
} else {
ZVAL_NULL(&worker_cb->zdata);
}
// Add the worker_cb to the list
zend_hash_next_index_insert_ptr(Z_ARRVAL(obj->cb_list), worker_cb);
/* add the function */
/* NOTE: _php_worker_function_callback is a wrapper that calls
* the function defined by gearman_worker_add_function */
obj->ret = gearman_worker_add_function(&(obj->worker),
Z_STRVAL(worker_cb->zname),
(uint32_t)timeout,
_php_worker_function_callback,
(void *)worker_cb
);
if (obj->ret != GEARMAN_SUCCESS) {
php_error_docref(NULL, E_WARNING, "Unable to add function to Gearman Worker: %s %s",
gearman_worker_error(&(obj->worker)), gearman_strerror(obj->ret));
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto int gearman_worker_work(object worker)
Wait for a job and call the appropriate callback function when it gets one. */
PHP_FUNCTION(gearman_worker_work) {
zval *zobj = NULL;
gearman_worker_obj *obj;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_worker_ce) == FAILURE) {
RETURN_FALSE;
}
obj = Z_GEARMAN_WORKER_P(zobj);
obj->ret = gearman_worker_work(&(obj->worker));
if (obj->ret != GEARMAN_SUCCESS && obj->ret != GEARMAN_IO_WAIT &&
obj->ret != GEARMAN_WORK_FAIL && obj->ret != GEARMAN_TIMEOUT &&
obj->ret != GEARMAN_WORK_EXCEPTION && obj->ret != GEARMAN_NO_JOBS) {
php_error_docref(NULL, E_WARNING, "%s",
gearman_worker_error(&(obj->worker)));
RETURN_FALSE;
}
if (obj->ret != GEARMAN_SUCCESS) {
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool gearman_worker_ping(object worker, string data)
Send data to all job servers to see if they echo it back. */
PHP_FUNCTION(gearman_worker_ping) {
zval *zobj;
gearman_worker_obj *obj;
char *workload;
size_t workload_len;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &zobj, gearman_worker_ce,
&workload, &workload_len) == FAILURE) {
RETURN_FALSE;
}
obj = Z_GEARMAN_WORKER_P(zobj);
obj->ret = gearman_worker_echo(&(obj->worker), workload, (size_t)workload_len);
if (obj->ret != GEARMAN_SUCCESS && obj->ret != GEARMAN_IO_WAIT) {
php_error_docref(NULL, E_WARNING, "%s",
gearman_worker_error(&(obj->worker)));
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */