Skip to content

Instantly share code, notes, and snippets.

@kingpabel
Created December 18, 2016 12:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kingpabel/8e1f9f67ed21f14ceda7f142ea49a7c8 to your computer and use it in GitHub Desktop.
Save kingpabel/8e1f9f67ed21f14ceda7f142ea49a7c8 to your computer and use it in GitHub Desktop.
Laravel 5.1 compatible command which allows to clear Beanstalkd queue.
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Queue\Connectors\BeanstalkdConnector;
use Pheanstalk\Pheanstalk;
class ClearBeanstalkdQueue extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'queue:beanstalkd:clear {queue?}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Clear Beanstalkd queue.';
/**
* @param BeanstalkdConnector $connector
*/
public function handle(BeanstalkdConnector $connector)
{
$config = config('queue.connections.beanstalkd');
/** @type Pheanstalk $pheanstalk */
$pheanstalk = $connector->connect($config)->getPheanstalk();
$queue = $this->argument('queue') ?: $config['queue'];
$this->info("Clearing queue: {$queue}");
$pheanstalk->useTube($queue);
$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