@@ -187,6 +187,8 @@ public function work($interval = Resque::DEFAULT_INTERVAL, $blocking = false)
187
187
pcntl_signal_dispatch ();
188
188
}
189
189
190
+ $ ready_statuses = array (Status::STATUS_WAITING , Status::STATUS_RUNNING );
191
+
190
192
while (true ) {
191
193
if ($ this ->shutdown ) {
192
194
break ;
@@ -208,7 +210,9 @@ public function work($interval = Resque::DEFAULT_INTERVAL, $blocking = false)
208
210
$ job = false ;
209
211
if (!$ this ->paused ) {
210
212
if ($ blocking === true ) {
211
- $ this ->logger ->log (LogLevel::INFO , 'Starting blocking with timeout of {interval} ' , array ('interval ' => $ interval ));
213
+ $ context = array ('interval ' => $ interval );
214
+ $ message = 'Starting blocking with timeout of {interval} ' ;
215
+ $ this ->logger ->log (LogLevel::INFO , $ message , $ context );
212
216
$ this ->updateProcLine ('Waiting with blocking timeout ' . $ interval );
213
217
} else {
214
218
$ this ->updateProcLine ('Waiting with interval ' . $ interval );
@@ -225,7 +229,8 @@ public function work($interval = Resque::DEFAULT_INTERVAL, $blocking = false)
225
229
226
230
if ($ blocking === false ) {
227
231
// If no job was found, we sleep for $interval before continuing and checking again
228
- $ this ->logger ->log (LogLevel::INFO , 'Sleeping for {interval} ' , array ('interval ' => $ interval ));
232
+ $ context = array ('interval ' => $ interval );
233
+ $ this ->logger ->log (LogLevel::INFO , 'Sleeping for {interval} ' , $ context );
229
234
if ($ this ->paused ) {
230
235
$ this ->updateProcLine ('Paused ' );
231
236
} else {
@@ -238,7 +243,8 @@ public function work($interval = Resque::DEFAULT_INTERVAL, $blocking = false)
238
243
continue ;
239
244
}
240
245
241
- $ this ->logger ->log (LogLevel::NOTICE , 'Starting work on {job} ' , array ('job ' => $ job ));
246
+ $ context = array ('job ' => $ job );
247
+ $ this ->logger ->log (LogLevel::NOTICE , 'Starting work on {job} ' , $ context );
242
248
Event::trigger ('beforeFork ' , $ job );
243
249
$ this ->workingOn ($ job );
244
250
@@ -288,7 +294,7 @@ public function work($interval = Resque::DEFAULT_INTERVAL, $blocking = false)
288
294
'Job exited with exit code ' . $ exitStatus
289
295
));
290
296
} else {
291
- if (in_array ($ job ->getStatus (), array (Status:: STATUS_WAITING , Status:: STATUS_RUNNING ) )) {
297
+ if (in_array ($ job ->getStatus (), $ ready_statuses )) {
292
298
$ job ->updateStatus (Status::STATUS_COMPLETE );
293
299
$ this ->logger ->log (LogLevel::INFO , 'done ' . $ job );
294
300
}
@@ -314,11 +320,13 @@ public function perform(JobHandler $job)
314
320
Event::trigger ('afterFork ' , $ job );
315
321
$ result = $ job ->perform ();
316
322
} catch (Exception $ e ) {
317
- $ this ->logger ->log (LogLevel::CRITICAL , '{job} has failed {exception} ' , array ('job ' => $ job , 'exception ' => $ e ));
323
+ $ context = array ('job ' => $ job , 'exception ' => $ e );
324
+ $ this ->logger ->log (LogLevel::CRITICAL , '{job} has failed {exception} ' , $ context );
318
325
$ job ->fail ($ e );
319
326
return ;
320
327
} catch (Error $ e ) {
321
- $ this ->logger ->log (LogLevel::CRITICAL , '{job} has failed {exception} ' , array ('job ' => $ job , 'exception ' => $ e ));
328
+ $ context = array ('job ' => $ job , 'exception ' => $ e );
329
+ $ this ->logger ->log (LogLevel::CRITICAL , '{job} has failed {exception} ' , $ context );
322
330
$ job ->fail ($ e );
323
331
return ;
324
332
}
@@ -346,21 +354,25 @@ public function reserve($blocking = false, $timeout = null)
346
354
347
355
if ($ blocking === true ) {
348
356
if (empty ($ queues )) {
349
- $ this ->logger ->log (LogLevel::INFO , 'No queue was found, sleeping for {interval} ' , array ('interval ' => $ timeout ));
357
+ $ context = array ('interval ' => $ timeout );
358
+ $ this ->logger ->log (LogLevel::INFO , 'No queue was found, sleeping for {interval} ' , $ context );
350
359
usleep ($ timeout * 1000000 );
351
360
return false ;
352
361
}
353
362
$ job = JobHandler::reserveBlocking ($ queues , $ timeout );
354
363
if ($ job ) {
355
- $ this ->logger ->log (LogLevel::INFO , 'Found job on {queue} ' , array ('queue ' => $ job ->queue ));
364
+ $ context = array ('queue ' => $ job ->queue );
365
+ $ this ->logger ->log (LogLevel::INFO , 'Found job on {queue} ' , $ context );
356
366
return $ job ;
357
367
}
358
368
} else {
359
369
foreach ($ queues as $ queue ) {
360
- $ this ->logger ->log (LogLevel::INFO , 'Checking {queue} for jobs ' , array ('queue ' => $ queue ));
370
+ $ context = array ('queue ' => $ queue );
371
+ $ this ->logger ->log (LogLevel::INFO , 'Checking {queue} for jobs ' , $ context );
361
372
$ job = JobHandler::reserve ($ queue );
362
373
if ($ job ) {
363
- $ this ->logger ->log (LogLevel::INFO , 'Found job on {queue} ' , array ('queue ' => $ job ->queue ));
374
+ $ context = array ('queue ' => $ job ->queue );
375
+ $ this ->logger ->log (LogLevel::INFO , 'Found job on {queue} ' , $ context );
364
376
return $ job ;
365
377
}
366
378
}
@@ -411,7 +423,8 @@ private function startup()
411
423
*/
412
424
private function updateProcLine ($ status )
413
425
{
414
- $ processTitle = static ::$ processPrefix . '- ' . Resque::VERSION . ' ( ' . implode (', ' , $ this ->queues ) . '): ' . $ status ;
426
+ $ processTitle = static ::$ processPrefix . '- ' . Resque::VERSION ;
427
+ $ processTitle .= ' ( ' . implode (', ' , $ this ->queues ) . '): ' . $ status ;
415
428
if (function_exists ('cli_set_process_title ' ) && PHP_OS !== 'Darwin ' ) {
416
429
cli_set_process_title ($ processTitle );
417
430
} elseif (function_exists ('setproctitle ' )) {
@@ -500,13 +513,16 @@ public function killChild()
500
513
return ;
501
514
}
502
515
503
- $ this ->logger ->log (LogLevel::INFO , 'Killing child at {child} ' , array ('child ' => $ this ->child ));
516
+ $ context = array ('child ' => $ this ->child );
517
+ $ this ->logger ->log (LogLevel::INFO , 'Killing child at {child} ' , $ context );
504
518
if (exec ('ps -o pid,s -p ' . $ this ->child , $ output , $ returnCode ) && $ returnCode != 1 ) {
505
- $ this ->logger ->log (LogLevel::DEBUG , 'Child {child} found, killing. ' , array ('child ' => $ this ->child ));
519
+ $ context = array ('child ' => $ this ->child );
520
+ $ this ->logger ->log (LogLevel::DEBUG , 'Child {child} found, killing. ' , $ context );
506
521
posix_kill ($ this ->child , SIGKILL );
507
522
$ this ->child = null ;
508
523
} else {
509
- $ this ->logger ->log (LogLevel::INFO , 'Child {child} not found, restarting. ' , array ('child ' => $ this ->child ));
524
+ $ context = array ('child ' => $ this ->child );
525
+ $ this ->logger ->log (LogLevel::INFO , 'Child {child} not found, restarting. ' , $ context );
510
526
$ this ->shutdown ();
511
527
}
512
528
}
@@ -529,7 +545,8 @@ public function pruneDeadWorkers()
529
545
if ($ host != $ this ->hostname || in_array ($ pid , $ workerPids ) || $ pid == getmypid ()) {
530
546
continue ;
531
547
}
532
- $ this ->logger ->log (LogLevel::INFO , 'Pruning dead worker: {worker} ' , array ('worker ' => (string )$ worker ));
548
+ $ context = array ('worker ' => (string )$ worker );
549
+ $ this ->logger ->log (LogLevel::INFO , 'Pruning dead worker: {worker} ' , $ context );
533
550
$ worker ->unregisterWorker ();
534
551
}
535
552
}
0 commit comments