Resque\Job::cleanup PHP Method

cleanup() public static method

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.
public static cleanup ( array $queues = ['*'] )
$queues array 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;
    }

Usage Example

Esempio n. 1
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $host = new Resque\Host();
     $cleaned_hosts = $host->cleanup();
     $worker = new Resque\Worker('*');
     $cleaned_workers = $worker->cleanup();
     $cleaned_hosts = array_merge_recursive($cleaned_hosts, $host->cleanup());
     $cleaned_jobs = Resque\Job::cleanup();
     $this->log('Cleaned hosts: <pop>' . json_encode($cleaned_hosts['hosts']) . '</pop>');
     $this->log('Cleaned workers: <pop>' . json_encode(array_merge($cleaned_hosts['workers'], $cleaned_workers)) . '</pop>');
     $this->log('Cleaned <pop>' . $cleaned_jobs['zombie'] . '</pop> zombie job' . ($cleaned_jobs['zombie'] == 1 ? '' : 's'));
     $this->log('Cleared <pop>' . $cleaned_jobs['processed'] . '</pop> processed job' . ($cleaned_jobs['processed'] == 1 ? '' : 's'));
 }
All Usage Examples Of Resque\Job::cleanup