forked from wcgallego/pecl-gearman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
php_gearman_client.c
1307 lines (1067 loc) · 45.1 KB
/
php_gearman_client.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
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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_task.h"
#include "php_gearman_client.h"
inline gearman_client_obj *gearman_client_fetch_object(zend_object *obj) {
return (gearman_client_obj *)((char*)(obj) - XtOffsetOf(gearman_client_obj, std));
}
static void gearman_client_ctor(INTERNAL_FUNCTION_PARAMETERS) {
gearman_client_obj *client;
if (zend_parse_parameters_none() == FAILURE) {
return;
}
client = Z_GEARMAN_CLIENT_P(return_value);
if (gearman_client_create(&(client->client)) == NULL) {
GEARMAN_EXCEPTION("Memory allocation failure", 0);
}
client->flags |= GEARMAN_CLIENT_OBJ_CREATED;
gearman_client_add_options(&(client->client), GEARMAN_CLIENT_FREE_TASKS);
gearman_client_set_workload_malloc_fn(&(client->client), _php_malloc, NULL);
gearman_client_set_workload_free_fn(&(client->client), _php_free, NULL);
gearman_client_set_task_context_free_fn(&(client->client), _php_task_free);
}
/* {{{ proto object gearman_client_create()
Returns a GearmanClient object */
PHP_FUNCTION(gearman_client_create) {
if (object_init_ex(return_value, gearman_client_ce) != SUCCESS) {
php_error_docref(NULL, E_WARNING, "Object creation failure.");
RETURN_FALSE;
}
gearman_client_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU);
}
inline zend_object *gearman_client_obj_new(zend_class_entry *ce) {
gearman_client_obj *intern = ecalloc(1,
sizeof(gearman_client_obj) +
zend_object_properties_size(ce));
zend_object_std_init(&(intern->std), ce);
object_properties_init(&intern->std, ce);
array_init(&intern->task_list);
intern->created_tasks = 0;
intern->std.handlers = &gearman_client_obj_handlers;
return &intern->std;
}
/* {{{ proto object GearmanClient::__construct()
Returns a GearmanClient object */
PHP_METHOD(GearmanClient, __construct)
{
return_value = getThis();
gearman_client_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU);
}
/* }}} */
/* {{{ proto object GearmanClient::__destruct()
cleans up GearmanClient object */
PHP_METHOD(GearmanClient, __destruct)
{
char *context = NULL;
gearman_client_obj *intern = Z_GEARMAN_CLIENT_P(getThis());
if (!intern) {
return;
}
context = gearman_client_context(&(intern->client));
efree(context);
if (intern->flags & GEARMAN_CLIENT_OBJ_CREATED) {
gearman_client_free(&intern->client);
}
// Clear Callbacks
zval_dtor(&intern->zworkload_fn);
zval_dtor(&intern->zcreated_fn);
zval_dtor(&intern->zdata_fn);
zval_dtor(&intern->zwarning_fn);
zval_dtor(&intern->zstatus_fn);
zval_dtor(&intern->zcomplete_fn);
zval_dtor(&intern->zexception_fn);
zval_dtor(&intern->zfail_fn);
zval_dtor(&intern->task_list);
zend_object_std_dtor(&intern->std);
}
/* {{{ proto int gearman_client_return_code()
get last gearman_return_t */
PHP_FUNCTION(gearman_client_return_code)
{
gearman_client_obj *obj;
zval *zobj;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_client_ce) == FAILURE) {
RETURN_NULL();
}
obj = Z_GEARMAN_CLIENT_P(zobj);
RETURN_LONG(obj->ret);
}
/* }}} */
/* {{{ proto string gearman_client_error()
Return an error string for the last error encountered. */
PHP_FUNCTION(gearman_client_error) {
char *error = NULL;
gearman_client_obj *obj;
zval *zobj;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_client_ce) == FAILURE) {
RETURN_NULL();
}
obj = Z_GEARMAN_CLIENT_P(zobj);
error = (char *)gearman_client_error(&(obj->client));
if (error) {
RETURN_STRING(error)
}
RETURN_FALSE;
}
/* }}} */
/* {{{ proto int gearman_client_get_errno()
Value of errno in the case of a GEARMAN_ERRNO return value. */
PHP_FUNCTION(gearman_client_get_errno) {
gearman_client_obj *obj;
zval *zobj;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_client_ce) == FAILURE) {
RETURN_NULL();
}
obj = Z_GEARMAN_CLIENT_P(zobj);
RETURN_LONG(gearman_client_errno(&(obj->client)))
}
/* }}} */
/* {{{ proto int gearman_client_options()
Get options for a client structure. */
PHP_FUNCTION(gearman_client_options) {
gearman_client_obj *obj;
zval *zobj;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_client_ce) == FAILURE) {
RETURN_NULL();
}
obj = Z_GEARMAN_CLIENT_P(zobj);
RETURN_LONG(gearman_client_options(&(obj->client)))
}
/* }}} */
/* {{{ proto void gearman_client_set_options(constant option)
Set options for a client structure.
NOTE: this is deprecated in gearmand */
PHP_FUNCTION(gearman_client_set_options) {
zend_long options;
gearman_client_obj *obj;
zval *zobj;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Ol", &zobj, gearman_client_ce, &options) == FAILURE) {
RETURN_FALSE;
}
obj = Z_GEARMAN_CLIENT_P(zobj);
gearman_client_set_options(&(obj->client), options);
RETURN_TRUE;
}
/* }}} */
/* {{{ proto void GearmanClient::addOptions(constant option)
Set options for a client structure. */
PHP_FUNCTION(gearman_client_add_options) {
zend_long options;
gearman_client_obj *obj;
zval *zobj;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Ol", &zobj, gearman_client_ce, &options) == FAILURE) {
RETURN_FALSE;
}
obj = Z_GEARMAN_CLIENT_P(zobj);
gearman_client_add_options(&(obj->client), options);
RETURN_TRUE;
}
/* }}} */
/* {{{ proto void GearmanClient::removeOptions(constant option)
Set options for a client structure. */
PHP_FUNCTION(gearman_client_remove_options) {
zend_long options;
gearman_client_obj *obj;
zval *zobj;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Ol", &zobj, gearman_client_ce, &options) == FAILURE) {
RETURN_FALSE;
}
obj = Z_GEARMAN_CLIENT_P(zobj);
gearman_client_remove_options(&(obj->client), options);
RETURN_TRUE;
}
/* }}} */
PHP_FUNCTION(gearman_client_timeout) {
gearman_client_obj *obj;
zval *zobj;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_client_ce) == FAILURE) {
RETURN_NULL();
}
obj = Z_GEARMAN_CLIENT_P(zobj);
RETURN_LONG(gearman_client_timeout(&(obj->client)))
}
/* }}} */
/* {{{ proto void gearman_client_set_timeout(object, constant timeout)
Set timeout for a client structure. */
PHP_FUNCTION(gearman_client_set_timeout) {
zend_long timeout;
gearman_client_obj *obj;
zval *zobj;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Ol", &zobj, gearman_client_ce, &timeout) == FAILURE) {
RETURN_FALSE;
}
obj = Z_GEARMAN_CLIENT_P(zobj);
gearman_client_set_timeout(&(obj->client), timeout);
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool gearman_client_set_ssl(object client, bool ssl [, string ca_file [, string certificate [, string key_file ]]])
Set SSL for a client structure. */
PHP_FUNCTION(gearman_client_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_client_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_CLIENT_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);
}
/* php_error_docref(NULL, E_WARNING, "ssl = %d", (int) ssl); */
/* php_error_docref(NULL, E_WARNING, "ca_file = %s\n", ca_file); */
/* php_error_docref(NULL, E_WARNING, "certificate = %s\n", certificate); */
/* php_error_docref(NULL, E_WARNING, "key_file = %s\n", key_file); */
gearman_client_set_ssl(&(obj->client), ssl, ca_file, certificate, key_file);
if (obj->ret != GEARMAN_SUCCESS) {
php_error_docref(NULL, E_WARNING, "%s",
gearman_client_error(&(obj->client)));
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool gearman_client_add_server(object client [, string host [, int
* port [, bool setupExceptionHandler ]]])
Add a job server to a client. 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_client_add_server) {
char *host = NULL;
size_t host_len = 0;
zend_long port = 0;
/* zend_bool setupExceptionHandler = 1; */
zend_bool setupExceptionHandler = 0;
gearman_client_obj *obj;
zval *zobj;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O|slb",
&zobj,
gearman_client_ce,
&host,
&host_len,
&port,
&setupExceptionHandler) == FAILURE) {
RETURN_FALSE;
}
obj = Z_GEARMAN_CLIENT_P(zobj);
obj->ret = gearman_client_add_server(&(obj->client), host, port);
if (obj->ret != GEARMAN_SUCCESS) {
php_error_docref(NULL, E_WARNING, "%s",
gearman_client_error(&(obj->client)));
RETURN_FALSE;
}
/* if (setupExceptionHandler && !gearman_client_set_server_option(&(obj->client), "exceptions", (sizeof("exceptions") - 1))) { */
/* GEARMAN_EXCEPTION("Failed to set exception option", 0); */
/* } */
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool gearman_client_add_servers(object client [, string servers [, bool setupExceptionHandler ]])
Add a list of job servers to a client. 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_client_add_servers) {
char *servers = NULL;
size_t servers_len = 0;
/* zend_bool setupExceptionHandler = 1; */
zend_bool setupExceptionHandler = 0;
gearman_client_obj *obj;
zval *zobj;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O|sb",
&zobj,
gearman_client_ce,
&servers,
&servers_len,
&setupExceptionHandler) == FAILURE) {
RETURN_FALSE;
}
obj = Z_GEARMAN_CLIENT_P(zobj);
obj->ret = gearman_client_add_servers(&(obj->client), servers);
if (obj->ret != GEARMAN_SUCCESS) {
php_error_docref(NULL, E_WARNING, "%s",
gearman_client_error(&(obj->client)));
RETURN_FALSE;
}
/* if (setupExceptionHandler && !gearman_client_set_server_option(&(obj->client), "exceptions", (sizeof("exceptions") - 1))) { */
/* GEARMAN_EXCEPTION("Failed to set exception option", 0); */
/* } */
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool GearmanClient::wait()
Wait for I/O activity on all connections in a client. */
PHP_FUNCTION(gearman_client_wait) {
gearman_client_obj *obj;
zval *zobj;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_client_ce) == FAILURE) {
RETURN_FALSE;
}
obj = Z_GEARMAN_CLIENT_P(zobj);
obj->ret = gearman_client_wait(&(obj->client));
if (! PHP_GEARMAN_CLIENT_RET_OK(obj->ret)) {
if (obj->ret != GEARMAN_TIMEOUT) {
php_error_docref(NULL, E_WARNING, "%s",
gearman_client_error(&(obj->client)));
}
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto object gearman_client_do_work_handler(void *add_task_func, object client, string function, zval workload [, string unique ])
Run a task, high/normal/low dependent upon do_work_func */
static void gearman_client_do_work_handler(void* (*do_work_func)(
gearman_client_st *client,
const char *function_name,
const char *unique,
const void *workload,
size_t workload_size,
size_t *result_size,
gearman_return_t *ret_ptr
),
INTERNAL_FUNCTION_PARAMETERS) {
char *function_name;
size_t function_name_len;
char *workload;
size_t workload_len;
char *unique = NULL;
size_t unique_len = 0;
void *result;
size_t result_size = 0;
gearman_client_obj *obj;
zval *zobj;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Oss|s", &zobj, gearman_client_ce,
&function_name, &function_name_len,
&workload, &workload_len,
&unique, &unique_len) == FAILURE) {
RETURN_EMPTY_STRING();
}
obj = Z_GEARMAN_CLIENT_P(zobj);
result = (char *)(*do_work_func)(
&(obj->client),
function_name,
unique,
workload,
(size_t)workload_len,
&result_size,
&(obj)->ret
);
if (! PHP_GEARMAN_CLIENT_RET_OK(obj->ret)) {
php_error_docref(NULL, E_WARNING, "%s", gearman_client_error(&(obj->client)));
RETURN_EMPTY_STRING();
}
/* NULL results are valid */
if (! result) {
RETURN_EMPTY_STRING();
}
RETURN_STRINGL((char *)result, (long) result_size);
}
/* }}} */
/* {{{ proto string GearmanClient::doNormal(string function, string workload [, string unique ])
Run a single task and return an allocated result. */
PHP_FUNCTION(gearman_client_do_normal) {
gearman_client_do_work_handler(gearman_client_do, INTERNAL_FUNCTION_PARAM_PASSTHRU);
}
/* }}} */
/* {{{ proto string GearmanClient::doHigh(object client, string function, string workload [, string unique ])
Run a high priority task and return an allocated result. */
PHP_FUNCTION(gearman_client_do_high) {
gearman_client_do_work_handler(gearman_client_do_high, INTERNAL_FUNCTION_PARAM_PASSTHRU);
}
/* }}} */
/* {{{ proto array GearmanClient::doLow(object client, string function, string workload [, string unique ])
Run a low priority task and return an allocated result. */
PHP_FUNCTION(gearman_client_do_low) {
gearman_client_do_work_handler(gearman_client_do_low, INTERNAL_FUNCTION_PARAM_PASSTHRU);
}
/* }}} */
/* {{{ proto object gearman_client_do_background_work_handler(void *add_task_func, object client, string function, zval workload [, string unique ])
Run a task in the background, high/normal/low dependent upon do_work_func */
static void gearman_client_do_background_work_handler(gearman_return_t (*do_background_work_func)(
gearman_client_st *client,
const char *function_name,
const char *unique,
const void *workload,
size_t workload_size,
gearman_job_handle_t job_handle
),
INTERNAL_FUNCTION_PARAMETERS) {
char *function_name;
size_t function_name_len;
char *workload;
size_t workload_len;
char *unique = NULL;
size_t unique_len = 0;
zend_string *job_handle;
gearman_client_obj *obj;
zval *zobj;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Oss|s!", &zobj, gearman_client_ce,
&function_name, &function_name_len,
&workload, &workload_len,
&unique, &unique_len) == FAILURE) {
RETURN_EMPTY_STRING();
}
obj = Z_GEARMAN_CLIENT_P(zobj);
job_handle = zend_string_alloc(GEARMAN_JOB_HANDLE_SIZE-1, 0);
obj->ret = (*do_background_work_func)(
&(obj->client),
(char *)function_name,
(char *)unique,
(void *)workload,
(size_t)workload_len,
job_handle->val
);
ZSTR_LEN(job_handle) = strnlen(ZSTR_VAL(job_handle), GEARMAN_JOB_HANDLE_SIZE-1);
if (! PHP_GEARMAN_CLIENT_RET_OK(obj->ret)) {
php_error_docref(NULL, E_WARNING, "%s",
gearman_client_error(&(obj->client)));
zend_string_release(job_handle);
RETURN_EMPTY_STRING();
}
if (! job_handle) {
zend_string_release(job_handle);
RETURN_EMPTY_STRING();
}
RETURN_STR(job_handle);
}
/* }}} */
/* {{{ proto string GearmanClient::doBackground(string function, string workload [, string unique ])
Run a task in the background. */
PHP_FUNCTION(gearman_client_do_background) {
gearman_client_do_background_work_handler(gearman_client_do_background, INTERNAL_FUNCTION_PARAM_PASSTHRU);
}
/* }}} */
/* {{{ proto string GearmanClient::doHighBackground(string function, string workload [, string unique ])
Run a high priority task in the background. */
PHP_FUNCTION(gearman_client_do_high_background) {
gearman_client_do_background_work_handler(gearman_client_do_high_background, INTERNAL_FUNCTION_PARAM_PASSTHRU);
}
/* }}} */
/* {{{ proto string GearmanClient::doLowBackground(string function, string workload [, string unique ])
Run a low priority task in the background. */
PHP_FUNCTION(gearman_client_do_low_background) {
gearman_client_do_background_work_handler(gearman_client_do_low_background, INTERNAL_FUNCTION_PARAM_PASSTHRU);
}
/* }}} */
/* {{{ proto string GearmanClient::doJobHandle()
Get the job handle for the running task. This should be used between repeated gearman_client_do_normal() and gearman_client_do_high() calls to get information. */
PHP_FUNCTION(gearman_client_do_job_handle) {
gearman_client_obj *obj;
zval *zobj;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_client_ce) == FAILURE) {
RETURN_EMPTY_STRING();
}
obj = Z_GEARMAN_CLIENT_P(zobj);
RETURN_STRING((char *)gearman_client_do_job_handle(&(obj->client)))
}
/* }}} */
/* {{{ proto array GearmanClient::doStatus()
Get the status for the running task. This should be used between repeated gearman_client_do() and gearman_client_do_high() calls to get information. */
PHP_FUNCTION(gearman_client_do_status) {
uint32_t numerator;
uint32_t denominator;
gearman_client_obj *obj;
zval *zobj;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_client_ce) == FAILURE) {
RETURN_EMPTY_STRING();
}
obj = Z_GEARMAN_CLIENT_P(zobj);
gearman_client_do_status(&(obj->client), &numerator, &denominator);
array_init(return_value);
add_next_index_long(return_value, (long) numerator);
add_next_index_long(return_value, (long) denominator);
}
/* }}} */
/* {{{ proto array GearmanClient::jobStatus(string job_handle)
Get the status for a backgound job. */
PHP_FUNCTION(gearman_client_job_status) {
char *job_handle;
size_t job_handle_len;
bool is_known;
bool is_running;
uint32_t numerator;
uint32_t denominator;
gearman_client_obj *obj;
zval *zobj;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &zobj, gearman_client_ce,
&job_handle, &job_handle_len) == FAILURE) {
RETURN_EMPTY_STRING();
}
obj = Z_GEARMAN_CLIENT_P(zobj);
obj->ret = gearman_client_job_status(&(obj->client), job_handle,
&is_known, &is_running,
&numerator, &denominator);
if (obj->ret != GEARMAN_SUCCESS && obj->ret != GEARMAN_IO_WAIT) {
php_error_docref(NULL, E_WARNING, "%s",
gearman_client_error(&(obj->client)));
}
array_init(return_value);
add_next_index_bool(return_value, is_known);
add_next_index_bool(return_value, is_running);
add_next_index_long(return_value, (long) numerator);
add_next_index_long(return_value, (long) denominator);
}
/* }}} */
/* {{{ proto array GearmanClient::jobStatusByUniqueKey(string unique_key)
Get the status for a backgound job using the unique key passed in during job submission, rather than job handle. */
PHP_FUNCTION(gearman_client_job_status_by_unique_key) {
char *unique_key;
size_t unique_key_len;
gearman_client_obj *obj;
zval *zobj;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &zobj, gearman_client_ce,
&unique_key, &unique_key_len) == FAILURE) {
RETURN_EMPTY_STRING();
}
obj = Z_GEARMAN_CLIENT_P(zobj);
gearman_status_t status = gearman_client_unique_status(&(obj->client), unique_key, unique_key_len);
gearman_return_t rc = gearman_status_return(status);
if (rc != GEARMAN_SUCCESS && rc != GEARMAN_IO_WAIT) {
php_error_docref(NULL, E_WARNING, "%s",
gearman_client_error(&(obj->client)));
}
array_init(return_value);
add_next_index_bool(return_value, gearman_status_is_known(status));
add_next_index_bool(return_value, gearman_status_is_running(status));
add_next_index_long(return_value, (long) gearman_status_numerator(status));
add_next_index_long(return_value, (long) gearman_status_denominator(status));
}
/* }}} */
/* {{{ proto bool GearmanClient::ping(string workload)
Send data to all job servers to see if they send it back. */
PHP_FUNCTION(gearman_client_ping) {
char *workload;
size_t workload_len;
gearman_client_obj *obj;
zval *zobj;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &zobj, gearman_client_ce, &workload, &workload_len) == FAILURE) {
RETURN_FALSE;
}
obj = Z_GEARMAN_CLIENT_P(zobj);
obj->ret = gearman_client_echo(&(obj->client), workload, (size_t)workload_len);
if (obj->ret != GEARMAN_SUCCESS && obj->ret != GEARMAN_IO_WAIT) {
php_error_docref(NULL, E_WARNING, "%s",
gearman_client_error(&(obj->client)));
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto object gearman_client_add_task_handler(void *add_task_func, object client, string function, zval workload [, string unique ])
Add a task to be run in parallel, background or not, high/normal/low dependent upon add_task_func. */
static void gearman_client_add_task_handler(gearman_task_st* (*add_task_func)(
gearman_client_st *client,
gearman_task_st *task,
void *context,
const char *function_name,
const char *unique,
const void *workload,
size_t workload_size,
gearman_return_t *ret_ptr),
INTERNAL_FUNCTION_PARAMETERS) {
zval *zworkload;
zval *zdata = NULL;
gearman_task_obj *task;
char *unique;
char *function_name;
size_t unique_len = 0;
size_t function_name_len = 0;
gearman_client_obj *obj;
zval *zobj;
// TODO - the documentation on php.net differs from this
// As found, this doesn't allow for user to pass in context.
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Osz|zs", &zobj, gearman_client_ce,
&function_name, &function_name_len,
&zworkload,
&zdata,
&unique, &unique_len
) == FAILURE) {
RETURN_FALSE;
}
obj = Z_GEARMAN_CLIENT_P(zobj);
if (unique_len == 0) {
unique = NULL;
}
if (Z_TYPE_P(zworkload) != IS_STRING) {
convert_to_string(zworkload);
}
/* get a task object, and prepare it for return */
if (object_init_ex(return_value, gearman_task_ce) != SUCCESS) {
php_error_docref(NULL, E_WARNING, "GearmanTask Object creation failure.");
RETURN_FALSE;
}
task = Z_GEARMAN_TASK_P(return_value);
if (zdata) {
ZVAL_COPY(&task->zdata, zdata);
}
ZVAL_COPY(&task->zworkload, zworkload);
/* need to store a ref to the client for later access to cb's */
ZVAL_COPY(&task->zclient, zobj);
/* add the task */
task->task = (*add_task_func)(
&(obj->client),
task->task,
(void *)task,
function_name,
unique,
Z_STRVAL_P(zworkload),
(size_t) Z_STRLEN_P(zworkload),
&obj->ret
);
if (obj->ret != GEARMAN_SUCCESS) {
php_error_docref(NULL, E_WARNING, "%s",
gearman_client_error(&(obj->client)));
RETURN_FALSE;
}
task->flags |= GEARMAN_TASK_OBJ_CREATED;
task->task_id = ++obj->created_tasks;
// prepend task to list of tasks on client obj
Z_ADDREF_P(return_value);
add_index_zval(&obj->task_list, task->task_id, return_value);
}
/* }}} */
/* {{{ proto object GearmanClient::addTask(string function, zval workload [, string unique ])
Add a task to be run in parallel. */
PHP_FUNCTION(gearman_client_add_task) {
gearman_client_add_task_handler(gearman_client_add_task, INTERNAL_FUNCTION_PARAM_PASSTHRU);
}
/* }}} */
/* {{{ proto object GearmanClient::addTaskHigh(string function, zval workload [, string unique ])
Add a high priority task to be run in parallel. */
PHP_FUNCTION(gearman_client_add_task_high) {
gearman_client_add_task_handler(gearman_client_add_task_high, INTERNAL_FUNCTION_PARAM_PASSTHRU);
}
/* }}} */
/* {{{ proto object GearmanClient::addTaskLow(string function, zval workload [, string unique ])
Add a low priority task to be run in parallel. */
PHP_FUNCTION(gearman_client_add_task_low) {
gearman_client_add_task_handler(gearman_client_add_task_low, INTERNAL_FUNCTION_PARAM_PASSTHRU);
}
/* }}} */
/* {{{ proto object GearmanClient::addTaskBackground(string function, zval workload [, string unique ])
Add a background task to be run in parallel. */
PHP_FUNCTION(gearman_client_add_task_background) {
gearman_client_add_task_handler(gearman_client_add_task_background, INTERNAL_FUNCTION_PARAM_PASSTHRU);
}
/* }}} */
/* {{{ proto object GearmanClient::addTaskHighBackground(string function, zval workload [, string unique ])
Add a high priority background task to be run in parallel. */
PHP_FUNCTION(gearman_client_add_task_high_background) {
gearman_client_add_task_handler(gearman_client_add_task_high_background, INTERNAL_FUNCTION_PARAM_PASSTHRU);
}
/* }}} */
/* {{{ proto object GearmanClient::addTaskLowBackground(string function, zval workload [, string unique ])
Add a low priority background task to be run in parallel. */
PHP_FUNCTION(gearman_client_add_task_low_background) {
gearman_client_add_task_handler(gearman_client_add_task_low_background, INTERNAL_FUNCTION_PARAM_PASSTHRU);
}
/* }}} */
/* {{{ proto bool gearman_client_run_tasks(object client)
Run tasks that have been added in parallel */
PHP_FUNCTION(gearman_client_run_tasks) {
gearman_client_obj *obj;
zval *zobj;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_client_ce) == FAILURE) {
RETURN_FALSE;
}
obj = Z_GEARMAN_CLIENT_P(zobj);
obj->ret = gearman_client_run_tasks(&(obj->client));
if (! PHP_GEARMAN_CLIENT_RET_OK(obj->ret)) {
php_error_docref(NULL, E_WARNING, "%s",
gearman_client_error(&(obj->client)));
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* this function is used to request status information from the gearmand
* server. it will then call your predefined status callback, passing
* zdata/context to it */
/* {{{ proto object gearman_client_add_task_status(object client, string job_handle [, zval data])
Add task to get the status for a backgound task in parallel. */
PHP_FUNCTION(gearman_client_add_task_status) {
zval *zdata = NULL;
char *job_handle;
size_t job_handle_len = 0;
gearman_client_obj *obj;
zval *zobj;
gearman_task_obj *task;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os|z", &zobj, gearman_client_ce,
&job_handle, &job_handle_len,
&zdata
) == FAILURE) {
RETURN_FALSE;
}
obj = Z_GEARMAN_CLIENT_P(zobj);
/* get a task object, and prepare it for return */
if (object_init_ex(return_value, gearman_task_ce) != SUCCESS) {
php_error_docref(NULL, E_WARNING, "GearmanTask Object creation failure.");
RETURN_FALSE;
}
task = Z_GEARMAN_TASK_P(return_value);
if (zdata) {
ZVAL_COPY(&task->zdata, zdata);
}
/* need to store a ref to the client for later access to cb's */
ZVAL_COPY(&task->zclient, zobj);
/* add the task */
task->task = gearman_client_add_task_status(&(obj->client),
task->task,
(void *)task,
job_handle,
&obj->ret
);
if (obj->ret != GEARMAN_SUCCESS) {
php_error_docref(NULL, E_WARNING, "%s",
gearman_client_error(&(obj->client)));
RETURN_FALSE;
}
task->flags |= GEARMAN_TASK_OBJ_CREATED;
// prepend task to list of tasks on client obj
Z_ADDREF_P(return_value);
add_next_index_zval(&obj->task_list, return_value);
}
/* }}} */
/* {{{ proto bool GearmanClient::setWorkloadCallback(callback function)
Callback function when workload data needs to be sent for a task. */
PHP_FUNCTION(gearman_client_set_workload_callback) {
zval *zworkload_fn;
zend_string *callable = NULL;
gearman_client_obj *obj;
zval *zobj;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Oz", &zobj, gearman_client_ce,
&zworkload_fn
) == FAILURE) {
RETURN_FALSE;
}
obj = Z_GEARMAN_CLIENT_P(zobj);
/* check that the function is callable */
if (! zend_is_callable(zworkload_fn, 0, &callable)) {
php_error_docref(NULL, E_WARNING, "function %s is not callable", callable->val);
zend_string_release(callable);
RETURN_FALSE;
}
zend_string_release(callable);
/* Defining callback again? Clean up old one first */
if (!Z_ISUNDEF(obj->zworkload_fn)) {
zval_dtor(&obj->zworkload_fn);
}
/* store the cb in client object */
ZVAL_COPY(&obj->zworkload_fn, zworkload_fn);
/* set the callback for php */
gearman_client_set_workload_fn(&(obj->client), _php_task_workload_fn);
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool GearmanClient::setCreatedCallback(callback function)
Callback function when workload data needs to be sent for a task. */
PHP_FUNCTION(gearman_client_set_created_callback) {
zval *zcreated_fn;
zend_string *callable = NULL;
gearman_client_obj *obj;
zval *zobj;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Oz", &zobj, gearman_client_ce,
&zcreated_fn
) == FAILURE) {
RETURN_FALSE;
}
obj = Z_GEARMAN_CLIENT_P(zobj);
/* check that the function is callable */
if (! zend_is_callable(zcreated_fn, 0, &callable)) {
php_error_docref(NULL, E_WARNING, "function %s is not callable", callable->val);
zend_string_release(callable);
RETURN_FALSE;
}
zend_string_release(callable);
/* Defining callback again? Clean up old one first */
if (!Z_ISUNDEF(obj->zcreated_fn)) {
zval_dtor(&obj->zcreated_fn);
}
/* store the cb in client object */
ZVAL_COPY(&obj->zcreated_fn, zcreated_fn);
/* set the callback for php */
gearman_client_set_created_fn(&(obj->client), _php_task_created_fn);
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool GearmanClient::setDataCallback(callback function)
Callback function when there is a data packet for a task. */
PHP_FUNCTION(gearman_client_set_data_callback) {
zval *zdata_fn;
zend_string *callable = NULL;
gearman_client_obj *obj;
zval *zobj;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Oz", &zobj, gearman_client_ce,
&zdata_fn
) == FAILURE) {
RETURN_FALSE;
}
obj = Z_GEARMAN_CLIENT_P(zobj);
/* check that the function is callable */
if (! zend_is_callable(zdata_fn, 0, &callable)) {
php_error_docref(NULL, E_WARNING, "function %s is not callable", callable->val);
zend_string_release(callable);
RETURN_FALSE;
}
zend_string_release(callable);