14

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.

stofl
  • 2,950
  • 6
  • 35
  • 48
  • 5
    We have already enough unemployment, thank you very much! `` – Pekka Aug 16 '11 at 12:38
  • Wouldnt it make more sense to mock the Gearman Service instead of relying on a real physical server running? – Gordon Aug 16 '11 at 13:01
  • Yes I am also doing that. But beside the main application there are small scripts that are executed via HTTP that are very slim and didn't include anything. They only call the GearmanClient to run a job in the background and then they are gone. I am testing them with cURL. – stofl Aug 16 '11 at 13:42

2 Answers2

33

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)

Artur Bodera
  • 1,662
  • 22
  • 20
  • Thank you. Is it also possible to remove all jobs (FUNCTION_NAME = *)? – stofl Jan 17 '12 at 16:52
  • Here you go. Added recipe to remove all jobs. – Artur Bodera Jan 18 '12 at 18:54
  • The shell script doesn't work for me. The second seams to filter out everything. But I do need this within php test cases and I will write a script there to get all jobs and to start some processes to eat up all those jobs. Thank you! – stofl Jan 20 '12 at 13:16
  • @ArturBodera is it possible to delete jobs from remote server? (to call gearman -n -w -f ${MATCH%\,*} -c ${MATCH#*\,} > /dev/null;) on other ip? – Eyal Ch Oct 30 '16 at 17:19
  • Sure, just use `-h` for remote host and `-p` for port. More [info in the docs](http://gearman.info/bin/gearman.html). – Artur Bodera Dec 05 '16 at 09:56
0

Using Persisten Queues it's easy, just truncate the queue table.

Rafael Kassner
  • 1,123
  • 6
  • 21
  • How are the jobs stored by default? In the memory I think, right? – stofl Sep 02 '11 at 07:34
  • In memory, I think so. In an old school way, you can restart Gearman service. Googling, I can't found a better solution, also in Gearman website. What type of processes are you doing with gearman? Maybe you can implement persistent queues and work with them. – Rafael Kassner Sep 09 '11 at 14:53
  • 1
    This won't work unless you restart gearmand after truncating the table – Tony Sep 23 '14 at 22:01