2. What is Gearman?
Generic
Application Framework
Job Server
Client / Worker API
Multi-platform and multi-language API
support
Support priorities
7. Gearman server installation
tips
Don’t
use Ubuntu package for gearman
server (outdated and doesn’t work with
current PHP PECL extension)
Build it from source:
https://launchpad.net/gearmand
Run: gearmand --deamon
8. Server configuration
All
config params are supplied through
command line
Important ones:
--port=XXX (default 4730)
--deamon
--job-retries (default NO LIMIT!)
--queue-type (default builtin, not persistent)
--listen (default INADDR_ANY)
Use
--help
10. Little bit of shell
Prerequisite:
libgearman (if built from source –
already included)
PEAR (apt-get install php-pear)
PECL (pear install pecl)*
pecl install gearman
Tell PHP to use gearman client/worker API extension
( extension=gearman.so in php.ini)
* For proxy: pear config http_proxy http://
12. GearmanClient
Used
for communication with Job Server
from your application
Foreground jobs (doHigh, doNormal,
doLow)
Background jobs (doHighBackground,
doBackground, doLowBackground)
Tasks (replace do* with addTask*)
addTask* returns GearmanTask object,
do* returns string!
13. GearmanWorker
We
use it to connect our worker script to
Job Server and wait for job
addFunction(string
$function_name
, callable $function)
work()
Callable provided will receive an
GearmanJob object as argument
14. Running tasks in parallel
You
can stack as many tasks using
addTask method of GearmanClient.
They will all be executed in parallel when
you call runTasks (if there’s enough
workers available)
Notice: Using do* methods will execute
the task immediately
15. Client callbacks for workers
events
When
running foreground tasks you can register
callbacks using methods of GearmanClient using
methods:
setCompleteCallback,
setCreatedCallback,
setDataCallback,
setExceptionCallback,
setFailCallback
They
all take single argument – callable object
which will receive GearmanTask object as an
argument.
16. SupervisorD – keep it running
PHP
worker script will wait for job, do it and die.
Supervisord – Process Control System*
apt-get install supervisor
Create configuration file in
/etc/supervisor/supervisord.conf
sudo service supervisor
* Project homepage: http://supervisord.org/
18. Relevant links
PHP Examples:
http://www.php.net/manual/en/gearman.examples.
php
Gearman Job Server documentation:
http://gearman.org/documentation/
Cool tutorials to get started with Gearman on
Ubuntu:
http://blog.andyburton.co.uk/index.php/201212/compiling-gearman-with-mysql-persistent-storageon-ubuntu-12-04-precise/
http://blog.andyburton.co.uk/index.php/201212/gearman-0-41-with-mysql-persistent-storage-andphp-pecl-0-8-3-with-gearmanmanager-on-ubuntu12-04/