Ok, I posted this question, now I think I found a good answer for it.
If you look in the code for Net_Gearman_Worker, you will find that the stopWork function is tracked in the work cycle, and if it returns true, it exits the function.
I have done the following:
Using memcache, I created a cached value, gearman_restarttime, and I use a separate script to set it to the current timestamp whenever I update the site. (I used Memcache, but it could be stored anywhere - a database, a file, or something else).
I extended the Worker class, essentially Net_Gearman_Worker_Foo, and all my workers created this. In the Foo class, I flipped the stopWork function to do the following: first, it checks gearman_restarttime; for the first time, it stores the value in a global variable. From now on, each time comparing the cached value with the global one. If it has changed, stopWork returns true, and the worker exits. Cron checks every minute to make sure all workers are still running, and restarts any remaining workers.
It might be worth setting a timer in stopWork and checking the cache only once every x minutes. In our case, Memcache is fast enough that checking the value every time does not seem to be a problem, but if you use some other system to store the current timestamp, the check will be less common.
Karptonite Jun 09 '10 at 19:33 2010-06-09 19:33
source share