The Gearman is a extension which is used to distribute work load to different processes or machines to optimize them. It is an anagram for a manager who can efficiently delegate tasks. Gearman is an opensource application framework designed to distribute appropriate computer tasks to multiple computers, so large tasks can be done more quickly. This was developed by Brad Fitzpatrick in 2009 and was initially written for Perl-based applications.
Benefits of Gearman
- Gearman is licensed under Berkeley Software Distribution so it is easy to get the latest builds and add one’s own functionality to it.
- As it supports multiple languages it can be used to create heterogenous applications. For example, we can use PHP and Java for the same project. where Java uses a function in PHP.
- We can implement parallel processing using Gearman.
- The gearman can support messages upto 4GB in size and can even chunk messages so that the overhead can be minimal.
Gearman Architecture
- The Gearman architecture consists of three major components :
- The gearman server,
- A Gearman client, and
- The Gearman worker.
The client sends the request to the worker via the server and the server works viceversa The Gearman has client and worker APIs that your programs use to communicate with the Gearman job server This ensures that you don’t have to configure the networking or mapping of jobs. Behind the screen, the Gearman client and worker APIs communicate with the job server using TCP socket.
Installing Gearman
- To start with the Gearman should be installed and PHP should be configured to use the Gearman configuration.
- Check if the Gearman is available to PHP.
Next download Gearman
This will download a Gunzip file, We have to extract it:
Configure the Gearman:
The above steps can be followed by make and make install.
To check if the Gearman is installed properly use the following command:
Nextm we configure PHP with Gearman support.
The downloaded Gearman file will be of few kbs so the download is pretty fast. The next step is to configure and install it with the PHP installation on the server
- tar -xzvf gearman-0.6.0.tgz
- cd gearman-0.6.0
- phpize
- ./configure
- make
- sudo make install
After this you can go to the modules directory of the PHP installation and see that the Gearman.so file is listed there. Open your php.ini file and add the extension to Gearman, alternatively you can also go to the php.d directory and create a gearman.ini file to connect to the gearman system object.
- cd /etc/php.d
- touch gearman.ini
In the gearman.ini write the following content.
; Enable gearman extension module
extension = gearman.so
Testing the Gearman on the website
Once the above step is completed , just restart the Apache service and load up the PHP info on your browser to see if the Gearman support is enabled.
Testing the Gearman installation on the Local machine
Create a php file with the following code :
<?php echo "The gearman version installed is".gearman_version()."n" ;?>
Once you run this command you should get an output similar to following:
The Gearman version installed is 0.6.0
Creating the worker
The worker is the file which will read and reply to all incoming requests.
<?php $worker= new GearmanWorker(); $worker->addServer(); $worker->addFunction("job", "job_function"); while ($worker->work());
function job_function($job)
{
return strlen(strtolower($job->workload()));
}
?>
Once the worker is created let it run as a background job in linux
php worker.php&
We can check if the worker is running by using the following command
ps -elf | grep worker.php
Now create the client for this worker
<?php $client= new GearmanClient(); $client->addServer(); print $client->do("job", "How many words are there in this string"); print "n"; ?>
Save the file as client.php
Now you can run this client by calling php client.php You can see output as 39
For more information on the gearman please go to the following resources