Yelp / python-gearman

Gearman API - Client, worker, and admin client interfaces
http://github.com/Yelp/python-gearman/
Other
242 stars 122 forks source link

after_job() callback #16

Closed rhettg closed 12 years ago

rhettg commented 12 years ago

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.

rhettg commented 12 years ago

I've also added to this a pull request a patch that fixes unicode handling.

Previously, if you had a task name that was a unicode string (even if it was ascii encodable), it would fail with a pretty cryptic comment. This bug as wasted my time on two occasions, since I forgot the solution after the first time. Now we have a fix!

eskil commented 12 years ago

But I'll happily take the unicode request, are you too busy with "work" to make it a separate pull request ?

rhettg commented 12 years ago

Closing in preference of #21 and #22