Categories
PHP

Installing and Getting Started with Gearman

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:

Install Gearman PHP Extension:

Now, let’s install gearman pecl extension:

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:

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:

No, I am on Debian! Okay, try this:

Still no results? Use aptitude to find libgearman.

Now run the command again:

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:

If you want the daemon to go verbose and help you in debugging with detailed data, use the “-vvv” switch like this:

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:

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:

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:

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!

Categories
PHP

Gear up your PHP Apps with GearMan

One of the reasons I love working at Leevio is the tons of new stuff I learn from here. The latest one is – Gearman!

Gearman is an application framework for distributing workload. How does it work? Simple, you run the core daemon (gearmand) with necessary configurations (ip, port, mode, data storage etc) and a worker process registers itself with a named hook. Then, when you need to run that worker, just make a call from the client side.In plain English, the process may read like this:

1) Gearman is the project manager in your office.
2) A new worker joins your office and tells the project manager that he can build awesome wordpress themes.
3) The project manager takes note on his capabilities and waits for clients.
4) A client calls and orders a wordpress theme with specifications.
5) The project manager then passes the specifications to the new wordpress developer, gets the work done by him and returns the result to the client.

The task can be run both in foreground and background. Gearman is multi-threaded, asynchronous, fault tolerant and has multiple language support. So what does this all translate into?

— Run necessary “jobs” in the background
— Write codes in multiple languages
— Build multi-threaded and asynchronous task handlers
— Build task queues instead of draining out all the system resources with concurrent processes

Gearman has PHP support. That means PHP web apps can take the advantage of gearman to offload their “jobs” to the gearman server for background processing.

Who uses Gearman with PHP?

— Yahoo (6M jobs per day)
— Digg (400k jobs per day)
— Xing.com and many others

How do we get started with Gearman? Well, the web is so full of Gearman topics that I am not going to write the codes myself. I would rather point you to articles written by more skilled people than me:

— Rasmus Lerdorf (Father of PHP): http://toys.lerdorf.com/archives/51-Playing-with-Gearman.html

— Matthew Weier O’Phinney (Developer of ZF): http://weierophinney.net/matthew/archives/240-Writing-Gearman-Workers-in-PHP.html

— Cesar D. Rodas on PHPClasses: http://www.phpclasses.org/blog/post/108-Distributing-PHP-processing-with-Gearman.html

— IBM Developer Works: http://www.ibm.com/developerworks/opensource/library/os-php-gearman/

— Gonzalo Ayuso: http://gonzalo123.wordpress.com/2011/03/07/watermarks-in-our-images-with-php-and-gearman/

Installation Notes:

http://www.geeksww.com/tutorials/operating_systems/linux/installation/installing_gearman_shared_pecl_extension_for_php_on_debianubuntu_linux.php

http://blog.stuartherbert.com/php/2010/02/26/getting-gearman-up-and-running-on-ubuntu-karmic/

Gearman Tricks:

http://till.klampaeckel.de/blog/archives/94-start-stop-daemon,-Gearman-and-a-little-PHP.html

Categories
Linux

Getting full theming of Ubuntu inside VirtualBox

Due to some issues with my laptop hardware, I can not use Ubuntu all time. After some time, the laptop just freezes. After repeated occurrence of the same crash, I have started using Windows full time and kept Ubuntu inside VirtualBox. In the default setup, the first thing I noticed is that the theme is broken – both in unity and the classic ubuntu session (which is a gnome2 based fallback option, I guess). I love working on Ubuntu, but I hate the grey old ubuntu theme that appears on my screen. I always wondered why this happened.

After some googling, I came to find that VirtualBox by default doesn’t enable 3D hardware acceleration. So, Unity can not find required hardware to enable the theme. I just increased the video memory for the virtual machine to 32MB and enabled 3D acceleration. Installed VirtualBox guest addition. Restarted the machine.

Everything was fine except the panels and nautilus icons. It is mainly because the “gnome-settings-daemon” crashes. I killed the process and then restarted it using:

(PS: Don’t use sudo, you need to kill the process running under your account)

Then I dealt with nautilus in the same manner: