Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 848 lines (680 sloc) 23.838 kb
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
1 <?php
2
3 /**
4
5 PHP script for managing PHP based Gearman workers
6
7 Copyright (c) 2010, Brian Moon
8 All rights reserved.
9
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are met:
12
13 * Redistributions of source code must retain the above copyright notice,
14 this list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright
16 notice, this list of conditions and the following disclaimer in the
17 documentation and/or other materials provided with the distribution.
18 * Neither the name of Brian Moon nor the names of its contributors may be
19 used to endorse or promote products derived from this software without
20 specific prior written permission.
21
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 POSSIBILITY OF SUCH DAMAGE.
33
34 **/
35
36 declare(ticks = 1);
37
38 /**
39 * Class that handles all the process management
40 */
41 class GearmanManager {
42
43 /**
44 * Log levels can be enabled from the command line with -v, -vv, -vvv
45 */
af9fa744 » brian
2010-03-14 Change logging output to have more information. Change how functions …
46 const LOG_LEVEL_INFO = 1;
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
47 const LOG_LEVEL_PROC_INFO = 2;
48 const LOG_LEVEL_WORKER_INFO = 3;
af9fa744 » brian
2010-03-14 Change logging output to have more information. Change how functions …
49 const LOG_LEVEL_DEBUG = 4;
c851e2d7 » brian
2010-04-29 Added support for PEAR Net_Gearman. Enhanced logging of results and w…
50 const LOG_LEVEL_CRAZY = 5;
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
51
52 /**
53 * Holds the worker configuration
54 */
c851e2d7 » brian
2010-04-29 Added support for PEAR Net_Gearman. Enhanced logging of results and w…
55 protected $config = array();
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
56
57 /**
58 * Boolean value that determines if the running code is the parent or a child
59 */
43e03634 » brian
2010-05-26 change ischild to isparent so it is more accurate when daemonizing
60 protected $isparent = true;
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
61
62 /**
63 * When true, workers will stop look for jobs and the parent process will
64 * kill off all running children
65 */
c851e2d7 » brian
2010-04-29 Added support for PEAR Net_Gearman. Enhanced logging of results and w…
66 protected $stop_work = false;
67
68 /**
69 * The timestamp when the signal was received to stop working
70 */
71 protected $stop_time = 0;
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
72
73 /**
74 * Holds the resource for the log file
75 */
c851e2d7 » brian
2010-04-29 Added support for PEAR Net_Gearman. Enhanced logging of results and w…
76 protected $log_file_handle;
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
77
78 /**
3d0d6933 » hjr3
2010-05-06 Added syslog support.
79 * Flag for logging to syslog
80 */
81 protected $log_syslog = false;
82
83 /**
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
84 * Verbosity level for the running script. Set via -v option
85 */
c851e2d7 » brian
2010-04-29 Added support for PEAR Net_Gearman. Enhanced logging of results and w…
86 protected $verbose = 0;
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
87
88 /**
89 * The array of running child processes
90 */
c851e2d7 » brian
2010-04-29 Added support for PEAR Net_Gearman. Enhanced logging of results and w…
91 protected $children = array();
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
92
93 /**
94 * The array of jobs that have workers running
95 */
c851e2d7 » brian
2010-04-29 Added support for PEAR Net_Gearman. Enhanced logging of results and w…
96 protected $jobs = array();
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
97
98 /**
99 * The PID of the running process. Set for parent and child processes
100 */
c851e2d7 » brian
2010-04-29 Added support for PEAR Net_Gearman. Enhanced logging of results and w…
101 protected $pid = 0;
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
102
103 /**
104 * PID file for the parent process
105 */
c851e2d7 » brian
2010-04-29 Added support for PEAR Net_Gearman. Enhanced logging of results and w…
106 protected $pid_file = "";
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
107
108 /**
8d30d46f » David Shafik
2010-12-01 * Add -p to allow prefixing class/function name
109 * Class/Function Prefix
110 */
111 protected $prefix = "";
112
113 /**
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
114 * PID of helper child
115 */
c851e2d7 » brian
2010-04-29 Added support for PEAR Net_Gearman. Enhanced logging of results and w…
116 protected $helper_pid = 0;
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
117
118 /**
bcd8c326 » David Shafik
2010-10-26 * Add ignore worker flag (-i)
119 * Restart worker after each job
120 */
121 protected $restart_each = false;
122
123 /**
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
124 * If true, the worker code directory is checked for updates and workers
125 * are restarted automatically.
126 */
c851e2d7 » brian
2010-04-29 Added support for PEAR Net_Gearman. Enhanced logging of results and w…
127 protected $check_code = false;
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
128
129 /**
130 * Holds the last timestamp of when the code was checked for updates
131 */
c851e2d7 » brian
2010-04-29 Added support for PEAR Net_Gearman. Enhanced logging of results and w…
132 protected $last_check_time = 0;
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
133
134 /**
135 * When forking helper children, the parent waits for a signal from them
136 * to continue doing anything
137 */
c851e2d7 » brian
2010-04-29 Added support for PEAR Net_Gearman. Enhanced logging of results and w…
138 protected $wait_for_signal = false;
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
139
140 /**
8a3f16da » brian
2010-03-15 Moved all non-worker related config from the config file to command l…
141 * Directory where worker functions are found
142 */
c851e2d7 » brian
2010-04-29 Added support for PEAR Net_Gearman. Enhanced logging of results and w…
143 protected $worker_dir = "";
8a3f16da » brian
2010-03-15 Moved all non-worker related config from the config file to command l…
144
145 /**
bcd8c326 » David Shafik
2010-10-26 * Add ignore worker flag (-i)
146 * Worker functions to ignore
147 */
148 protected $ignore_workers = array();
149
150 /**
8a3f16da » brian
2010-03-15 Moved all non-worker related config from the config file to command l…
151 * Number of workers that do all jobs
152 */
c851e2d7 » brian
2010-04-29 Added support for PEAR Net_Gearman. Enhanced logging of results and w…
153 protected $do_all_count = 0;
8a3f16da » brian
2010-03-15 Moved all non-worker related config from the config file to command l…
154
155 /**
156 * Maximum time a worker will run
157 */
c851e2d7 » brian
2010-04-29 Added support for PEAR Net_Gearman. Enhanced logging of results and w…
158 protected $max_run_time = 3600;
8a3f16da » brian
2010-03-15 Moved all non-worker related config from the config file to command l…
159
160 /**
161 * Servers that workers connect to
162 */
c851e2d7 » brian
2010-04-29 Added support for PEAR Net_Gearman. Enhanced logging of results and w…
163 protected $servers = array();
8a3f16da » brian
2010-03-15 Moved all non-worker related config from the config file to command l…
164
165 /**
166 * List of functions available for work
167 */
c851e2d7 » brian
2010-04-29 Added support for PEAR Net_Gearman. Enhanced logging of results and w…
168 protected $functions = array();
8a3f16da » brian
2010-03-15 Moved all non-worker related config from the config file to command l…
169
170 /**
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
171 * Creates the manager and gets things going
172 *
173 */
174 public function __construct() {
175
176 if(!function_exists("posix_kill")){
177 $this->show_help("The function posix_kill was not found. Please ensure POSIX functions are installed");
178 }
179
180 if(!function_exists("pcntl_fork")){
181 $this->show_help("The function pcntl_fork was not found. Please ensure Process Control functions are installed");
182 }
183
184 $this->pid = getmypid();
185
186 /**
c851e2d7 » brian
2010-04-29 Added support for PEAR Net_Gearman. Enhanced logging of results and w…
187 * Parse command line options. Loads the config file as well
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
188 */
c851e2d7 » brian
2010-04-29 Added support for PEAR Net_Gearman. Enhanced logging of results and w…
189 $this->getopt();
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
190
191 /**
c851e2d7 » brian
2010-04-29 Added support for PEAR Net_Gearman. Enhanced logging of results and w…
192 * Register signal listeners
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
193 */
c851e2d7 » brian
2010-04-29 Added support for PEAR Net_Gearman. Enhanced logging of results and w…
194 $this->register_ticks();
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
195
8a3f16da » brian
2010-03-15 Moved all non-worker related config from the config file to command l…
196 /**
197 * Load up the workers
198 */
199 $worker_files = glob($this->worker_dir."/*.php");
200
201 if(empty($worker_files)){
202 $this->log("No workers found");
203 posix_kill($this->pid, SIGUSR1);
204 exit();
205 }
206
207 foreach($worker_files as $file){
208 $function = substr(basename($file), 0, -4);
0168190c » brian
2010-05-25 Allow for count=0 in the ini to skip workers from starting
209 if(!isset($this->functions[$function])){
8a3f16da » brian
2010-03-15 Moved all non-worker related config from the config file to command l…
210 $this->functions[$function] = array("count"=>1);
211 }
212 }
213
214
215 /**
216 * Validate workers in the helper process
217 */
218 $this->fork_me("validate_workers");
219
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
220 $this->log("Started with pid $this->pid", GearmanManager::LOG_LEVEL_PROC_INFO);
221
222 /**
223 * Start the initial workers and set up a running environment
224 */
225 $this->bootstrap();
226
227
228 /**
229 * Main processing loop for the parent process
230 */
231 while(!$this->stop_work || count($this->children)) {
232
8a3f16da » brian
2010-03-15 Moved all non-worker related config from the config file to command l…
233 $status = null;
234
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
235 /**
236 * Check for exited children
237 */
238 $exited = pcntl_wait( $status, WNOHANG );
239
240 /**
241 * We run other children, make sure this is a worker
242 */
243 if(isset($this->children[$exited])){
244 /**
245 * If they have exited, remove them from the children array
246 * If we are not stopping work, start another in its place
247 */
248 if($exited) {
249 $worker = $this->children[$exited];
250 unset($this->children[$exited]);
251 $this->log("Child $exited exited ($worker)", GearmanManager::LOG_LEVEL_PROC_INFO);
252 if(!$this->stop_work){
253 $this->start_worker($worker);
254 }
255 }
256 }
257
258
c851e2d7 » brian
2010-04-29 Added support for PEAR Net_Gearman. Enhanced logging of results and w…
259 if($this->stop_work && time() - $this->stop_time > 60){
260 $this->log("Children have not exited, killing.", GearmanManager::LOG_LEVEL_PROC_INFO);
261 $this->stop_children(SIGKILL);
262 }
263
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
264 /**
265 * php will eat up your cpu if you don't have this
266 */
267 usleep(50000);
268
269 }
270
271 /**
272 * Kill the helper if it is running
273 */
274 if(isset($this->helper_pid)){
275 posix_kill($this->helper_pid, SIGKILL);
276 }
277
278 $this->log("Exiting");
279
280 }
281
282
283 /**
284 * Handles anything we need to do when we are shutting down
285 *
286 */
287 public function __destruct() {
43e03634 » brian
2010-05-26 change ischild to isparent so it is more accurate when daemonizing
288 if($this->isparent){
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
289 if(!empty($this->pid_file) && file_exists($this->pid_file)){
290 unlink($this->pid_file);
291 }
292 }
293 }
294
295 /**
296 * Parses the command line options
297 *
298 */
c851e2d7 » brian
2010-04-29 Added support for PEAR Net_Gearman. Enhanced logging of results and w…
299 protected function getopt() {
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
300
8d30d46f » David Shafik
2010-12-01 * Add -p to allow prefixing class/function name
301 $opts = getopt("ac:dD:h:Hi:l:o:P:p:ru:v::w:x:");
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
302
8a3f16da » brian
2010-03-15 Moved all non-worker related config from the config file to command l…
303 if(isset($opts["H"])){
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
304 $this->show_help();
305 }
306
307 /**
308 * If we want to daemonize, fork here and exit
309 */
310 if(isset($opts["d"])){
311 $pid = pcntl_fork();
312 if($pid>0){
43e03634 » brian
2010-05-26 change ischild to isparent so it is more accurate when daemonizing
313 $this->isparent = false;
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
314 exit();
315 }
8d30d46f » David Shafik
2010-12-01 * Add -p to allow prefixing class/function name
316 posix_setsid();
ead419fe » hjr3
2010-05-06 Get new pid after daemonizing.
317 $this->pid = getmypid();
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
318 }
319
bcd8c326 » David Shafik
2010-10-26 * Add ignore worker flag (-i)
320 if (isset($opts['r'])) {
321 $this->restart_each = true;
322 }
323
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
324 if(isset($opts["P"])){
325 $fp = @fopen($opts["P"], "w");
326 if($fp){
327 fwrite($fp, $this->pid);
328 fclose($fp);
329 } else {
330 $this->show_help("Unable to write PID to $opts[P]");
331 }
332 $this->pid_file = $opts["P"];
333 }
334
bcd8c326 » David Shafik
2010-10-26 * Add ignore worker flag (-i)
335 if(isset($opts['u'])) {
336 $user = posix_getpwnam($opts['u']);
337 if (!$user || !isset($user['uid'])) {
338 $this->show_help("User ({$opts['u']}) not found.");
339 }
340
341 posix_setuid($user['uid']);
342 if (posix_geteuid() != $user['uid']) {
343 $this->show_help("Unable to change user to {$opts['u']} (UID: {$user['uid']}).");
344 }
345 }
346
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
347 if(isset($opts["v"])){
348 switch($opts["v"]){
349 case false:
af9fa744 » brian
2010-03-14 Change logging output to have more information. Change how functions …
350 $this->verbose = GearmanManager::LOG_LEVEL_INFO;
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
351 break;
352 case "v":
353 $this->verbose = GearmanManager::LOG_LEVEL_PROC_INFO;
354 break;
355 case "vv":
356 $this->verbose = GearmanManager::LOG_LEVEL_WORKER_INFO;
af9fa744 » brian
2010-03-14 Change logging output to have more information. Change how functions …
357 break;
358 case "vvv":
359 $this->verbose = GearmanManager::LOG_LEVEL_DEBUG;
360 break;
c851e2d7 » brian
2010-04-29 Added support for PEAR Net_Gearman. Enhanced logging of results and w…
361 default:
362 case "vvvv":
363 $this->verbose = GearmanManager::LOG_LEVEL_CRAZY;
364 break;
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
365 }
366 }
367
368 if(isset($opts["l"])){
3d0d6933 » hjr3
2010-05-06 Added syslog support.
369 if($opts["l"] === 'syslog'){
370 $this->log_syslog = true;
371 } else {
372 $this->log_file_handle = @fopen($opts["l"], "a");
373 if(!$this->log_file_handle){
374 $this->show_help("Could not open log file $opts[l]");
375 }
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
376 }
377 }
378
8a3f16da » brian
2010-03-15 Moved all non-worker related config from the config file to command l…
379 if(isset($opts["c"]) && !file_exists($opts["c"])){
380 $this->show_help("Config file $opts[c] not found.");
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
381 }
382
383 if(isset($opts["a"])){
384 $this->check_code = true;
385 }
386
8a3f16da » brian
2010-03-15 Moved all non-worker related config from the config file to command l…
387 if(isset($opts["w"])){
388 $this->worker_dir = $opts["w"];
389 } else {
390 $this->worker_dir = "./workers";
391 }
392
bcd8c326 » David Shafik
2010-10-26 * Add ignore worker flag (-i)
393 if (isset($opts["i"])) {
394 if (!is_array($opts["i"])) {
395 $this->ignore_workers = array($opts["i"]);
396 } else {
397 $this->ignore_workers = $opts["i"];
398 }
399 }
400
8a3f16da » brian
2010-03-15 Moved all non-worker related config from the config file to command l…
401 if(!file_exists($this->worker_dir)){
402 $this->show_help("Worker dir ".$this->worker_dir." not found");
403 }
404
405
406 if(isset($opts["x"])){
407 $this->max_run_time = (int)$opts["x"];
408 }
409
410 if(isset($opts["D"])){
411 $this->do_all_count = (int)$opts["D"];
412 }
413
414 if(isset($opts["h"])){
415 if(!is_array($opts["h"])){
416 $this->servers = array($opts["h"]);
417 } else {
418 $this->servers = $opts["h"];
419 }
420 } else {
421 $this->servers = array("127.0.0.1");
422 }
423
8d30d46f » David Shafik
2010-12-01 * Add -p to allow prefixing class/function name
424 if(isset($opts["p"])){
425 $this->prefix = $opts["p"];
426 }
427
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
428 /**
429 * parse the config file
430 */
8a3f16da » brian
2010-03-15 Moved all non-worker related config from the config file to command l…
431 if(isset($opts["c"])){
432 $this->parse_config($opts["c"]);
433 }
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
434
435 }
436
437
438 /**
439 * Parses the config file
440 *
441 * @param string $file The config file. Just pass so we don't have
442 * to keep it around in a var
443 */
c851e2d7 » brian
2010-04-29 Added support for PEAR Net_Gearman. Enhanced logging of results and w…
444 protected function parse_config($file) {
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
445
446 $this->log("Loading configuration from $file");
447
8a3f16da » brian
2010-03-15 Moved all non-worker related config from the config file to command l…
448 if(substr($file, -4) == ".php"){
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
449
8a3f16da » brian
2010-03-15 Moved all non-worker related config from the config file to command l…
450 require $file;
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
451
8a3f16da » brian
2010-03-15 Moved all non-worker related config from the config file to command l…
452 } elseif(substr($file, -4) == ".ini"){
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
453
8a3f16da » brian
2010-03-15 Moved all non-worker related config from the config file to command l…
454 $gearman_config = parse_ini_file($file, true);
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
455
456 }
457
8a3f16da » brian
2010-03-15 Moved all non-worker related config from the config file to command l…
458 if(empty($gearman_config)){
459 $this->show_help("No configuration found in $file");
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
460 }
461
8a3f16da » brian
2010-03-15 Moved all non-worker related config from the config file to command l…
462 foreach($gearman_config as $function=>$data){
463 $this->functions[$function] = $data;
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
464 }
465
466 }
467
468 /**
469 * Forks the process and runs the given method. The parent then waits
470 * for the child process to signal back that it can continue
471 *
472 * @param string $method Class method to run after forking
473 *
474 */
c851e2d7 » brian
2010-04-29 Added support for PEAR Net_Gearman. Enhanced logging of results and w…
475 protected function fork_me($method){
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
476 $this->wait_for_signal = true;
477 $pid = pcntl_fork();
478 switch($pid) {
479 case 0:
43e03634 » brian
2010-05-26 change ischild to isparent so it is more accurate when daemonizing
480 $this->isparent = false;
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
481 $this->$method();
482 break;
483 case -1:
484 $this->log("Failed to fork");
485 $this->stop_work = true;
486 break;
487 default:
488 $this->helper_pid = $pid;
c851e2d7 » brian
2010-04-29 Added support for PEAR Net_Gearman. Enhanced logging of results and w…
489 while($this->wait_for_signal && !$this->stop_work) {
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
490 usleep(5000);
491 }
492 break;
493 }
494 }
495
496
497 /**
498 * Forked method that validates the worker code and checks it if desired
499 *
500 */
c851e2d7 » brian
2010-04-29 Added support for PEAR Net_Gearman. Enhanced logging of results and w…
501 protected function validate_workers(){
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
502
cc73aacd » brianlmoon
2010-08-26 Fixed some PID logic flaws with the helper process. When tracking cha…
503 $parent_pid = $this->pid;
504 $this->pid = getmypid();
505
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
506 $this->log("Helper forked", GearmanManager::LOG_LEVEL_PROC_INFO);
507
8a3f16da » brian
2010-03-15 Moved all non-worker related config from the config file to command l…
508 $this->log("Loading workers in ".$this->worker_dir);
509
510 $worker_files = glob($this->worker_dir."/*.php");
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
511
512 if(empty($worker_files)){
513 $this->log("No workers found");
cc73aacd » brianlmoon
2010-08-26 Fixed some PID logic flaws with the helper process. When tracking cha…
514 posix_kill($parent_pid, SIGUSR1);
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
515 exit();
516 }
517
c851e2d7 » brian
2010-04-29 Added support for PEAR Net_Gearman. Enhanced logging of results and w…
518 $this->validate_lib_workers($worker_files);
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
519
520 /**
521 * Since we got here, all must be ok, send a CONTINUE
522 */
cc73aacd » brianlmoon
2010-08-26 Fixed some PID logic flaws with the helper process. When tracking cha…
523 posix_kill($parent_pid, SIGCONT);
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
524
525 if($this->check_code){
cc73aacd » brianlmoon
2010-08-26 Fixed some PID logic flaws with the helper process. When tracking cha…
526 $this->log("Running loop to check for new code", self::LOG_LEVEL_DEBUG);
527 $last_check_time = 0;
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
528 while(1) {
cc73aacd » brianlmoon
2010-08-26 Fixed some PID logic flaws with the helper process. When tracking cha…
529 $max_time = 0;
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
530 foreach($worker_files as $f){
531 clearstatcache();
532 $mtime = filemtime($f);
cc73aacd » brianlmoon
2010-08-26 Fixed some PID logic flaws with the helper process. When tracking cha…
533 $max_time = max($max_time, $mtime);
534 $this->log("$f - $mtime $last_check_time", self::LOG_LEVEL_CRAZY);
535 if($last_check_time!=0 && $mtime > $last_check_time){
536 $this->log("New code found. Sending SIGHUP", self::LOG_LEVEL_PROC_INFO);
537 posix_kill($parent_pid, SIGHUP);
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
538 break;
539 }
540 }
cc73aacd » brianlmoon
2010-08-26 Fixed some PID logic flaws with the helper process. When tracking cha…
541 $last_check_time = $max_time;
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
542 sleep(5);
543 }
544 } else {
545 exit();
546 }
547
548 }
549
550 /**
551 * Bootstap a set of workers and any vars that need to be set
552 *
553 */
c851e2d7 » brian
2010-04-29 Added support for PEAR Net_Gearman. Enhanced logging of results and w…
554 protected function bootstrap() {
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
555
8a3f16da » brian
2010-03-15 Moved all non-worker related config from the config file to command l…
556 $function_count = array();
557
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
558 /**
559 * If we have "do_all" workers, start them first
560 * do_all workers register all functions
561 */
8a3f16da » brian
2010-03-15 Moved all non-worker related config from the config file to command l…
562 if(!empty($this->do_all_count) && is_int($this->do_all_count)){
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
563
8a3f16da » brian
2010-03-15 Moved all non-worker related config from the config file to command l…
564 for($x=0;$x<$this->do_all_count;$x++){
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
565 $this->start_worker();
566 }
567
e191044e » brian
2010-03-19 Adding support for classes being used as worker functions
568 foreach(array_keys($this->functions) as $worker){
8a3f16da » brian
2010-03-15 Moved all non-worker related config from the config file to command l…
569 $function_count[$worker] = $this->do_all_count;
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
570 }
571
572 }
573
574 /**
575 * Next we loop the workers and ensure we have enough running
576 * for each worker
577 */
8a3f16da » brian
2010-03-15 Moved all non-worker related config from the config file to command l…
578 foreach($this->functions as $worker=>$config) {
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
579
8a3f16da » brian
2010-03-15 Moved all non-worker related config from the config file to command l…
580 if(empty($function_count[$worker])){
581 $function_count[$worker] = 0;
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
582 }
583
8a3f16da » brian
2010-03-15 Moved all non-worker related config from the config file to command l…
584 while($function_count[$worker] < $config["count"]){
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
585 $this->start_worker($worker);
8a3f16da » brian
2010-03-15 Moved all non-worker related config from the config file to command l…
586 $function_count[$worker]++;;
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
587 }
588
589 /**
590 * php will eat up your cpu if you don't have this
591 */
592 usleep(50000);
593
594 }
595
596 /**
597 * Set the last code check time to now since we just loaded all the code
598 */
599 $this->last_check_time = time();
600
601 }
602
c851e2d7 » brian
2010-04-29 Added support for PEAR Net_Gearman. Enhanced logging of results and w…
603 protected function start_worker($worker="all") {
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
604
bcd8c326 » David Shafik
2010-10-26 * Add ignore worker flag (-i)
605 if (in_array($worker, $this->ignore_workers)) {
606 return;
607 }
608
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
609 $pid = pcntl_fork();
610
611 switch($pid) {
612
613 case 0:
614
43e03634 » brian
2010-05-26 change ischild to isparent so it is more accurate when daemonizing
615 $this->isparent = false;
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
616
c851e2d7 » brian
2010-04-29 Added support for PEAR Net_Gearman. Enhanced logging of results and w…
617 $this->register_ticks(false);
618
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
619 $this->pid = getmypid();
620
621 if($worker == "all"){
e191044e » brian
2010-03-19 Adding support for classes being used as worker functions
622 $worker_list = array_keys($this->functions);
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
623 } else {
624 $worker_list = array($worker);
625 }
626
c851e2d7 » brian
2010-04-29 Added support for PEAR Net_Gearman. Enhanced logging of results and w…
627 $this->start_lib_worker($worker_list);
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
628
629 $this->log("Child exiting", GearmanManager::LOG_LEVEL_WORKER_INFO);
630
631 exit();
632
633 break;
634
635 case -1:
636
637 $this->log("Could not fork");
638 $this->stop_work = true;
639 $this->stop_children();
640 break;
641
642 default:
643
644 // parent
645 $this->log("Started child $pid ($worker)", GearmanManager::LOG_LEVEL_PROC_INFO);
646 $this->children[$pid] = $worker;
647 }
648
649 }
650
e191044e » brian
2010-03-19 Adding support for classes being used as worker functions
651
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
652
653 /**
654 * Stops all running children
655 */
c851e2d7 » brian
2010-04-29 Added support for PEAR Net_Gearman. Enhanced logging of results and w…
656 protected function stop_children($signal=SIGTERM) {
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
657 $this->log("Stopping children", GearmanManager::LOG_LEVEL_PROC_INFO);
658
659 foreach($this->children as $pid=>$worker){
660 $this->log("Stopping child $pid ($worker)", GearmanManager::LOG_LEVEL_PROC_INFO);
c851e2d7 » brian
2010-04-29 Added support for PEAR Net_Gearman. Enhanced logging of results and w…
661 posix_kill($pid, $signal);
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
662 }
663
664 }
665
666 /**
667 * Registers the process signal listeners
668 */
c851e2d7 » brian
2010-04-29 Added support for PEAR Net_Gearman. Enhanced logging of results and w…
669 protected function register_ticks($parent=true) {
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
670
671 if($parent){
c851e2d7 » brian
2010-04-29 Added support for PEAR Net_Gearman. Enhanced logging of results and w…
672 $this->log("Registering signals for parent", GearmanManager::LOG_LEVEL_DEBUG);
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
673 pcntl_signal(SIGTERM, array($this, "signal"));
674 pcntl_signal(SIGINT, array($this, "signal"));
675 pcntl_signal(SIGUSR1, array($this, "signal"));
676 pcntl_signal(SIGUSR2, array($this, "signal"));
677 pcntl_signal(SIGCONT, array($this, "signal"));
678 pcntl_signal(SIGHUP, array($this, "signal"));
679 } else {
c851e2d7 » brian
2010-04-29 Added support for PEAR Net_Gearman. Enhanced logging of results and w…
680 $this->log("Registering signals for child", GearmanManager::LOG_LEVEL_DEBUG);
681 $res = pcntl_signal(SIGTERM, array($this, "signal"));
682 if(!$res){
683 exit();
684 }
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
685 }
686 }
687
688 /**
689 * Handles signals
690 */
691 public function signal($signo) {
692
c851e2d7 » brian
2010-04-29 Added support for PEAR Net_Gearman. Enhanced logging of results and w…
693 static $term_count = 0;
694
43e03634 » brian
2010-05-26 change ischild to isparent so it is more accurate when daemonizing
695 if(!$this->isparent){
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
696
697 $this->stop_work = true;
698
699 } else {
700
701 switch ($signo) {
702 case SIGUSR1:
703 $this->show_help("No worker files could be found");
704 break;
705 case SIGUSR2:
706 $this->show_help("Error validating worker functions");
707 break;
708 case SIGCONT:
709 $this->wait_for_signal = false;
710 break;
711 case SIGINT:
712 case SIGTERM:
713 $this->log("Shutting down...");
714 $this->stop_work = true;
c851e2d7 » brian
2010-04-29 Added support for PEAR Net_Gearman. Enhanced logging of results and w…
715 $this->stop_time = time();
716 $term_count++;
717 if($term_count < 5){
718 $this->stop_children();
719 } else {
720 $this->stop_children(SIGKILL);
721 }
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
722 break;
723 case SIGHUP:
724 $this->log("Restarting children", GearmanManager::LOG_LEVEL_PROC_INFO);
725 $this->stop_children();
726 break;
727 default:
728 // handle all other signals
729 }
730 }
731
732 }
733
734 /**
735 * Logs data to disk or stdout
736 */
c851e2d7 » brian
2010-04-29 Added support for PEAR Net_Gearman. Enhanced logging of results and w…
737 protected function log($message, $level=GearmanManager::LOG_LEVEL_INFO) {
af9fa744 » brian
2010-03-14 Change logging output to have more information. Change how functions …
738
739 static $init = false;
740
8a3f16da » brian
2010-03-15 Moved all non-worker related config from the config file to command l…
741 if($level > $this->verbose) return;
742
3d0d6933 » hjr3
2010-05-06 Added syslog support.
743 if ($this->log_syslog) {
744 $this->syslog($message, $level);
745 return;
746 }
747
af9fa744 » brian
2010-03-14 Change logging output to have more information. Change how functions …
748 if(!$init){
749 $init = true;
750
751 if($this->log_file_handle){
752 $ds = date("Y-m-d H:i:s");
753 fwrite($this->log_file_handle, "Date PID Type Message\n");
754 } else {
755 echo "PID Type Message\n";
756 }
757
758 }
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
759
af9fa744 » brian
2010-03-14 Change logging output to have more information. Change how functions …
760 $label = "";
761
762 switch($level) {
763 case GearmanManager::LOG_LEVEL_INFO;
764 $label = "INFO ";
765 break;
766 case GearmanManager::LOG_LEVEL_PROC_INFO:
767 $label = "PROC ";
768 break;
769 case GearmanManager::LOG_LEVEL_WORKER_INFO:
770 $label = "WORKER";
771 break;
772 case GearmanManager::LOG_LEVEL_DEBUG:
773 $label = "DEBUG ";
774 break;
cc73aacd » brianlmoon
2010-08-26 Fixed some PID logic flaws with the helper process. When tracking cha…
775 case GearmanManager::LOG_LEVEL_CRAZY:
776 $label = "CRAZY ";
777 break;
af9fa744 » brian
2010-03-14 Change logging output to have more information. Change how functions …
778 }
779
780
781 $log_pid = str_pad($this->pid, 5, " ", STR_PAD_LEFT);
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
782
783 if($this->log_file_handle){
af9fa744 » brian
2010-03-14 Change logging output to have more information. Change how functions …
784 $ds = date("Y-m-d H:i:s");
cc73aacd » brianlmoon
2010-08-26 Fixed some PID logic flaws with the helper process. When tracking cha…
785 $prefix = "[$ds] $log_pid $label";
786 fwrite($this->log_file_handle, $prefix." ".str_replace("\n", "\n$prefix ", trim($message))."\n");
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
787 } else {
cc73aacd » brianlmoon
2010-08-26 Fixed some PID logic flaws with the helper process. When tracking cha…
788 $prefix = "$log_pid $label";
789 echo $prefix." ".str_replace("\n", "\n$prefix ", trim($message))."\n";
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
790 }
791
792 }
793
794 /**
3d0d6933 » hjr3
2010-05-06 Added syslog support.
795 * Logs data to syslog
796 */
797 protected function syslog($message, $level) {
798 switch($level) {
799 case GearmanManager::LOG_LEVEL_INFO;
800 case GearmanManager::LOG_LEVEL_PROC_INFO:
801 case GearmanManager::LOG_LEVEL_WORKER_INFO:
802 default:
803 $priority = LOG_INFO;
804 break;
805 case GearmanManager::LOG_LEVEL_DEBUG:
806 $priority = LOG_DEBUG;
807 break;
808 }
809
810 if (!syslog($priority, $message)) {
811 echo "Unable to write to syslog\n";
812 }
813 }
814
815 /**
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
816 * Shows the scripts help info with optional error message
817 */
c851e2d7 » brian
2010-04-29 Added support for PEAR Net_Gearman. Enhanced logging of results and w…
818 protected function show_help($msg = "") {
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
819 if($msg){
820 echo "ERROR:\n";
821 echo " ".wordwrap($msg, 72, "\n ")."\n\n";
822 }
823 echo "Gearman worker manager script\n\n";
824 echo "USAGE:\n";
20c397b8 » brian
2010-03-14 just show the basename, not the full path in the help
825 echo " # ".basename(__FILE__)." -h | -c CONFIG [-v] [-l LOG_FILE] [-d] [-v] [-a] [-P PID_FILE]\n\n";
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
826 echo "OPTIONS:\n";
c851e2d7 » brian
2010-04-29 Added support for PEAR Net_Gearman. Enhanced logging of results and w…
827 echo " -a Automatically check for new worker code\n";
828 echo " -c CONFIG Worker configuration file\n";
829 echo " -d Daemon, detach and run in the background\n";
830 echo " -D NUMBER Start NUMBER workers that do all jobs\n";
831 echo " -h HOST[:PORT] Connect to HOST and optional PORT\n";
832 echo " -H Shows this help\n";
bcd8c326 » David Shafik
2010-10-26 * Add ignore worker flag (-i)
833 echo " -i WORKER Ignore WORKER\n";
3d0d6933 » hjr3
2010-05-06 Added syslog support.
834 echo " -l LOG_FILE Log output to LOG_FILE or use keyword 'syslog' for syslog support\n";
8d30d46f » David Shafik
2010-12-01 * Add -p to allow prefixing class/function name
835 echo " -p PREFIX Prefix function/class name with PREFIX\n";
c851e2d7 » brian
2010-04-29 Added support for PEAR Net_Gearman. Enhanced logging of results and w…
836 echo " -P PID_FILE File to write process ID out to\n";
bcd8c326 » David Shafik
2010-10-26 * Add ignore worker flag (-i)
837 echo " -r Restart workers after each job is complete\n";
838 echo " -u USERNAME Run wokers as USERNAME\n";
c851e2d7 » brian
2010-04-29 Added support for PEAR Net_Gearman. Enhanced logging of results and w…
839 echo " -v Increase verbosity level by one\n";
840 echo " -w DIR Directory where workers are located\n";
841 echo " -x SECONDS Maximum seconds for a worker to live\n";
c5d9581d » brian
2010-03-14 Initial commit for GearmanManager
842 echo "\n";
843 exit();
844 }
845
846 }
847
ead419fe » hjr3
2010-05-06 Get new pid after daemonizing.
848 ?>
Something went wrong with that request. Please try again.