In my last post on Gearman, I have discussed why I love this tool. In this post, I am going to share some commands and codes to get you started with Gearman.
Install Gearman:
1 |
sudo apt-get install gearman |
Install Gearman PHP Extension:
1 2 |
# Enable PEAR and PECL sudo apt-get install php-pear |
Now, let’s install gearman pecl extension:
1 |
sudo pecl install gearman |
Did that work? NO! Why? Because Gearman PECL extension is still in beta mode. Unless a PECL package is “stable”, you have to mention the full channel name to force the beta installation:
1 |
sudo pecl install channel://pecl.php.net/gearman-0.8.0 |
Did it work? NO! Why? It asks for “libgearman”. Hell no! I can’t find that package! 🙁
Ok, don’t panick! It happens that libgearman was initially released as libgearman1 and afterwards came libgearman2 and libgearman3.
Are you on Ubuntu? Use this:
1 |
sudo apt-get install libgearman-dev |
No, I am on Debian! Okay, try this:
1 |
sudo apt-get install libgearman2 |
Still no results? Use aptitude to find libgearman.
Now run the command again:
1 |
sudo pecl install channel://pecl.php.net/gearman-0.8.0 |
Works, right? If you get an error message regarding “autoconf” or something, Google is your best friend. If everything is okay, go ahead!
Run Gearman Daemon:
Gearman Daemon runs in the background. It matches the client request to the specific worker process. It registers worker processes and distributes the workload among them. To run it:
1 |
sudo gearmand -d -u root |
If you want the daemon to go verbose and help you in debugging with detailed data, use the “-vvv” switch like this:
1 |
sudo gearmand -d -vvv -u root |
You can change the startup parameters by editing the file: /etc/default/gearman-job-server. Don’t forget to play with the configuration a bit 🙂
Get a PHP Worker:
Let’s code a handy string processor:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php // Create a GearmanWorker object $worker= new GearmanWorker(); // Add the default server $worker->addServer(); // Register the processing function $worker->addFunction("process_string", "processString"); // Trigger the infinite loop while ($worker->work()); // The main function that processes the job function processString($job) { // get the workload - it's a string. use serialization to pass objects $param = $job->workload(); return "You passed the string: {$param} \n It had: ".strlen($param)." chars \n"; } ?> |
So you see, I built a super useful string processor. With it’s high level of intelligence, it can echo back the string and tell you how many characters were there. When invoked, Gearman shall call the function with the $job parameter as a gearman job. The workload() method will extract parameter passed from the client.
We just defined a function, created a GearmanWorker object, connected to the default server and registered the function.
Now, how do we invoke the job? With a client:
1 2 3 4 5 6 7 8 |
<?php // construct a client object $client= new GearmanClient(); // add the default server $client->addServer(); // print the output of the job. first parameter is the job name, second one is the parameter print $client->do("process_string", "Hello Gearman!"); ?> |
So, what are the output? I had three terminal tabs open. One for the main gearmand with verbose mode, another for worker.php and other one for the client output. Here’s the output sequentially:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
masnun@lighthouse:~$ sudo gearmand -d -vvv -u root INFO Starting up masnun@lighthouse:~$ INFO Listening on 0.0.0.0:4730 (7) INFO Creating wakeup pipe INFO Creating IO thread wakeup pipe INFO Adding event for listening socket (7) INFO Adding event for wakeup pipe INFO Entering main event loop INFO Accepted connection from 127.0.0.1:45305 INFO [ 0] 127.0.0.1:45305 Connected INFO Accepted connection from 127.0.0.1:45306 INFO [ 0] 127.0.0.1:45306 Connected INFO [ 0] 127.0.0.1:45306 Disconnected |
1 |
masnun@lighthouse:~/gearman$ php worker.php |
1 2 3 4 |
masnun@lighthouse:~/gearman$ php client.php You passed the string: Hello Gearman! It had: 14 chars masnun@lighthouse:~/gearman$ |
NOTES:
— For ease of demonstration, I have executed the client from a command line script, in actual live environment, you shall be calling it from a web app.
— On a remote SSH connection, you may not be able to have 3 terminal tabs open. Put an “&” after the commands to send them to background 🙂
— Use the same command to spawn as many workers as you need
Happy Gearing with PHP!
7 replies on “Installing and Getting Started with Gearman”
sudo apt-get install build-essentials 🙂
Nice simple and nifty example 🙂
[…] Reference: You can also read the getting started guide from this page. Or if you want to install it through unix package manager you can also follow this post […]
Masnun vai, thanks for nice explanation of using gearman. Here is some other nice resources I believe :
http://www.slideshare.net/felixdv/high-gear-php-with-gearman
http://www.lornajane.net/posts/2011/Using-Gearman-from-PHP
http://mwop.net/blog/240-Writing-Gearman-Workers-in-PHP.html
https://github.com/brianlmoon/GearmanManager/
Happy coding !!!
Very good article, simple and very practical.
Greetings from Barcelona (Catalonia)
[…] Reference: You can also read the getting started guide from this page. Or if you want to install it through unix package manager you can also follow this post […]
Can i install this for windows