Yelp/python-gearman — pull request #16 by rhettg (2012-01-18 06:57:49+00:00)
Title: after_job() callback

I have a worker that I want to exit periodically.

Seemingly the way to do that would be to redefine after_poll() to check if the worker has been running too long, then return False so the worker will exit.

This has a subtle bug however. If the send_complete message has not yet been sent back to the server, the worker may exit before reporting the job's success or failure.

This pull request adds after_job(), which is called each time a job is no longer being handled, providing a much more useful hook for controlling the work loop.

My worker then does something like:

def after_job(self):
if time.time() - self.start_time > 10:
log.info("Running for %d seconds, exiting", time.time() - self.start_time)
return False

return True

There are no tests included. As far as I can tell (by inserting 'raise Exception()', that loop isn't tested anyway.