Skip to content
lenn0x edited this page Sep 13, 2010 · 3 revisions

About

Net_Gearman is a PEAR package for interfacing with Danga’s Gearman. Gearman is a system to farm out work to other machines, dispatching function calls to machines that are better suited to do work, to do work in parallel, to load balance lots of function calls, or to call functions between languages.

Net_Gearman is currently in production at Yahoo! and Digg doing all sorts of offloaded near time processing.

Installation

  1. git clone git://github.com/lenn0x/net_gearman.git

Examples

Client


<?php

require_once 'Net/Gearman/Client.php';

$client = new Net_Gearman_Client('localhost:7003');
$client->someBackgroundJob(array(
    'userid' => 5555,
    'action' => 'new-comment'
));

?>

Client with Callbacks


<?php

require_once 'Net/Gearman/Client.php';

function complete($func, $handle, $result) {
    var_dump($func);
    var_dump($handle);
    var_dump($result);
}

function fail($task_object) {
    var_dump($task_object);
}

$client = new Net_Gearman_Client('localhost:7003');
$set = new Net_Gearman_Set();
$jobs = array(
        'AddTwoNumbers' => array('1', '2'),
        'Multiply' => array('3', '4')
    );

foreach ($jobs as $job => $args) {
    $task = new Net_Gearman_Task($job, $args);
    $task->attachCallback("complete",Net_Gearman_Task::TASK_COMPLETE);
    $task->attachCallback("fail",Net_Gearman_Task::TASK_FAIL);
    $set->addTask($task);
}

$client->runSet($set);

?>

Job


<?php

class Net_Gearman_Job_someBackgroundJob extends Net_Gearman_Job_Common
{
    public function run($args)
    {
        if (!isset($args['userid']) || !isset($args['action'])) {
            throw new Net_Gearman_Job_Exception('Invalid/Missing arguments');
        }

        // Insert a record or something based on the $args

        return array(); // Results are returned to Gearman, except for 
                        // background jobs like this one.
    }
}

?>

Worker


<?php

require_once 'Net/Gearman/Worker.php';

$worker = new Net_Gearman_Worker('localhost:7003');
$worker->addAbility('someBackgroundJob');
$worker->beginWork();

?>

Clone this wiki locally