@@ -1,5 +1,5 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | -declare(ticks = 1); |
|
| 2 | +declare(ticks=1); |
|
| 3 | 3 | |
| 4 | 4 | /** |
| 5 | 5 | * Resque worker that handles checking queues for jobs, fetching them |
@@ -71,14 +71,14 @@ discard block |
||
| 71 | 71 | { |
| 72 | 72 | $this->logger = new Resque_Log(); |
| 73 | 73 | |
| 74 | - if(!is_array($queues)) { |
|
| 74 | + if (!is_array($queues)) { |
|
| 75 | 75 | $queues = array($queues); |
| 76 | 76 | } |
| 77 | 77 | |
| 78 | 78 | $this->queues = $queues; |
| 79 | 79 | $this->hostname = php_uname('n'); |
| 80 | 80 | |
| 81 | - $this->id = $this->hostname . ':'.getmypid() . ':' . implode(',', $this->queues); |
|
| 81 | + $this->id = $this->hostname . ':' . getmypid() . ':' . implode(',', $this->queues); |
|
| 82 | 82 | } |
| 83 | 83 | |
| 84 | 84 | /** |
@@ -88,12 +88,12 @@ discard block |
||
| 88 | 88 | public static function all() |
| 89 | 89 | { |
| 90 | 90 | $workers = Resque::redis()->smembers('workers'); |
| 91 | - if(!is_array($workers)) { |
|
| 91 | + if (!is_array($workers)) { |
|
| 92 | 92 | $workers = array(); |
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | $instances = array(); |
| 96 | - foreach($workers as $workerId) { |
|
| 96 | + foreach ($workers as $workerId) { |
|
| 97 | 97 | $instances[] = self::find($workerId); |
| 98 | 98 | } |
| 99 | 99 | return $instances; |
@@ -118,7 +118,7 @@ discard block |
||
| 118 | 118 | */ |
| 119 | 119 | public static function find($workerId) |
| 120 | 120 | { |
| 121 | - if(!self::exists($workerId) || false === strpos($workerId, ":")) { |
|
| 121 | + if (!self::exists($workerId) || false === strpos($workerId, ":")) { |
|
| 122 | 122 | return false; |
| 123 | 123 | } |
| 124 | 124 | |
@@ -152,15 +152,15 @@ discard block |
||
| 152 | 152 | $this->updateProcLine('Starting'); |
| 153 | 153 | $this->startup(); |
| 154 | 154 | |
| 155 | - while(true) { |
|
| 156 | - if($this->shutdown) { |
|
| 155 | + while (true) { |
|
| 156 | + if ($this->shutdown) { |
|
| 157 | 157 | break; |
| 158 | 158 | } |
| 159 | 159 | |
| 160 | 160 | // Attempt to find and reserve a job |
| 161 | 161 | $job = false; |
| 162 | - if(!$this->paused) { |
|
| 163 | - if($blocking === true) { |
|
| 162 | + if (!$this->paused) { |
|
| 163 | + if ($blocking === true) { |
|
| 164 | 164 | $this->logger->log(Psr\Log\LogLevel::INFO, 'Starting blocking with timeout of {interval}', array('interval' => $interval)); |
| 165 | 165 | $this->updateProcLine('Waiting for ' . implode(',', $this->queues) . ' with blocking timeout ' . $interval); |
| 166 | 166 | } else { |
@@ -172,17 +172,17 @@ discard block |
||
| 172 | 172 | |
| 173 | 173 | pcntl_signal_dispatch(); |
| 174 | 174 | |
| 175 | - if(!$job) { |
|
| 175 | + if (!$job) { |
|
| 176 | 176 | // For an interval of 0, break now - helps with unit testing etc |
| 177 | - if($interval == 0) { |
|
| 177 | + if ($interval == 0) { |
|
| 178 | 178 | break; |
| 179 | 179 | } |
| 180 | 180 | |
| 181 | - if($blocking === false) |
|
| 181 | + if ($blocking === false) |
|
| 182 | 182 | { |
| 183 | 183 | // If no job was found, we sleep for $interval before continuing and checking again |
| 184 | 184 | $this->logger->log(Psr\Log\LogLevel::INFO, 'Sleeping for {interval}', array('interval' => $interval)); |
| 185 | - if($this->paused) { |
|
| 185 | + if ($this->paused) { |
|
| 186 | 186 | $this->updateProcLine('Paused'); |
| 187 | 187 | } |
| 188 | 188 | else { |
@@ -212,7 +212,7 @@ discard block |
||
| 212 | 212 | } |
| 213 | 213 | } |
| 214 | 214 | |
| 215 | - if($this->child > 0) { |
|
| 215 | + if ($this->child > 0) { |
|
| 216 | 216 | // Parent process, sit and wait |
| 217 | 217 | $status = 'Forked ' . $this->child . ' at ' . strftime('%F %T'); |
| 218 | 218 | $this->updateProcLine($status); |
@@ -256,7 +256,7 @@ discard block |
||
| 256 | 256 | Resque_Event::trigger('afterFork', $job); |
| 257 | 257 | $job->perform(); |
| 258 | 258 | } |
| 259 | - catch(Exception $e) { |
|
| 259 | + catch (Exception $e) { |
|
| 260 | 260 | $this->logger->log(Psr\Log\LogLevel::CRITICAL, '{job} has failed {stack}', array('job' => $job, 'stack' => $e)); |
| 261 | 261 | $job->fail($e); |
| 262 | 262 | return; |
@@ -280,21 +280,21 @@ discard block |
||
| 280 | 280 | } |
| 281 | 281 | |
| 282 | 282 | $queues = $this->queues(); |
| 283 | - if(!is_array($queues)) { |
|
| 283 | + if (!is_array($queues)) { |
|
| 284 | 284 | return; |
| 285 | 285 | } |
| 286 | 286 | |
| 287 | - if($blocking === true) { |
|
| 287 | + if ($blocking === true) { |
|
| 288 | 288 | $job = Resque_Job::reserveBlocking($queues, $timeout); |
| 289 | - if($job) { |
|
| 289 | + if ($job) { |
|
| 290 | 290 | $this->logger->log(Psr\Log\LogLevel::INFO, 'Found job on {queue}', array('queue' => $job->queue)); |
| 291 | 291 | return $job; |
| 292 | 292 | } |
| 293 | 293 | } else { |
| 294 | - foreach($queues as $queue) { |
|
| 294 | + foreach ($queues as $queue) { |
|
| 295 | 295 | $this->logger->log(Psr\Log\LogLevel::INFO, 'Checking {queue} for jobs', array('queue' => $queue)); |
| 296 | 296 | $job = Resque_Job::reserve($queue); |
| 297 | - if($job) { |
|
| 297 | + if ($job) { |
|
| 298 | 298 | $this->logger->log(Psr\Log\LogLevel::INFO, 'Found job on {queue}', array('queue' => $job->queue)); |
| 299 | 299 | return $job; |
| 300 | 300 | } |
@@ -317,7 +317,7 @@ discard block |
||
| 317 | 317 | */ |
| 318 | 318 | public function queues($fetch = true) |
| 319 | 319 | { |
| 320 | - if(!in_array('*', $this->queues) || $fetch == false) { |
|
| 320 | + if (!in_array('*', $this->queues) || $fetch == false) { |
|
| 321 | 321 | return $this->queues; |
| 322 | 322 | } |
| 323 | 323 | |
@@ -347,10 +347,10 @@ discard block |
||
| 347 | 347 | private function updateProcLine($status) |
| 348 | 348 | { |
| 349 | 349 | $processTitle = 'resque-' . Resque::VERSION . ': ' . $status; |
| 350 | - if(function_exists('cli_set_process_title') && PHP_OS !== 'Darwin') { |
|
| 350 | + if (function_exists('cli_set_process_title') && PHP_OS !== 'Darwin') { |
|
| 351 | 351 | cli_set_process_title($processTitle); |
| 352 | 352 | } |
| 353 | - else if(function_exists('setproctitle')) { |
|
| 353 | + else if (function_exists('setproctitle')) { |
|
| 354 | 354 | setproctitle($processTitle); |
| 355 | 355 | } |
| 356 | 356 | } |
@@ -365,7 +365,7 @@ discard block |
||
| 365 | 365 | */ |
| 366 | 366 | private function registerSigHandlers() |
| 367 | 367 | { |
| 368 | - if(!function_exists('pcntl_signal')) { |
|
| 368 | + if (!function_exists('pcntl_signal')) { |
|
| 369 | 369 | return; |
| 370 | 370 | } |
| 371 | 371 | |
@@ -423,13 +423,13 @@ discard block |
||
| 423 | 423 | */ |
| 424 | 424 | public function killChild() |
| 425 | 425 | { |
| 426 | - if(!$this->child) { |
|
| 426 | + if (!$this->child) { |
|
| 427 | 427 | $this->logger->log(Psr\Log\LogLevel::DEBUG, 'No child to kill.'); |
| 428 | 428 | return; |
| 429 | 429 | } |
| 430 | 430 | |
| 431 | 431 | $this->logger->log(Psr\Log\LogLevel::INFO, 'Killing child at {child}', array('child' => $this->child)); |
| 432 | - if(exec('ps -o pid,state -p ' . $this->child, $output, $returnCode) && $returnCode != 1) { |
|
| 432 | + if (exec('ps -o pid,state -p ' . $this->child, $output, $returnCode) && $returnCode != 1) { |
|
| 433 | 433 | $this->logger->log(Psr\Log\LogLevel::DEBUG, 'Child {child} found, killing.', array('child' => $this->child)); |
| 434 | 434 | posix_kill($this->child, SIGKILL); |
| 435 | 435 | $this->child = null; |
@@ -452,10 +452,10 @@ discard block |
||
| 452 | 452 | { |
| 453 | 453 | $workerPids = $this->workerPids(); |
| 454 | 454 | $workers = self::all(); |
| 455 | - foreach($workers as $worker) { |
|
| 455 | + foreach ($workers as $worker) { |
|
| 456 | 456 | if (is_object($worker)) { |
| 457 | 457 | list($host, $pid, $queues) = explode(':', (string)$worker, 3); |
| 458 | - if($host != $this->hostname || in_array($pid, $workerPids) || $pid == getmypid()) { |
|
| 458 | + if ($host != $this->hostname || in_array($pid, $workerPids) || $pid == getmypid()) { |
|
| 459 | 459 | continue; |
| 460 | 460 | } |
| 461 | 461 | $this->logger->log(Psr\Log\LogLevel::INFO, 'Pruning dead worker: {worker}', array('worker' => (string)$worker)); |
@@ -474,7 +474,7 @@ discard block |
||
| 474 | 474 | { |
| 475 | 475 | $pids = array(); |
| 476 | 476 | exec('ps -A -o pid,command | grep [r]esque', $cmdOutput); |
| 477 | - foreach($cmdOutput as $line) { |
|
| 477 | + foreach ($cmdOutput as $line) { |
|
| 478 | 478 | list($pids[],) = explode(' ', trim($line), 2); |
| 479 | 479 | } |
| 480 | 480 | return $pids; |
@@ -494,7 +494,7 @@ discard block |
||
| 494 | 494 | */ |
| 495 | 495 | public function unregisterWorker() |
| 496 | 496 | { |
| 497 | - if(is_object($this->currentJob)) { |
|
| 497 | + if (is_object($this->currentJob)) { |
|
| 498 | 498 | $this->currentJob->fail(new Resque_Job_DirtyExitException); |
| 499 | 499 | } |
| 500 | 500 | |
@@ -554,7 +554,7 @@ discard block |
||
| 554 | 554 | public function job() |
| 555 | 555 | { |
| 556 | 556 | $job = Resque::redis()->get('worker:' . $this); |
| 557 | - if(!$job) { |
|
| 557 | + if (!$job) { |
|
| 558 | 558 | return array(); |
| 559 | 559 | } |
| 560 | 560 | else { |