Other posts in this series
Now that Gearmand is setup we need to get some workers going. Gearman is itself language agnostic; it doesn’t really care if your client/worker is in Python, PHP or Ruby, to name a few. There are several libraries available to help you get going.
##PECL::Gearman In order to get clients and workers setup using PHP you need to have libgearman installed; this came with Gearmand from my previous post in this series. For this example I’ll be using the Gearman PECL extension. I assume you already have PHP installed on your system.
By default, the configure script looks in
, 1
/usr/local
, and 1
/usr
to find the libgearman header files. If
you’re using Homebrew, MacPorts, or installed from source you should have no problem.1
/opt/local
To install using PECL, run
The
is required since there is no full fledged stable release to use at this time.1
-beta
Side Note: This should work fine for installations of libgearman I covered previously, but if you’re like myself and have your Homebrew installed elsewhere (I use Cinderella), your header files are in a different place, and you’ll need to compile the extension by downloading the source, extracting it, and running the following
1
2
3
4
5
6
7
~$ cd Downloads/gearman-0.7.0/gearman-0.7.0
~$ phpize
~$ ./configure --with-gearman=`brew --prefix`
~$ make
~$ (sudo) make install
Installing shared extensions: /usr/lib/php/extensions/no-debug-non-zts-20090626/
~$
When the installation completes, you should receive instructions on activating the extension. They typically involve updating
your
file to include the 1
php.ini
and add 1
extension_dir
near the end of the file. After that,
restart your sever.1
extension=gearman.so
To confirm everything is OK, create a simple php file that echo’s out the gearman version:
And run the file
##Creating Jobs
We’ll start with a very simple “reverse string” job to get going. First we’ll create the client to submit the job. We do so by instantiating a new
object,
set the server where Gearmand is running, and calling 1
GearmanClient
with the job name and the workload:1
do
You can run this on the command line like so:
The client will wait for the response (there are other types of jobs where the client does not wait, called background jobs).
Now we create a worker in a similar process: instantiating a new
object and set the server where Gearmand is running, except now we register a method of work
that we can do, and define that function:1
GearmanWorker
Now we run the worker from another terminal session. Because we’ve put the worker in an endless
loop, the worker
will connect to the server and perform jobs until we kill the process with 1
while
1
cntrl+c
Back on the original terminal session, where we left the client waiting, you should see an update:
The Gearmand server held on to the job until a worker arrived that could preform the work. Once our worker fired up, the server handed the job over, the worker did the work, and told the server it was done and could accept more work. The server in turn passed the result back to the client, and the client process terminated, printing the reversed string.