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
PHP

Using Zend Framework Models and Libraries from Command Line (or Cron Job)

If you are developing web applications using the Zend Framework for PHP and you need a few cronjobs, you might be wondering if you could (re)use the existing models and libraries. Recently while working on a project, I was wondering the same thing. I have seen people asking the same thing about CodeIgniter framework before but my initial search for a suitable way to use ZF models and libs with cron was unsuccessful. It’s not like that I didn’t find any solutions, the fact is I found quite a few. Unfortunately none of them worked for me. While browsing through the search results, a post on StackOverflow caught my eyes. Someone suggested that we should take the original index.php and Bootstrap.php and hack the codes to get the core ZF running minus the views. That made sense. While running on the command line, we don’t care about the HTTP response and other stuff related with server-client communication. After half an hour of trial and error, I finally found that you can actually take the existing index.php and delete a few words off it to convert it from a web app to a command line app.

Sounds interesting? Locate these lines in the index.php file in the ZF public directory:

Here we first construct the application object, bootstrap it and then run the application. Bootstrapping automatically loads the required classes and executes them. But running the application tries to find a HTTP reuquest and process it. Since we don’t have a HTTP Request, we actually don’t need the last step. You got the hint?

Yes, after constructing the Application object, just bootstrap it, don’t run. So, take the index.php, make a duplicate copy out of it and then change the last few lines like this:

What I do, I create a another folder named “cron” parallel to the public directory, save the bootstrap file as “core.php”. Then in every cron script, I just require_once the core. php, simple, eh?

Categories
PHP

“Pro Zend Framework Techniques: Build a Full CMS Project” – Read Online