a way to cancel a job?

33 views
Skip to first unread message

Kumar McMillan

unread,
Nov 2, 2009, 1:47:22 PM11/2/09
to gea...@googlegroups.com
Hi, I am new to Gearman. I was looking through the protocol to get an
idea for what requests can be sent to workers,
http://gearman.org/index.php?id=protocol I don't see a way to tell a
job to CANCEL itself, am I missing something?

In other words, we have a web front-end where a user will initiate a
long-running process. She might want to press a button to cancel that
process before it has finished. It seems like Gearman would be a good
way to manage this as a background task but is there a way to easily
send a cancel messasge to the worker? I can't think of way that does
not involve hackery to get the PID of the worker.

thanks, Kumar

Eric Day

unread,
Nov 2, 2009, 2:20:38 PM11/2/09
to gea...@googlegroups.com
Hi Kumar,

With foreground job requests, a server will automatically remove queued
jobs when the client disconnects. If the worker is already running,
then it will run through to completion.

The ability to cancel a job that is already running on a worker is
not something that can be controlled with just the protocol, this
is very dependent on your worker code. If your worker is sitting
in a tight loop or a blocking function call, there is no chance
of reading a cancel request from a socket unless your workers were
multi-threaded. The application would need to yield periodically to
see if it should stop. There are a few ways you could handle this,
using Gearman to coordinate those messages.

You could have an external process listening for jobs that should be
killed (pids), and the client could send a kill request to this other
worker with PID when the abandoned worker should die. This means your
application workers will need to send their PID back to the client
before beginning the real work.

Another method would be making your worker listen on another unique
function name, and in your application code you can grab a real work
item, and them periodically check this other worker instance to see
if a kill request has come in. If your application blocks or cannot
yield, this is obviously not an option.

There are probably other clever ways to handle this, but I would think
about how to use Gearman to also coordinate those cancel requests.

-Eric

Kumar McMillan

unread,
Nov 2, 2009, 3:04:20 PM11/2/09
to gea...@googlegroups.com
Hi Eric. Thanks, that makes sense. A multi-threaded worker
supervisor would be useful for my application anyway since we could
control the number of threads to listen for work on (instead of
running multiple workers). As you say, adding another thread to
listen for cancel requests seems easy to do.
Reply all
Reply to author
Forward
0 new messages