Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Gearman Workers in the Background

Tags:

php

gearman

I'm using Ubuntu Natty with PHP 5.3.8. I just got Gearman working on my server.

I did a few tests with some scripts I got off the PHP Manual, and everything works ok.

However, I'd like to know if there's a way I can run the worker in the background, and also monitor it so that when I move to a multi-worker situation, I can keep track of just how many workers I've got working.

Usually, I would have to open two terminals, one for the worker and one for the client. The one for the worker becomes 'stuck' in effect after the php script is executed.

Thanks in advance.

like image 805
ObiHill Avatar asked Nov 21 '11 20:11

ObiHill


2 Answers

Ok. I found a solution to my earlier problem. It involves using Supervisord to deamonize the gearman worker(s).

Here's how to install Supervisord

apt-get install python-setuptools
easy_install supervisor
echo_supervisord_conf > /etc/supervisord.conf

Here's how to configure it (first get an init script from here and save it to /etc/init.d/supervisord), then do the following:

chmod +x /etc/init.d/supervisord
update-rc.d -f supervisord defaults

You then need to update your supervisor.conf file to tell supervisord which command you want to run as a daemon (of course, this would be your gearman worker). Below is just a sample of what you would add to your supervisord.conf file, you'll need to update it to your own specific situation.

[program:gearman]
command=/usr/bin/php php_gearman_worker.php
numprocs=1
directory=/root/gearman
stdout_logfile=/root/gearman/supervisord.log
environment=GEARMAN_USER=gearman
autostart=true
autorestart=true
user=gearman
stopsignal=KILL

When you're done, then start supervisord i.e. /etc/init.d/supervisord start.

Once you do this, your Gearman worker is now active and you can now run your gearman client via the command line or with your browser.

I hope this helps.

like image 156
ObiHill Avatar answered Nov 03 '22 06:11

ObiHill


For background workers you can use GearmanManager by Brian Moon https://github.com/brianlmoon/GearmanManager

like image 26
Bullet Avatar answered Nov 03 '22 07:11

Bullet