Queueing is a natural part of modern web applications. Without it, you will end up with a poor user experience due to unnecessary execution blocking leading to inadequate response times. In Laravel (which everyone is using, of course), one of the easiest providers to get going with queues is beanstalkd. You can set it up in 15 seconds on any linux distro – and away you go without any configuration necessary. However when you need to debug and investigate, it isn’t really clear how to proceed. As such, I wanted to quickly go over some tools that I’ve discovered which are very helpful.
CLI based tube inspection
If all you have is a CLI, and you want a hassle-free way to check your tubes, I highly recommend beanstool. With no setup necessary, it will quickly give you all the info you need.
Web based tube inspector
If you need a GUI and/or more detail, then beanstalk console has you covered. It’s a very simple and lightweight PHP application which will give you plenty of info, and allow you to quickly and efficiently go through your tubes if they are clogged with several jobs.
Clear a queue in Laravel
This is perhaps the single most useful one. The scenario is that you have hundreds or thousands of faulty jobs stuck in your queue, and you want to quickly clear all of them. Amazingly, there is no trivial out-of-the-box way to do that, so I’ve come up with the following Laravel Command to get that job done.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
<?php namespace Project\Commands; use Illuminate\Console\Command; use Symfony\Component\Console\Input\InputArgument; class ClearQueue extends Command { /** * The console command name. * * @var string */ protected $name = 'queue:clear'; /** * The console command description. * * @var string */ protected $description = 'Clear a Beanstalkd queue, by deleting all pending jobs.'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Defines the arguments. * * @return array */ public function getArguments() { return array( array('queue', InputArgument::OPTIONAL, 'The name of the queue to clear.'), ); } /** * Execute the console command. * * @return void */ public function fire() { $queue = ($this->argument('queue')) ? $this->argument('queue') : \Config::get('queue.connections.beanstalkd.queue'); $this->info(sprintf('Clearing queue: %s', $queue)); $pheanstalk = \Queue::getPheanstalk(); $pheanstalk->useTube($queue); $pheanstalk->watch($queue); while ($job = $pheanstalk->reserve(0)) { $pheanstalk->delete($job); } $this->info('...cleared.'); } } |