Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save k98kurz/932bf26635364f98993a to your computer and use it in GitHub Desktop.
Save k98kurz/932bf26635364f98993a to your computer and use it in GitHub Desktop.
Clear a Beanstalkd Queue in Laravel
<?php
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
class ClearBeanstalkdQueueCommand extends Command {
/**
* The console command name.
*
* @var string
*/
protected $name = 'queue:beanstalkd:clear';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Clear a Beanstalkd queue by deleting all pending jobs or deleting a specified job.';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Defines the arguments.
*
* @return array
*/
public function getArguments()
{
return array(
['queue', InputArgument::OPTIONAL, 'The name of the queue to clear.'],
['jobid', InputArgument::OPTIONAL, 'The id of the job to delete.']
);
}
/**
* Execute the console command.
*
* @return void
*/
public function fire()
{
$queue = ($this->argument('queue')) ? $this->argument('queue') : Config::get('queue.connections.beanstalkd.queue');
$jobid = ($this->argument('jobid')) ? $this->argument('jobid') : -1;
$this->info(sprintf('Clearing queue: %s', $queue));
$pheanstalk = Queue::getPheanstalk();
$pheanstalk->useTube($queue);
if ($jobid > -1) {
$job = $pheanstalk->peek($jobid);
$pheanstalk->delete($job);
$this->info('...job with id '.$jobid.' deleted.');
} else {
$pheanstalk->watch($queue);
while ($job = $pheanstalk->reserve(0)) {
$pheanstalk->delete($job);
}
$this->info('...cleared.');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment