Gearman and PHP for Web applications

krishnaveni

What is Gearman?

Gearman is a job management system where you can have multiple jobs done parallel and asynchronized. It has interfaces for multiple languages by which you can call functionality in other programming languages.It distributes the work massively.It also does the load balancing work and easily embeddable with minimal effort since its lightweight and fast. Gearman works in a most fault tolerant way and it’s open source!

How Gearman works?
There are 3 components of Gearman.

1.Client
The client is responsible for creating a connection to the job server and ask for the task to be performed.

2. Worker
Worker performs actual task originated from the client.Multiple worker instances can be available to complete the task fast and can serve multiple clients.The completed task’s output is returned to the client by job server.

3. Job Server
Job server coordinates tasks from client to workers.It finds the idle worker among the multiple instances of worker and transmits the job on. More than one job servers can be configured so that if one job server fails another can be substituted.

cluster

 

Why we need Gearman?

-To run multiple instances of our services to work in parallel(multitasking).
-Distribute the services across multiple servers.
-To implement a service written in another language.
-To handle long running processes.

Use Cases
-Fetching tweet data from twitter streaming API. Streaming API throws huge data each second which can be processed using gearman.
-To process RSS feeds in the background.
-Image Resizing
-Send Emails
-Crawling websites

Install Gearman on Ubuntu Server
$ sudo apt-get install gearman-job-server

Install Gearman PHP Extension:
This is to enable communication between PHP and Gearman
$ sudo apt-get install php-pear
$ sudo apt-get install libgearman-dev
$ sudo pecl install channel://pecl.php.net/gearman-0.8.0

Add Gearman extension
Add extension=gearman.so in php.in file
/etc/php5/apache2/php.ini
/etc/php5/cli/php.ini

Restart Apache server
sudo service apache2 restart

Run Gearman Daemon:
$ sudo gearmand -d
-d to start gearman in background
-u <user> to run gearman as specific user
-L <host> only listen to specific host
-p <port> listen to specific port

Now we are ready to create Gearman workers and clients.

Example
1. Setting up the client
client.php
<?php
$client= new GearmanClient();
$client->addServer(); // add defalut server – localhost
$client->addServer(‘127.0.0.1:4730’); //add server with specific IP
echo $client->do(“upper_fn”, “hello world”);
//echo $handle = $client->doBackground(“upper_fn”, “hello world”);
// $status =$client->jobStatus($handle);
?>
The above code created client object and adds server with default host and another server with specific IP which work as a substitute for the default server.
Method “ do” triggers the gearman function “upper_fn” using the argument “hello world”.
DoBackground() method executes the task asynchronously.
JobStatus() will return an array with status information of background job running.
$status[0] -> boolean indicating job whether job is known.
$status[1] -> boolean indicating job is still running
$status[2] -> fraction of completion percentage

2.Setting up the worker
worker.php
<?php
$worker= new GearmanWorker();
$worker->addServer();
$worker->addFunction(“upper_fn”, “Upper”);
// you can keep the name different for client, worker
//ex. upper_fn and Upper
while (1){
$ret = $worker->work();
if($worker->returnCode() != GEARMAN_SUCCESS)
{
break;
}
sleep(1);
}

function Upper($job)
{
// print_r($job->workload());
$name = $job->workload();

return strtoupper($name);
}
// avoid writing exit or die in worker script.
// with exit or die the gearman process ends abruptly.
?>
The above code creates worker object, adds a server with default host , register function “upper_fn” with the callback function “Upper” and then enters a loop waiting for a job.

Whenever a job is received, job server looks for the free workers who have registered with function name “upper” and forwards the job on, and then the worker calls callback function which simply converts the string to uppercase and returns the string passed in as a workload.

Running Gearman
1. php worker.php & // this will run the worker constantly
You might get this error
“PHP Fatal error: Class ‘GearmanWorker’ not found in /var/www/html/gearman_play/worker.php on line 2“.
This can be resolved by reinstalling the php5 gearman.
sudo apt-get install –reinstall php5-gearman
2. ps -aux // will list worker.php in the running processes
3. php client.php // will assign job to worker

You can instantiate as many workers you want just by “php worker.php &” so that multiple instances of worker available to handle the jobs. Job server sends a job to the worker which is idle.

Also, if your worker process is very expensive, you can push the worker into a separate machine.This way you can improve the performance of your web server.

Gearman with PHP7

Refer this document to install Gearman on PHP7
https://garthkerr.com/install-php-7-on-ubuntu-14-04-with-gearman-support/

Advantages of Gearman
Speed up the work
Parallel and asynchronous
Load balance
Work across servers
Work across languages

Caution
1.Long running scripts may cause memory leak issue, which should be handled carefully especially in a case of a loop.
2.Do not write exit or die in worker script which exit from the Gearman process abruptly.

Google Certified Agency

about the author

krishnaveni