Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stopping gearman workers nicely

Tags:

php

gearman

I have a number of Gearman workers running constantly, saving things like records of user page views, etc. Occasionally, I'll update the PHP code that is used by the Gearman workers. In order to get the workers to switch to the new code, I the kill and restart the PHP processes for the workers.

What is a better way to do this? Presumably, I'm sometime losing data (albeit not very important data) when I kill one of those worker processes.

Edit: I found an answer that works for me, and posted it below.

like image 442
Karptonite Avatar asked Feb 16 '10 02:02

Karptonite


2 Answers

Solution 1


Generally I run my workers with the unix daemon utility with the -r flag and let them expire after one job. Your script will end gracefully after each iteration and daemon will restart automatically.

Your workers will be stale for one job but that may not be as big a deal to you as losing data

This solution also has the advantage of freeing up memory. You may run into problems with memory if you're doing large jobs as PHP pre 5.3 has god awful GC.

Solution 2


You could also add a quit function to all of your workers that exits the script. When you'd like to restart you simply give gearman calls to quit with a high priority.

like image 96
bnmrrs Avatar answered Sep 21 '22 00:09

bnmrrs


function AutoRestart() {    static $startTime = time();     if (filemtime(__FILE__) > $startTime) {       exit();    } }  AutoRestart();   
like image 35
Orwellophile Avatar answered Sep 22 '22 00:09

Orwellophile