@@ -3,7 +3,7 @@ |
||
| 3 | 3 | { |
| 4 | 4 | public function perform() |
| 5 | 5 | { |
| 6 | - fwrite(STDOUT, 'Start job! -> '); |
|
| 6 | + fwrite(STDOUT, 'Start job! -> '); |
|
| 7 | 7 | sleep(1); |
| 8 | 8 | fwrite(STDOUT, 'Job ended!' . PHP_EOL); |
| 9 | 9 | } |
@@ -20,7 +20,7 @@ |
||
| 20 | 20 | if (empty($argv[2])) { |
| 21 | 21 | $jobId = Resque::enqueue('default', $argv[1], $args, true); |
| 22 | 22 | } else { |
| 23 | - $jobId = Resque::enqueue($argv[1], $argv[2], $args, true); |
|
| 23 | + $jobId = Resque::enqueue($argv[1], $argv[2], $args, true); |
|
| 24 | 24 | } |
| 25 | 25 | |
| 26 | 26 | echo "Queued job ".$jobId."\n\n"; |
@@ -10,7 +10,7 @@ discard block |
||
| 10 | 10 | { |
| 11 | 11 | const VERSION = '1.2'; |
| 12 | 12 | |
| 13 | - const DEFAULT_INTERVAL = 5; |
|
| 13 | + const DEFAULT_INTERVAL = 5; |
|
| 14 | 14 | |
| 15 | 15 | /** |
| 16 | 16 | * @var Resque_Redis Instance of Resque_Redis that talks to redis. |
@@ -122,7 +122,7 @@ discard block |
||
| 122 | 122 | */ |
| 123 | 123 | public static function pop($queue) |
| 124 | 124 | { |
| 125 | - $item = self::redis()->lpop('queue:' . $queue); |
|
| 125 | + $item = self::redis()->lpop('queue:' . $queue); |
|
| 126 | 126 | |
| 127 | 127 | if(!$item) { |
| 128 | 128 | return; |
@@ -140,11 +140,11 @@ discard block |
||
| 140 | 140 | */ |
| 141 | 141 | public static function dequeue($queue, $items = Array()) |
| 142 | 142 | { |
| 143 | - if(count($items) > 0) { |
|
| 143 | + if(count($items) > 0) { |
|
| 144 | 144 | return self::removeItems($queue, $items); |
| 145 | - } else { |
|
| 145 | + } else { |
|
| 146 | 146 | return self::removeList($queue); |
| 147 | - } |
|
| 147 | + } |
|
| 148 | 148 | } |
| 149 | 149 | |
| 150 | 150 | /** |
@@ -155,9 +155,9 @@ discard block |
||
| 155 | 155 | */ |
| 156 | 156 | public static function removeQueue($queue) |
| 157 | 157 | { |
| 158 | - $num = self::removeList($queue); |
|
| 159 | - self::redis()->srem('queues', $queue); |
|
| 160 | - return $num; |
|
| 158 | + $num = self::removeList($queue); |
|
| 159 | + self::redis()->srem('queues', $queue); |
|
| 160 | + return $num; |
|
| 161 | 161 | } |
| 162 | 162 | |
| 163 | 163 | /** |
@@ -170,28 +170,28 @@ discard block |
||
| 170 | 170 | */ |
| 171 | 171 | public static function blpop(array $queues, $timeout) |
| 172 | 172 | { |
| 173 | - $list = array(); |
|
| 174 | - foreach($queues AS $queue) { |
|
| 173 | + $list = array(); |
|
| 174 | + foreach($queues AS $queue) { |
|
| 175 | 175 | $list[] = 'queue:' . $queue; |
| 176 | - } |
|
| 176 | + } |
|
| 177 | 177 | |
| 178 | - $item = self::redis()->blpop($list, (int)$timeout); |
|
| 178 | + $item = self::redis()->blpop($list, (int)$timeout); |
|
| 179 | 179 | |
| 180 | - if(!$item) { |
|
| 180 | + if(!$item) { |
|
| 181 | 181 | return; |
| 182 | - } |
|
| 182 | + } |
|
| 183 | 183 | |
| 184 | - /** |
|
| 185 | - * Normally the Resque_Redis class returns queue names without the prefix |
|
| 186 | - * But the blpop is a bit different. It returns the name as prefix:queue:name |
|
| 187 | - * So we need to strip off the prefix:queue: part |
|
| 188 | - */ |
|
| 189 | - $queue = substr($item[0], strlen(self::redis()->getPrefix() . 'queue:')); |
|
| 184 | + /** |
|
| 185 | + * Normally the Resque_Redis class returns queue names without the prefix |
|
| 186 | + * But the blpop is a bit different. It returns the name as prefix:queue:name |
|
| 187 | + * So we need to strip off the prefix:queue: part |
|
| 188 | + */ |
|
| 189 | + $queue = substr($item[0], strlen(self::redis()->getPrefix() . 'queue:')); |
|
| 190 | 190 | |
| 191 | - return array( |
|
| 191 | + return array( |
|
| 192 | 192 | 'queue' => $queue, |
| 193 | 193 | 'payload' => json_decode($item[1], true) |
| 194 | - ); |
|
| 194 | + ); |
|
| 195 | 195 | } |
| 196 | 196 | |
| 197 | 197 | /** |
@@ -304,7 +304,7 @@ discard block |
||
| 304 | 304 | while (!$finished) { |
| 305 | 305 | $string = self::redis()->rpoplpush($requeueQueue, self::redis()->getPrefix() .$originalQueue); |
| 306 | 306 | if (empty($string)) { |
| 307 | - $finished = true; |
|
| 307 | + $finished = true; |
|
| 308 | 308 | } |
| 309 | 309 | } |
| 310 | 310 | |
@@ -327,29 +327,29 @@ discard block |
||
| 327 | 327 | */ |
| 328 | 328 | private static function matchItem($string, $items) |
| 329 | 329 | { |
| 330 | - $decoded = json_decode($string, true); |
|
| 330 | + $decoded = json_decode($string, true); |
|
| 331 | 331 | |
| 332 | - foreach($items as $key => $val) { |
|
| 332 | + foreach($items as $key => $val) { |
|
| 333 | 333 | # class name only ex: item[0] = ['class'] |
| 334 | 334 | if (is_numeric($key)) { |
| 335 | - if($decoded['class'] == $val) { |
|
| 335 | + if($decoded['class'] == $val) { |
|
| 336 | 336 | return true; |
| 337 | - } |
|
| 337 | + } |
|
| 338 | 338 | # class name with args , example: item[0] = ['class' => {'foo' => 1, 'bar' => 2}] |
| 339 | - } elseif (is_array($val)) { |
|
| 340 | - $decodedArgs = (array)$decoded['args'][0]; |
|
| 341 | - if ($decoded['class'] == $key && |
|
| 339 | + } elseif (is_array($val)) { |
|
| 340 | + $decodedArgs = (array)$decoded['args'][0]; |
|
| 341 | + if ($decoded['class'] == $key && |
|
| 342 | 342 | count($decodedArgs) > 0 && count(array_diff($decodedArgs, $val)) == 0) { |
| 343 | 343 | return true; |
| 344 | 344 | } |
| 345 | 345 | # class name with ID, example: item[0] = ['class' => 'id'] |
| 346 | 346 | } else { |
| 347 | - if ($decoded['class'] == $key && $decoded['id'] == $val) { |
|
| 347 | + if ($decoded['class'] == $key && $decoded['id'] == $val) { |
|
| 348 | 348 | return true; |
| 349 | - } |
|
| 349 | + } |
|
| 350 | + } |
|
| 350 | 351 | } |
| 351 | - } |
|
| 352 | - return false; |
|
| 352 | + return false; |
|
| 353 | 353 | } |
| 354 | 354 | |
| 355 | 355 | /** |
@@ -362,9 +362,9 @@ discard block |
||
| 362 | 362 | */ |
| 363 | 363 | private static function removeList($queue) |
| 364 | 364 | { |
| 365 | - $counter = self::size($queue); |
|
| 366 | - $result = self::redis()->del('queue:' . $queue); |
|
| 367 | - return ($result == 1) ? $counter : 0; |
|
| 365 | + $counter = self::size($queue); |
|
| 366 | + $result = self::redis()->del('queue:' . $queue); |
|
| 367 | + return ($result == 1) ? $counter : 0; |
|
| 368 | 368 | } |
| 369 | 369 | |
| 370 | 370 | /* |
@@ -227,8 +227,7 @@ |
||
| 227 | 227 | ); |
| 228 | 228 | try { |
| 229 | 229 | Resque_Event::trigger('beforeEnqueue', $hookParams); |
| 230 | - } |
|
| 231 | - catch(Resque_Job_DontCreate $e) { |
|
| 230 | + } catch(Resque_Job_DontCreate $e) { |
|
| 232 | 231 | return false; |
| 233 | 232 | } |
| 234 | 233 | |
@@ -169,9 +169,9 @@ discard block |
||
| 169 | 169 | return $this->instance; |
| 170 | 170 | } |
| 171 | 171 | |
| 172 | - $this->instance = $this->getJobFactory()->create($this->payload['class'], $this->getArguments(), $this->queue); |
|
| 173 | - $this->instance->job = $this; |
|
| 174 | - return $this->instance; |
|
| 172 | + $this->instance = $this->getJobFactory()->create($this->payload['class'], $this->getArguments(), $this->queue); |
|
| 173 | + $this->instance->job = $this; |
|
| 174 | + return $this->instance; |
|
| 175 | 175 | } |
| 176 | 176 | |
| 177 | 177 | /** |
@@ -276,14 +276,14 @@ discard block |
||
| 276 | 276 | return $this; |
| 277 | 277 | } |
| 278 | 278 | |
| 279 | - /** |
|
| 280 | - * @return Resque_Job_FactoryInterface |
|
| 281 | - */ |
|
| 282 | - public function getJobFactory() |
|
| 283 | - { |
|
| 284 | - if ($this->jobFactory === null) { |
|
| 285 | - $this->jobFactory = new Resque_Job_Factory(); |
|
| 286 | - } |
|
| 287 | - return $this->jobFactory; |
|
| 288 | - } |
|
| 279 | + /** |
|
| 280 | + * @return Resque_Job_FactoryInterface |
|
| 281 | + */ |
|
| 282 | + public function getJobFactory() |
|
| 283 | + { |
|
| 284 | + if ($this->jobFactory === null) { |
|
| 285 | + $this->jobFactory = new Resque_Job_Factory(); |
|
| 286 | + } |
|
| 287 | + return $this->jobFactory; |
|
| 288 | + } |
|
| 289 | 289 | } |
@@ -12,8 +12,8 @@ discard block |
||
| 12 | 12 | class Resque_Worker |
| 13 | 13 | { |
| 14 | 14 | /** |
| 15 | - * @var LoggerInterface Logging object that impliments the PSR-3 LoggerInterface |
|
| 16 | - */ |
|
| 15 | + * @var LoggerInterface Logging object that impliments the PSR-3 LoggerInterface |
|
| 16 | + */ |
|
| 17 | 17 | public $logger; |
| 18 | 18 | |
| 19 | 19 | /** |
@@ -56,30 +56,30 @@ discard block |
||
| 56 | 56 | */ |
| 57 | 57 | private $child = null; |
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * Instantiate a new worker, given a list of queues that it should be working |
|
| 61 | - * on. The list of queues should be supplied in the priority that they should |
|
| 62 | - * be checked for jobs (first come, first served) |
|
| 63 | - * |
|
| 64 | - * Passing a single '*' allows the worker to work on all queues in alphabetical |
|
| 65 | - * order. You can easily add new queues dynamically and have them worked on using |
|
| 66 | - * this method. |
|
| 67 | - * |
|
| 68 | - * @param string|array $queues String with a single queue name, array with multiple. |
|
| 69 | - */ |
|
| 70 | - public function __construct($queues) |
|
| 71 | - { |
|
| 72 | - $this->logger = new Resque_Log(); |
|
| 73 | - |
|
| 74 | - if(!is_array($queues)) { |
|
| 75 | - $queues = array($queues); |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - $this->queues = $queues; |
|
| 79 | - $this->hostname = php_uname('n'); |
|
| 80 | - |
|
| 81 | - $this->id = $this->hostname . ':'.getmypid() . ':' . implode(',', $this->queues); |
|
| 82 | - } |
|
| 59 | + /** |
|
| 60 | + * Instantiate a new worker, given a list of queues that it should be working |
|
| 61 | + * on. The list of queues should be supplied in the priority that they should |
|
| 62 | + * be checked for jobs (first come, first served) |
|
| 63 | + * |
|
| 64 | + * Passing a single '*' allows the worker to work on all queues in alphabetical |
|
| 65 | + * order. You can easily add new queues dynamically and have them worked on using |
|
| 66 | + * this method. |
|
| 67 | + * |
|
| 68 | + * @param string|array $queues String with a single queue name, array with multiple. |
|
| 69 | + */ |
|
| 70 | + public function __construct($queues) |
|
| 71 | + { |
|
| 72 | + $this->logger = new Resque_Log(); |
|
| 73 | + |
|
| 74 | + if(!is_array($queues)) { |
|
| 75 | + $queues = array($queues); |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + $this->queues = $queues; |
|
| 79 | + $this->hostname = php_uname('n'); |
|
| 80 | + |
|
| 81 | + $this->id = $this->hostname . ':'.getmypid() . ':' . implode(',', $this->queues); |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | 84 | /** |
| 85 | 85 | * Return all workers known to Resque as instantiated instances. |
@@ -182,8 +182,7 @@ discard block |
||
| 182 | 182 | $this->logger->log(Psr\Log\LogLevel::INFO, 'Sleeping for {interval}', array('interval' => $interval)); |
| 183 | 183 | if($this->paused) { |
| 184 | 184 | $this->updateProcLine('Paused'); |
| 185 | - } |
|
| 186 | - else { |
|
| 185 | + } else { |
|
| 187 | 186 | $this->updateProcLine('Waiting for ' . implode(',', $this->queues)); |
| 188 | 187 | } |
| 189 | 188 | |
@@ -225,8 +224,7 @@ discard block |
||
| 225 | 224 | $job->fail(new Resque_Job_DirtyExitException( |
| 226 | 225 | 'Job exited with exit code ' . $exitStatus |
| 227 | 226 | )); |
| 228 | - } |
|
| 229 | - else |
|
| 227 | + } else |
|
| 230 | 228 | { |
| 231 | 229 | if (in_array($job->getStatus(), array(Resque_Job_Status::STATUS_WAITING, Resque_Job_Status::STATUS_RUNNING))) |
| 232 | 230 | { |
@@ -253,8 +251,7 @@ discard block |
||
| 253 | 251 | try { |
| 254 | 252 | Resque_Event::trigger('afterFork', $job); |
| 255 | 253 | $job->perform(); |
| 256 | - } |
|
| 257 | - catch(Exception $e) { |
|
| 254 | + } catch(Exception $e) { |
|
| 258 | 255 | $this->logger->log(Psr\Log\LogLevel::CRITICAL, '{job} has failed {stack}', array('job' => $job, 'stack' => $e)); |
| 259 | 256 | $job->fail($e); |
| 260 | 257 | return; |
@@ -347,8 +344,7 @@ discard block |
||
| 347 | 344 | $processTitle = 'resque-' . Resque::VERSION . ': ' . $status; |
| 348 | 345 | if(function_exists('cli_set_process_title') && PHP_OS !== 'Darwin') { |
| 349 | 346 | cli_set_process_title($processTitle); |
| 350 | - } |
|
| 351 | - else if(function_exists('setproctitle')) { |
|
| 347 | + } else if(function_exists('setproctitle')) { |
|
| 352 | 348 | setproctitle($processTitle); |
| 353 | 349 | } |
| 354 | 350 | } |
@@ -431,8 +427,7 @@ discard block |
||
| 431 | 427 | $this->logger->log(Psr\Log\LogLevel::DEBUG, 'Child {child} found, killing.', array('child' => $this->child)); |
| 432 | 428 | posix_kill($this->child, SIGKILL); |
| 433 | 429 | $this->child = null; |
| 434 | - } |
|
| 435 | - else { |
|
| 430 | + } else { |
|
| 436 | 431 | $this->logger->log(Psr\Log\LogLevel::INFO, 'Child {child} not found, restarting.', array('child' => $this->child)); |
| 437 | 432 | $this->shutdown(); |
| 438 | 433 | } |
@@ -554,8 +549,7 @@ discard block |
||
| 554 | 549 | $job = Resque::redis()->get('worker:' . $this); |
| 555 | 550 | if(!$job) { |
| 556 | 551 | return array(); |
| 557 | - } |
|
| 558 | - else { |
|
| 552 | + } else { |
|
| 559 | 553 | return json_decode($job, true); |
| 560 | 554 | } |
| 561 | 555 | } |
@@ -3,30 +3,30 @@ |
||
| 3 | 3 | class Resque_Job_Factory implements Resque_Job_FactoryInterface |
| 4 | 4 | { |
| 5 | 5 | |
| 6 | - /** |
|
| 7 | - * @param $className |
|
| 8 | - * @param array $args |
|
| 9 | - * @param $queue |
|
| 10 | - * @return Resque_JobInterface |
|
| 11 | - * @throws \Resque_Exception |
|
| 12 | - */ |
|
| 13 | - public function create($className, $args, $queue) |
|
| 14 | - { |
|
| 15 | - if (!class_exists($className)) { |
|
| 16 | - throw new Resque_Exception( |
|
| 17 | - 'Could not find job class ' . $className . '.' |
|
| 18 | - ); |
|
| 19 | - } |
|
| 6 | + /** |
|
| 7 | + * @param $className |
|
| 8 | + * @param array $args |
|
| 9 | + * @param $queue |
|
| 10 | + * @return Resque_JobInterface |
|
| 11 | + * @throws \Resque_Exception |
|
| 12 | + */ |
|
| 13 | + public function create($className, $args, $queue) |
|
| 14 | + { |
|
| 15 | + if (!class_exists($className)) { |
|
| 16 | + throw new Resque_Exception( |
|
| 17 | + 'Could not find job class ' . $className . '.' |
|
| 18 | + ); |
|
| 19 | + } |
|
| 20 | 20 | |
| 21 | - if (!method_exists($className, 'perform')) { |
|
| 22 | - throw new Resque_Exception( |
|
| 23 | - 'Job class ' . $className . ' does not contain a perform method.' |
|
| 24 | - ); |
|
| 25 | - } |
|
| 21 | + if (!method_exists($className, 'perform')) { |
|
| 22 | + throw new Resque_Exception( |
|
| 23 | + 'Job class ' . $className . ' does not contain a perform method.' |
|
| 24 | + ); |
|
| 25 | + } |
|
| 26 | 26 | |
| 27 | - $instance = new $className; |
|
| 28 | - $instance->args = $args; |
|
| 29 | - $instance->queue = $queue; |
|
| 30 | - return $instance; |
|
| 31 | - } |
|
| 27 | + $instance = new $className; |
|
| 28 | + $instance->args = $args; |
|
| 29 | + $instance->queue = $queue; |
|
| 30 | + return $instance; |
|
| 31 | + } |
|
| 32 | 32 | } |
@@ -98,10 +98,10 @@ discard block |
||
| 98 | 98 | */ |
| 99 | 99 | public static function prefix($namespace) |
| 100 | 100 | { |
| 101 | - if (substr($namespace, -1) !== ':' && $namespace != '') { |
|
| 102 | - $namespace .= ':'; |
|
| 103 | - } |
|
| 104 | - self::$defaultNamespace = $namespace; |
|
| 101 | + if (substr($namespace, -1) !== ':' && $namespace != '') { |
|
| 102 | + $namespace .= ':'; |
|
| 103 | + } |
|
| 104 | + self::$defaultNamespace = $namespace; |
|
| 105 | 105 | } |
| 106 | 106 | |
| 107 | 107 | /** |
@@ -110,7 +110,7 @@ discard block |
||
| 110 | 110 | * DSN-supplied value will be used instead and this parameter is ignored. |
| 111 | 111 | * @param object $client Optional Credis_Cluster or Credis_Client instance instantiated by you |
| 112 | 112 | */ |
| 113 | - public function __construct($server, $database = null, $client = null) |
|
| 113 | + public function __construct($server, $database = null, $client = null) |
|
| 114 | 114 | { |
| 115 | 115 | try { |
| 116 | 116 | if (is_array($server)) { |
@@ -255,16 +255,16 @@ discard block |
||
| 255 | 255 | |
| 256 | 256 | public static function getPrefix() |
| 257 | 257 | { |
| 258 | - return self::$defaultNamespace; |
|
| 258 | + return self::$defaultNamespace; |
|
| 259 | 259 | } |
| 260 | 260 | |
| 261 | 261 | public static function removePrefix($string) |
| 262 | 262 | { |
| 263 | - $prefix=self::getPrefix(); |
|
| 263 | + $prefix=self::getPrefix(); |
|
| 264 | 264 | |
| 265 | - if (substr($string, 0, strlen($prefix)) == $prefix) { |
|
| 266 | - $string = substr($string, strlen($prefix), strlen($string) ); |
|
| 267 | - } |
|
| 268 | - return $string; |
|
| 265 | + if (substr($string, 0, strlen($prefix)) == $prefix) { |
|
| 266 | + $string = substr($string, strlen($prefix), strlen($string) ); |
|
| 267 | + } |
|
| 268 | + return $string; |
|
| 269 | 269 | } |
| 270 | 270 | } |
@@ -115,11 +115,9 @@ discard block |
||
| 115 | 115 | try { |
| 116 | 116 | if (is_array($server)) { |
| 117 | 117 | $this->driver = new Credis_Cluster($server); |
| 118 | - } |
|
| 119 | - else if (is_object($client)) { |
|
| 118 | + } else if (is_object($client)) { |
|
| 120 | 119 | $this->driver = $client; |
| 121 | - } |
|
| 122 | - else { |
|
| 120 | + } else { |
|
| 123 | 121 | list($host, $port, $dsnDatabase, $user, $password, $options) = self::parseDsn($server); |
| 124 | 122 | // $user is not used, only $password |
| 125 | 123 | |
@@ -144,8 +142,7 @@ discard block |
||
| 144 | 142 | if ($database !== null) { |
| 145 | 143 | $this->driver->select($database); |
| 146 | 144 | } |
| 147 | - } |
|
| 148 | - catch(CredisException $e) { |
|
| 145 | + } catch(CredisException $e) { |
|
| 149 | 146 | throw new Resque_RedisException('Error communicating with Redis: ' . $e->getMessage(), 0, $e); |
| 150 | 147 | } |
| 151 | 148 | } |
@@ -240,15 +237,13 @@ discard block |
||
| 240 | 237 | foreach ($args[0] AS $i => $v) { |
| 241 | 238 | $args[0][$i] = self::$defaultNamespace . $v; |
| 242 | 239 | } |
| 243 | - } |
|
| 244 | - else { |
|
| 240 | + } else { |
|
| 245 | 241 | $args[0] = self::$defaultNamespace . $args[0]; |
| 246 | 242 | } |
| 247 | 243 | } |
| 248 | 244 | try { |
| 249 | 245 | return $this->driver->__call($name, $args); |
| 250 | - } |
|
| 251 | - catch (CredisException $e) { |
|
| 246 | + } catch (CredisException $e) { |
|
| 252 | 247 | throw new Resque_RedisException('Error communicating with Redis: ' . $e->getMessage(), 0, $e); |
| 253 | 248 | } |
| 254 | 249 | } |