Last Updated: February 25, 2016
·
3.414K
· MarcQualie

Drain a Gearman Job Queue

Many times in the past during development I've accidentally added a lot more items to a queue then I meant to. Either by creating an infinite loop of queue processing or running a script with way more data than I expected.

In complex systems there are multiple queues, which you might want to keep processing as normal so simply restarting the Gearman instance isn't ideal. Also, persistence could/should be enabled which would simply repopulate the huge queue.

With this in mind, I created a small snippet that I use regularly to quickly flush out a single function/queue. This idea was inspired by a code snippet I found somewhere (I can't remember where, it was a long time ago) and I simply adapted it into a function.

Put the following code inside your bashrc or zshrc file.

function drain_gearman() {
    gearman -t 1000 -n -w -f $1 > /dev/null
}

Then you can simply use it by using the following

drain_gearman queue_name

I hope this saves someone a lot of time and hassle.