forked from barracudanetworks/forkdaemon-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfork_daemon.php
More file actions
1828 lines (1602 loc) · 51.6 KB
/
fork_daemon.php
File metadata and controls
1828 lines (1602 loc) · 51.6 KB
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
<?php
/**
* System process functions
* @category system
* @package fork_daemon
*/
class fork_daemon
{
/**
* Child process status constants
*
* @access public
*/
const WORKER = 0;
const HELPER = 1;
const STOPPED = 2;
/**
* Bucket constants
*
* @access public
*/
const DEFAULT_BUCKET = -1;
/**
* Logging constants
*
* @access public
*/
const LOG_LEVEL_ALL = -1;
const LOG_LEVEL_CRIT = 2;
const LOG_LEVEL_WARN = 4;
const LOG_LEVEL_INFO = 6;
const LOG_LEVEL_DEBUG = 7;
/**
* Socket constants
*
* @access public
*/
const SOCKET_HEADER_SIZE = 4;
/**
* Variables
*/
/**
* Maximum time in seconds a PID may execute
* @access private
* @var integer $child_max_run_time
*/
private $child_max_run_time = array(self::DEFAULT_BUCKET => 86400);
/**
* Function the child invokes with a set of worker units
* @access private
* @var integer $child_function_run
*/
private $child_function_run = array(self::DEFAULT_BUCKET => '');
/**
* Function the parent invokes when a child finishes
* @access private
* @var integer $parent_function_child_exited
*/
private $parent_function_child_exited = array(self::DEFAULT_BUCKET => '');
/**
* Function the child invokes when sigint/term is received
* @access private
* @var integer $child_function_exit
*/
private $child_function_exit = array(self::DEFAULT_BUCKET => '');
/**
* Function the parent invokes when a child is killed due to exceeding the max runtime
* @access private
* @var integer $child_function_timeout
*/
private $child_function_timeout = array(self::DEFAULT_BUCKET => '');
/**
* Function the parent invokes before forking a child
* @access private
* @var string[] $parent_function_prefork
*/
private $parent_function_prefork = '';
/**
* Function the parent invokes when a child is spawned
* @access private
* @var integer $parent_function_fork
*/
private $parent_function_fork = array(self::DEFAULT_BUCKET => '');
/**
* Function the parent invokes when the parent receives a SIGHUP
* @access private
* @var integer $parent_function_sighup
*/
private $parent_function_sighup = '';
/**
* Property of the parent sighup function. If true, the parent
* will send sighup to all children when the parent receives a
* sighup.
* @access private
* @var integer $parent_function_sighup_cascade
*/
private $parent_function_sighup_cascade = true;
/**
* Function the child invokes when the child receives a SIGHUP
* @access private
* @var integer $child_function_sighup
*/
private $child_function_sighup = array(self::DEFAULT_BUCKET => '');
/**
* Function the parent invokes when a child has results to post
* @access private
* @var integer $parent_function_results
*/
private $parent_function_results = array(self::DEFAULT_BUCKET => '');
/**
* Max number of seconds to wait for a child process
* exit once it has been requested to exit
* @access private
* @var integer $children_kill_timeout
*/
private $children_max_timeout = 30;
/**
* Function the parent runs when the daemon is getting shutdown
* @access private
* @var integer $parent_function_exit
*/
private $parent_function_exit = '';
/**
* Stores whether the daemon is in single item mode or not
* @access private
* @var bool $child_single_work_item
*/
private $child_single_work_item = array(self::DEFAULT_BUCKET => false);
/**
* Function to call when there is a message to log
* @access private
* @var array $log_function array of callables index by severity
* called with call_user_func($log_function, $message)
*/
private $log_function = null;
/**
* Stores whether or not we have received an exit request
* @access private
* @default false
* @var bool $exit_request_status
*/
private $exit_request_status = false;
/**************** SERVER CONTROLS ****************/
/**
* Upper limit on the number of children started.
* @access private
* @var integer $max_children
*/
private $max_children = array(self::DEFAULT_BUCKET => 25);
/**
* Upper limit on the number of work units sent to each child.
* @access private
* @var integer $max_work_per_child
*/
private $max_work_per_child = array(self::DEFAULT_BUCKET => 100);
/**
* Interval to do house keeping in seconds
* @access private
* @var integer $housekeeping_check_interval
*/
private $housekeeping_check_interval = 20;
/**************** TRACKING CONTROLS ****************/
/**
* track children of parent including their status and create time
* @access private
* @var array $forked_children
*/
private $forked_children = array();
/**
* number of tracked children (not stopped)
* @access private
* @var array $forked_children_count
*/
private $forked_children_count = 0;
/**
* track the work units to process
* @access private
* @var array $work_units
*/
private $work_units = array(self::DEFAULT_BUCKET => array());
/**
* track the buckets
* @access private
* @var array $buckets
*/
private $buckets = array(0 => self::DEFAULT_BUCKET);
/**
* for the parent the track the results received from chilren
* @access private
* @var array $work_units
*/
private $results = array(self::DEFAULT_BUCKET => array());
/**
* within a child, track the bucket the child exists in. note,
* this shouldn't be set or referenced in the parent process
* @access private
* @var int $child_bucket
*/
private $child_bucket = null;
/**************** MOST IMPORTANT CONTROLS ****************/
/**
* parent pid
* @access private
* @var array $parent_pid
*/
static private $parent_pid;
/**
* last housekeeping check time
* @access private
* @var array $housekeeping_last_check
*/
private $housekeeping_last_check = 0;
/**************** FUNCTION DEFINITIONS ****************/
/**
* Set and Get functions
*/
/**
* Allows the app to set the max_children value
* @access public
* @param int $value the new max_children value.
* @param int $bucket the bucket to use
*/
public function max_children_set($value, $bucket = self::DEFAULT_BUCKET)
{
if ($value < 1)
{
$value = 0;
$this->log(($bucket === self::DEFAULT_BUCKET ? 'default' : $bucket) . ' bucket max_children set to 0, bucket will be disabled', self::LOG_LEVEL_WARN);
}
$this->max_children[$bucket] = $value;
}
/**
* Allows the app to retrieve the current max_children value.
* @access public
* @param int $bucket the bucket to use
* @return int the max_children value
*/
public function max_children_get($bucket = self::DEFAULT_BUCKET)
{
return($this->max_children[$bucket]);
}
/**
* Allows the app to set the max_work_per_child value
* @access public
* @param int $value new max_work_per_child value.
* @param int $bucket the bucket to use
*/
public function max_work_per_child_set($value, $bucket = self::DEFAULT_BUCKET)
{
if ($this->child_single_work_item[$bucket])
{
$value = 1;
}
if ($value < 1)
{
$value = 0;
$this->log(($bucket === self::DEFAULT_BUCKET ? 'default' : $bucket) . ' bucket max_work_per_child set to 0, bucket will be disabled', self::LOG_LEVEL_WARN);
}
$this->max_work_per_child[$bucket] = $value;
}
/**
* Allows the app to retrieve the current max_work_per_child value.
* @access public
* @param int $bucket the bucket to use
* @return int the max_work_per_child value
*/
public function max_work_per_child_get($bucket = self::DEFAULT_BUCKET)
{
return($this->max_work_per_child[$bucket]);
}
/**
* Allows the app to set the child_max_run_time value
* @access public
* @param int $value new child_max_run_time value.
* @param int $bucket the bucket to use
*/
public function child_max_run_time_set($value, $bucket = self::DEFAULT_BUCKET)
{
if ($value < 1)
{
$value = 0;
$this->log(($bucket === self::DEFAULT_BUCKET ? 'default' : $bucket) . ' bucket child_max_run_time set to 0', self::LOG_LEVEL_WARN);
}
$this->child_max_run_time[$bucket] = $value;
}
/**
* Allows the app to retrieve the current child_max_run_time value.
* @access public
* @param int $bucket the bucket to use
* @return int the child_max_run_time value
*/
public function child_max_run_time_get($bucket = self::DEFAULT_BUCKET)
{
return($this->child_max_run_time[$bucket]);
}
/**
* Allows the app to set the child_single_work_item value
* @access public
* @param int $value new child_single_work_item value.
* @param int $bucket the bucket to use
*/
public function child_single_work_item_set($value, $bucket = self::DEFAULT_BUCKET)
{
if ($value < 1)
{
$value = 0;
$this->log(($bucket === self::DEFAULT_BUCKET ? 'default' : $bucket) . ' bucket child_single_work_item set to 0', self::LOG_LEVEL_WARN);
}
$this->child_single_work_item[$bucket] = $value;
}
/**
* Allows the app to retrieve the current child_single_work_item value.
* @access public
* @param int $bucket the bucket to use
* @return int the child_single_work_item value
*/
public function child_single_work_item_get($bucket = self::DEFAULT_BUCKET)
{
return($this->child_single_work_item[$bucket]);
}
/**
* Allows the app to retrieve the current child_bucket value.
* @access public
* @return int the child_bucket value representing the bucket number of the child
*/
public function child_bucket_get()
{
// this function does not apply to the parent
if (self::$parent_pid == getmypid()) return false;
return($this->child_bucket);
}
/**
* Creates a new bucket to house forking operations
* @access public
* @param int $bucket the bucket to create
*/
public function add_bucket($bucket)
{
/* create the bucket by copying values from the default bucket */
$this->max_children[$bucket] = $this->max_children[self::DEFAULT_BUCKET];
$this->child_single_work_item[$bucket] = $this->child_single_work_item[self::DEFAULT_BUCKET];
$this->max_work_per_child[$bucket] = $this->max_work_per_child[self::DEFAULT_BUCKET];
$this->child_max_run_time[$bucket] = $this->child_max_run_time[self::DEFAULT_BUCKET];
$this->child_single_work_item[$bucket] = $this->child_single_work_item[self::DEFAULT_BUCKET];
$this->child_function_run[$bucket] = $this->child_function_run[self::DEFAULT_BUCKET];
$this->parent_function_fork[$bucket] = $this->parent_function_fork[self::DEFAULT_BUCKET];
$this->child_function_sighup[$bucket] = $this->child_function_sighup[self::DEFAULT_BUCKET];
$this->child_function_exit[$bucket] = $this->child_function_exit[self::DEFAULT_BUCKET];
$this->child_function_timeout[$bucket] = $this->child_function_timeout[self::DEFAULT_BUCKET];
$this->parent_function_child_exited[$bucket] = $this->parent_function_child_exited[self::DEFAULT_BUCKET];
$this->work_units[$bucket] = array();
$this->buckets[$bucket] = $bucket;
$this->results[$bucket] = array();
}
/**
* Allows the app to set the call back function for child processes
* @access public
* @param string name of function to be called.
* @param int $bucket the bucket to use
* @return bool true if the callback was successfully registered, false if it failed
*/
public function register_child_run($function_name, $bucket = self::DEFAULT_BUCKET)
{
/* call child function */
if ( ( is_array($function_name) && method_exists($function_name[0], $function_name[1]) ) || method_exists($this, $function_name) || function_exists($function_name) )
{
$this->child_function_run[$bucket] = $function_name;
return true;
}
return false;
}
/**
* Allows the app to set call back functions to cleanup resources before forking
* @access public
* @param array names of functions to be called.
* @return bool true if the callback was successfully registered, false if it failed
*/
public function register_parent_prefork(array $function_names)
{
$this->parent_function_prefork = $function_names;
return true;
}
/**
* Allows the app to set the call back function for when a child process is spawned
* @access public
* @param string name of function to be called.
* @param int $bucket the bucket to use
* @return bool true if the callback was successfully registered, false if it failed
*/
public function register_parent_fork($function_name, $bucket = self::DEFAULT_BUCKET)
{
/* call child function */
if ( ( is_array($function_name) && method_exists($function_name[0], $function_name[1]) ) || method_exists($this, $function_name) || function_exists($function_name) )
{
$this->parent_function_fork[$bucket] = $function_name;
return true;
}
return false;
}
/**
* Allows the app to set the call back function for when a parent process receives a SIGHUP
* @access public
* @param string name of function to be called.
* @param bool $cascade_signal if true, the parent will send a sighup to all of it's children
* @param int $bucket the bucket to use
* @return bool true if the callback was successfully registered, false if it failed
*/
public function register_parent_sighup($function_name, $cascade_signal = true)
{
/* call child function */
if ( ( is_array($function_name) && method_exists($function_name[0], $function_name[1]) ) || method_exists($this, $function_name) || function_exists($function_name) )
{
$this->parent_function_sighup = $function_name;
$this->parent_function_sighup_cascade = $cascade_signal;
return true;
}
return false;
}
/**
* Allows the app to set the call back function for when a child process receives a SIGHUP
* @access public
* @param string name of function to be called.
* @param int $bucket the bucket to use
* @return bool true if the callback was successfully registered, false if it failed
*/
public function register_child_sighup($function_name, $bucket = self::DEFAULT_BUCKET)
{
/* call child function */
if ( ( is_array($function_name) && method_exists($function_name[0], $function_name[1]) ) || method_exists($this, $function_name) || function_exists($function_name) )
{
$this->child_function_sighup[$bucket] = $function_name;
return true;
}
return false;
}
/**
* Allows the app to set the call back function for when a child process exits
* @access public
* @param string name of function to be called.
* @param int $bucket the bucket to use
* @return bool true if the callback was successfully registered, false if it failed
*/
public function register_child_exit($function_name, $bucket = self::DEFAULT_BUCKET)
{
/* call child function */
if ( ( is_array($function_name) && method_exists($function_name[0], $function_name[1]) ) || method_exists($this, $function_name) || function_exists($function_name) )
{
$this->child_function_exit[$bucket] = $function_name;
return true;
}
return false;
}
/**
* Allows the app to set the call back function for when a child process is killed for exceeding its max runtime
* @access public
* @param string name of function to be called.
* @param int $bucket the bucket to use
* @return bool true if the callback was successfully registered, false if it failed
*/
public function register_child_timeout($function_name, $bucket = self::DEFAULT_BUCKET)
{
/* call child function */
if ( ( is_array($function_name) && method_exists($function_name[0], $function_name[1]) ) || method_exists($this, $function_name) || function_exists($function_name) )
{
$this->child_function_timeout[$bucket] = $function_name;
return true;
}
return false;
}
/**
* Allows the app to set the call back function for when the parent process exits
* @access public
* @param string name of function to be called.
* @return bool true if the callback was successfully registered, false if it failed
*/
public function register_parent_exit($function_name)
{
// call parent function
if ( ( is_array($function_name) && method_exists($function_name[0], $function_name[1]) ) || method_exists($this, $function_name) || function_exists($function_name) )
{
$this->parent_function_exit = $function_name;
return true;
}
return false;
}
/**
* Allows the app to set the call back function for when a child exits in the parent
* @access public
* @param string name of function to be called.
* @param int $bucket the bucket to use
* @return bool true if the callback was successfully registered, false if it failed
*/
public function register_parent_child_exit($function_name, $bucket = self::DEFAULT_BUCKET)
{
/* call parent function */
if ( ( is_array($function_name) && method_exists($function_name[0], $function_name[1]) ) || method_exists($this, $function_name) || function_exists($function_name) )
{
$this->parent_function_child_exited[$bucket] = $function_name;
return true;
}
return false;
}
/**
* Allows the app to set the call back function for when the a child has results
* @access public
* @param string name of function to be called.
* @return bool true if the callback was successfully registered, false if it failed
*/
public function register_parent_results($function_name, $bucket = self::DEFAULT_BUCKET)
{
// call parent function
if ( ( is_array($function_name) && method_exists($function_name[0], $function_name[1]) ) || method_exists($this, $function_name) || function_exists($function_name) )
{
$this->parent_function_results[$bucket] = $function_name;
return true;
}
return false;
}
/**
* Allows the app to set the call back function for logging
* @access public
* @param callable name of function to be called.
* @param int $severity the severity level
* @return bool true if the callback was successfully registered, false if it failed
*/
public function register_logging($function_name, $severity)
{
/* call parent function */
if ( ( is_array($function_name) && method_exists($function_name[0], $function_name[1]) ) || method_exists($this, $function_name) || function_exists($function_name) )
{
$this->log_function[$severity] = $function_name;
return true;
}
return false;
}
/************ NORMAL FUNCTION DEFS ************/
/**
* This is the class constructor, initializes the object.
* @access public
*/
public function __construct()
{
/* record pid of parent process */
self::$parent_pid = getmypid();
/* install signal handlers */
declare(ticks = 1);
pcntl_signal(SIGHUP, array(&$this, 'signal_handler_sighup'));
pcntl_signal(SIGCHLD, array(&$this, 'signal_handler_sigchild'));
pcntl_signal(SIGTERM, array(&$this, 'signal_handler_sigint'));
pcntl_signal(SIGINT, array(&$this, 'signal_handler_sigint'));
pcntl_signal(SIGALRM, SIG_IGN);
pcntl_signal(SIGUSR2, SIG_IGN);
pcntl_signal(SIGBUS, SIG_IGN);
pcntl_signal(SIGPIPE, SIG_IGN);
pcntl_signal(SIGABRT, SIG_IGN);
pcntl_signal(SIGFPE, SIG_IGN);
pcntl_signal(SIGILL, SIG_IGN);
pcntl_signal(SIGQUIT, SIG_IGN);
pcntl_signal(SIGTRAP, SIG_IGN);
pcntl_signal(SIGSYS, SIG_IGN);
/* add barracuda specific prefork functions (doesn't hurt anything) */
$this->parent_function_prefork = array('db_clear_connection_cache', 'memcache_clear_connection_cache');
}
/**
* Destructor does not do anything.
* @access public
*/
public function __destruct()
{
}
/**
* Handle both parent and child registered sighup callbacks.
*
* @param int $signal_number is the signal that called this function. (should be '1' for SIGHUP)
* @access public
*/
public function signal_handler_sighup($signal_number)
{
if (self::$parent_pid == getmypid())
{
// parent received sighup
$this->log('parent process [' . getmypid() . '] received sighup', self::LOG_LEVEL_DEBUG);
// call parent's sighup registered callback
$this->invoke_callback($this->parent_function_sighup, $parameters = array(), true);
// if cascading, send sighup to all child processes
if ($this->parent_function_sighup_cascade === true)
{
foreach ($this->forked_children as $pid => $pid_info)
{
if ($pid_info['status'] == self::STOPPED)
continue;
$this->log('parent process [' . getmypid() . '] sending sighup to child ' . $pid, self::LOG_LEVEL_DEBUG);
posix_kill($pid, SIGHUP);
}
}
}
else
{
// child received sighup. note a child is only in one bucket, do not loop through all buckets
$this->log('child process [' . getmypid() . '] received sighup with bucket type [' . $this->child_bucket . ']', self::LOG_LEVEL_DEBUG);
$this->invoke_callback(
$this->child_function_sighup[$this->child_bucket],
array($this->child_bucket),
true
);
}
}
/**
* Handle parent registered sigchild callbacks.
*
* @param int $signal_number is the signal that called this function.
* @access public
*/
public function signal_handler_sigchild($signal_number)
{
// do not allow signals to interrupt this
declare(ticks = 0)
{
// reap all child zombie processes
if (self::$parent_pid == getmypid())
{
$status = '';
do
{
// get child pid that exited
$child_pid = pcntl_waitpid(0, $status, WNOHANG);
if ($child_pid > 0)
{
// child exited
$identifier = false;
if (!isset($this->forked_children[$child_pid]))
{
die("Cannot find $child_pid in array!\n");
}
$child = $this->forked_children[$child_pid];
$identifier = $child['identifier'];
// call exit function if and only if its declared */
if ($child['status'] == self::WORKER)
$this->invoke_callback($this->parent_function_child_exited[ $this->forked_children[$child_pid]['bucket'] ], array($child_pid, $this->forked_children[$child_pid]['identifier']), true);
// stop the child pid
$this->forked_children[$child_pid]['status'] = self::STOPPED;
$this->forked_children_count--;
// respawn helper processes
if ($child['status'] == self::HELPER && $child['respawn'] === true)
{
$this->log('Helper process ' . $child_pid . ' died, respawning', self::LOG_LEVEL_INFO);
$this->helper_process_spawn($child['function'], $child['arguments'], $child['identifier'], true);
}
// Poll for results from any children
$this->post_results($child['bucket']);
}
elseif ($child_pid < 0)
{
// ignore acceptable error 'No child processes' given we force this signal to run potentially when no children exist
if (pcntl_get_last_error() == 10) continue;
// pcntl_wait got an error
$this->log('pcntl_waitpid failed with error ' . pcntl_get_last_error() . ':' . pcntl_strerror((pcntl_get_last_error())), self::LOG_LEVEL_DEBUG);
}
}
while ($child_pid > 0);
}
}
}
/**
* Handle both parent and child registered sigint callbacks
*
* User terminated by CTRL-C (detected only by the parent)
*
* @param int $signal_number is the signal that called this function
* @access public
*/
public function signal_handler_sigint($signal_number)
{
// log that we received an exit request
$this->received_exit_request(true);
// kill child processes
if (self::$parent_pid == getmypid())
{
foreach ($this->forked_children as $pid => &$pid_info)
{
if ($pid_info['status'] == self::STOPPED)
continue;
// tell helpers not to respawn
if ($pid_info['status'] == self::HELPER)
$pid_info['respawn'] = false;
$this->log('requesting child exit for pid: ' . $pid, self::LOG_LEVEL_INFO);
posix_kill($pid, SIGINT);
}
sleep(1);
// checking for missed sigchild
$this->signal_handler_sigchild(SIGCHLD);
$start_time = time();
// wait for child processes to go away
while ($this->forked_children_count > 0)
{
if (time() > ($start_time + $this->children_max_timeout))
{
foreach ($this->forked_children as $pid => $child)
{
if ($child['status'] == self::STOPPED)
continue;
$this->log('force killing child pid: ' . $pid, self::LOG_LEVEL_INFO);
posix_kill($pid, SIGKILL);
// stop the child
$this->forked_children[$pid]['status'] = self::STOPPED;
$this->forked_children_count--;
}
}
else
{
$this->log('waiting ' . ($start_time + $this->children_max_timeout - time()) . ' seconds for ' . $this->forked_children_count . ' children to clean up', self::LOG_LEVEL_INFO);
sleep(1);
$this->housekeeping_check();
}
}
// make call back to parent exit function if it exists
$this->invoke_callback($this->parent_function_exit, $parameters = array(self::$parent_pid), true);
}
else
{
// invoke child cleanup callback
if (isset($this->child_bucket))
$this->invoke_callback($this->child_function_exit[$this->child_bucket], $parameters = array($this->child_bucket), true);
}
exit(-1);
}
/**
* Check or set if we have recieved an exit request
*
* @param boolen $requested (optional) have we received the request
* @return current exit request status
*/
public function received_exit_request($requested = null)
{
// if we are retreiving the value of the exit request
if ($requested === null)
{
return $this->exit_request_status;
}
// ensure we have good data, or set to false if not
if (! is_bool($requested))
{
$requested = false;
}
// set and return the ne value
return ($this->exit_request_status = $requested);
}
/**
* Add work to the group of work to be processed
*
* @param mixed array of items to be handed back to child in chunks
* @param string a unique identifier for this work
* @param int $bucket the bucket to use
* @param bool $sort_queue true to sort the work unit queue
*/
public function addwork($new_work_units, $identifier = '', $bucket = self::DEFAULT_BUCKET, $sort_queue = false)
{
// ensure bucket is setup before we try to add data to it
if (! array_key_exists($bucket, $this->work_units))
$this->add_bucket($bucket);
// add to queue to send
if ($this->child_single_work_item[$bucket])
{
// prepend identifier with 'id-' because array_splice() re-arranges numeric keys
$this->work_units[$bucket]['id-' . $identifier] = $new_work_units;
}
elseif ($new_work_units === null || sizeof($new_work_units) === 0)
{
// no work
}
else
{
// merge in the new work units
$this->work_units[$bucket] = array_merge($this->work_units[$bucket], $new_work_units);
}
// sort the queue
if ($sort_queue)
ksort($this->work_units[$bucket]);
return;
}
/*
* Based on identifier and bucket is a child working on the work
*
* @param string unique identifier for the work
* @param int $bucket the bucket
* @return bool true if child has work, false if not
*/
public function is_work_running($identifier, $bucket = self::DEFAULT_BUCKET)
{
foreach ($this->forked_children as $info)
{
if (($info['status'] != self::STOPPED) && ($info['identifier'] == $identifier) && ($info['bucket'] == $bucket))
{
return true;
}
}
return false;
}
/*
* Return array of currently running children
*
* @param int $bucket the bucket
* @return bool true if child has work, false if not
*/
public function work_running($bucket = self::DEFAULT_BUCKET)
{
$results = array();
foreach ($this->forked_children as $pid => $child)
{
if ($child['status'] != self::STOPPED)
$results[$pid] = $child;
}
return $results;
}
/**
* Return a list of the buckets which have been created
*
* @param bool $include_default_bucket optionally include self::DEFAULT_BUCKET in returned value (DEFAULT: true)
* @return array list of buckets
*/
public function bucket_list($include_default_bucket = true)
{
$bucket_list = array();
foreach($this->buckets as $bucket_id)
{
// skip the default bucket if ignored
if ( ($include_default_bucket === false) && ($bucket_id === self::DEFAULT_BUCKET) )
continue;
$bucket_list[] = $bucket_id;
}
return $bucket_list;
}
/**
* Check to see if a bucket exists
*
* @return bool true if the bucket exists, false if it does not
*/
public function bucket_exists($bucket_id)
{
return (array_key_exists($bucket_id, $this->buckets));
}
/**
* Return the number of work sets queued
*
* A work set is a chunk of items to be worked on. A whole work set
* is handed off to a child processes. This size of the work sets can
* be controlled by $this->max_work_per_child_set()
*
* @param int $bucket the bucket to use
* @param bool $process_all_buckets if set to true, return the count of all buckets
* @return int the number of work sets queued
*/
public function work_sets_count($bucket = self::DEFAULT_BUCKET, $process_all_buckets = false)
{
// if asked to process all buckets, count all of them and return the count
if ($process_all_buckets === true)
{
$count = 0;
foreach($this->buckets as $bucket_slot)
{
$count += count($this->work_units[$bucket_slot]);
}
return $count;
}
return count($this->work_units[$bucket]);
}
/**
* Return the contents of work sets queued
*
* A work set is a chunk of items to be worked on. A whole work set
* is handed off to a child processes. This size of the work sets can
* be controlled by $this->max_work_per_child_set()
*
* @param int $bucket the bucket to use
* @return array contents of the bucket
*/
public function work_sets($bucket = self::DEFAULT_BUCKET)
{
return $this->work_units[$bucket];
}