Skip to content

Commit

Permalink
Add support for clearing reserved jobs, and delayed jobs on redis
Browse files Browse the repository at this point in the history
  • Loading branch information
morrislaptop authored Aug 5, 2019
1 parent ea814b3 commit 58c8b64
Showing 1 changed file with 57 additions and 28 deletions.
85 changes: 57 additions & 28 deletions src/Clearer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,62 @@

class Clearer implements ClearerContract
{
/**
* @var QueueManager
*/
protected $manager;

/**
* {@inheritDoc}
*/
function __construct(FactoryContract $manager)
{
$this->manager = $manager;
}

/**
* {@inheritDoc}
*/
public function clear($connection, $queue)
{
$count = 0;
$connection = $this->manager->connection($connection);

while ($job = $connection->pop($queue)) {
$job->delete();
$count++;
}

return $count;
}
/**
* @var QueueManager
*/
protected $manager;

/**
* {@inheritDoc}
*/
function __construct(FactoryContract $manager)
{
$this->manager = $manager;
}

/**
* {@inheritDoc}
*/
public function clear($connection, $queue)
{
$count = 0;
$connection = $this->manager->connection($connection);

$count += $this->clearJobs($connection, $queue);
$count += $this->clearJobs($connection, $queue . ':reserved');
$count += $this->clearDelayedJobs($connection, $queue);

return $count;
}

protected function clearJobs($connection, $queue)
{
$count = 0;

while ($job = $connection->pop($queue)) {
$job->delete();
$count++;
}

return $count;
}

protected function clearDelayedJobs($connection, $queue)
{
if (method_exists($connection, 'getRedis')) {
return $this->clearDelayedJobsOnRedis($connection, $queue);
}

throw new \InvalidArgumentException('Queue Connection not supported');
}

protected function clearDelayedJobsOnRedis($connection, $queue) {
$key = "queues:{$queue}:delayed";
$redis = $connection->getRedis()->connection(config('queue.connections.redis.connection'));
$count = $redis->zcount($key, '-inf', '+inf');
$redis->del($key);

return $count;
}

}

0 comments on commit 58c8b64

Please sign in to comment.