Author: Michael Haynes (mike@mjphaynes.com)
Example #1
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $queues = Resque\Redis::instance()->smembers('queues');
     if (empty($queues)) {
         $this->log('<warn>There are no queues.</warn>');
         return;
     }
     $table = new Resque\Helpers\Table($this);
     $table->setHeaders(array('#', 'Name', 'Queued', 'Delayed', 'Processed', 'Failed', 'Cancelled', 'Total'));
     foreach ($queues as $i => $queue) {
         $stats = Resque\Redis::instance()->hgetall(Resque\Queue::redisKey($queue, 'stats'));
         $table->addRow(array($i + 1, $queue, (int) @$stats['queued'], (int) @$stats['delayed'], (int) @$stats['processed'], (int) @$stats['failed'], (int) @$stats['cancelled'], (int) @$stats['total']));
     }
     $this->log((string) $table);
 }
Example #2
0
 /**
  * Look for any jobs which are running but the worker is dead.
  * Meaning that they are also not running but left in limbo
  *
  * This is a form of garbage collection to handle cases where the
  * server may have been killed and the workers did not die gracefully
  * and therefore leave state information in Redis.
  *
  * @param array $queues list of queues to check
  */
 public static function cleanup(array $queues = array('*'))
 {
     $cleaned = array('zombie' => 0, 'processed' => 0);
     $redis = Redis::instance();
     if (in_array('*', $queues)) {
         $queues = (array) $redis->smembers(Queue::redisKey());
         sort($queues);
     }
     $workers = $redis->smembers(Worker::redisKey());
     foreach ($queues as $queue) {
         $jobs = $redis->zrangebyscore(Queue::redisKey($queue, 'running'), 0, time());
         foreach ($jobs as $payload) {
             $job = self::loadPayload($queue, $payload);
             $packet = $job->getPacket();
             if (!in_array($packet['worker'], $workers)) {
                 $job->fail(new Exception\Zombie());
                 $cleaned['zombie']++;
             }
         }
         $cleaned['processed'] = $redis->zremrangebyscore(Queue::redisKey($queue, 'processed'), 0, time() - \Resque::getConfig('default.expiry_time', \Resque::DEFAULT_EXPIRY_TIME));
     }
     return $cleaned;
 }
Example #3
0
 /**
  * Find any delayed jobs and add them to the queue if found
  *
  * @param int $endTime optional end time for range
  * @param int $startTime optional start time for range
  */
 public function queueDelayed($endTime = null, $startTime = 0)
 {
     $startTime = $startTime ?: 0;
     $endTime = $endTime ?: time();
     foreach ($this->resolveQueues() as $queue) {
         $this->redis->multi();
         $jobs = $this->redis->zrangebyscore(Queue::redisKey($queue, 'delayed'), $startTime, $endTime);
         $this->redis->zremrangebyscore(Queue::redisKey($queue, 'delayed'), $startTime, $endTime);
         list($jobs, $found) = $this->redis->exec();
         if ($found > 0) {
             foreach ($jobs as $payload) {
                 $job = Job::loadPayload($queue, $payload);
                 $job->setWorker($this);
                 if (Event::fire(Event::JOB_QUEUE_DELAYED, $job) !== false) {
                     $job->queue();
                     Event::fire(Event::JOB_QUEUED_DELAYED, $job);
                 }
             }
             Stats::decr('delayed', $found);
             Stats::decr('delayed', $found, Queue::redisKey($queue, 'stats'));
             $this->log('Added <pop>' . $found . '</pop> delayed job' . ($found == 1 ? '' : 's') . ' to <pop>' . $queue . '</pop> queue', Logger::NOTICE);
         }
     }
 }