PhpRiot
News Archive
PhpRiot Newsletter
Your Email Address:

More information

Doing the Heavy Lifting: Gearman and Magento

Note: This article was originally published at Planet PHP on 17 January 2011.
Planet PHP

With most web applications, there comes a time where there is a need to perform either CPU or I/O intensive work based on user actions. Whether processing uploaded files, or performing system-wide database updates, developers are increasingly turning to Gearman as a simple way to hand off the heavy lifting to another server to be performed asynchronously. It is easy to install and configure and has interfaces to many popular languages, including PHP.
With this in mind, the team at Ibuildings created a module to allow easy usage of Gearman from within the open source ecommerce solution Magento. Releasing this module provides a simple way to perform job queue operations within the popular open-source ecommerce platform. The module is open source and freely available for download at https://github.com/ibuildings/Magento-Gearman-Module/tree/Version_1_0_0. This tutorial walks through installation of the module and some examples using it; it is assumed that you are already familiar with Magento development to begin this tutorial.

Preparation

Once you have a working installation of Magento, you will need to install Gearman and the PHP classes to work with it. Here are some example steps, aimed at a Debian-based system for illustration purposes.
$ sudo apt-get install libgearman-dev $ sudo pecl install gearman


While developing, you might wish to run the Gearman worker also on your development environment or virtual machine. If so, then you will need to install the server packages for Gearman as well:
$ sudo apt-get install gearman-job-server


At this point you can install the Ibuildings_Gearman module into your Magento installation. Change into the {MAGENTO_ROOT}/app directory and install the module directly from GitHub with the following command:
$ git git://github.com/frak/Magento-Gearman-Module.git


Once you have cleared your cache, you should then go to the System Configuration page, and look for the aoIbuildings Gearman Modulea configuration section. Here you will need to add the IP address and port for your Gearman server. If you are running on a development machine with a local Gearman job server, the settings will be ao127.0.0.1a and ao4730a respectively.

Implementation

There were two scenarios that were envisaged when developing this component: a situation where you would aofire and forgeta the job, where you can safely ignore the status of the job during its lifecycle, or where you would like a reference to the job so that you can check its status periodically and update the user as to job progress.
For the first situation, the module employs the Magento Event/Observer pattern, and can be used from a Model object as follows:

$event = array(); $event['queue'] = 'test'; $event['task'] = array('id' = 1234, 'data' = 'This is a string!', 'callback' = 'http://some.example.com/work_was_done.php'); Mage::dispatchEvent('gearman_do_async_task',

Truncated by Planet PHP, read more at the original (another 17642 bytes)