Remove all Gearman jobs from the Gearman Job Server

stofl picture stofl · Aug 16, 2011 · Viewed 17.4k times · Source

Is there a way to remove all Gearman jobs from the Gearman Job Server? I have a PHP application that runs Gearman jobs in the background. For my unit tests I need to ensure that a) there are no jobs waiting for a worker that executes it and b) that there is no worker working. The latter is not that important because it is easy to kill the workers but the former--I don't know how to implement this.

Answer

Artur Bodera picture Artur Bodera · Jan 13, 2012

Removing all jobs of single type

Use the following command (just replace FUNCTION_NAME):

gearman -n -w -f FUNCTION_NAME > /dev/null

To remove only n number of jobs add -c n param, i.e. to remove 20 jobs:

gearman -c 20 -n -w -f FUNCTION_NAME > /dev/null


Removing all jobs from server

If you want to remove all jobs, run the following loop:

for MATCH in $(echo status | nc 127.0.0.1 4730 | grep -v \\. |  grep -Pv '^[^\t]+\t0\t' | cut -s -f 1-2 --output-delimiter=\,); 
do 
    gearman -n -w -f ${MATCH%\,*} -c ${MATCH#*\,} > /dev/null;
done

Replace 127.0.0.1 4730 with your gearmand server address and port.

ps: removing jobs from persistent storage (i.e. sqlite) will not remove them from gearmand until it is restarted (because gearmand process has in-memory cache)