Worker.php
PHP version 5.1.0+
LICENSE: This source file is subject to the New BSD license that is available through the world-wide-web at the following URI: http://www.opensource.org/licenses/bsd-license.php. If you did not receive a copy of the New BSD License and are unable to obtain it through the web, please send a note to license@php.net so we can mail you a copy immediately.
- Author
- Joe Stump
- Category
- Net
- Copyright
- 2007-2008 Digg.com, Inc.
- License
- New BSD License
- Link
- http://pear.php.net/package/Net_Gearman
- Link
- http://www.danga.com/gearman/
- Package
- ShSo\Net\Gearman
- Version
- CVS: $Id$
\ShSo\Net\Gearman\Worker
Run an instance of a worker to listen for jobs. It then manages the running of jobs, etc.
<?php
$servers = array( '127.0.0.1:7003', '127.0.0.1:7004' );
$abilities = array('HelloWorld', 'Foo', 'Bar');
try { $worker = new ShSo\Net\Gearman\Worker($servers); foreach ($abilities as $ability) { $worker->addAbility('HelloWorld'); } $worker->beginWork(); } catch (ShSo\Net\Gearman\Exception $e) { echo $e->getMessage() . "\n"; exit; }
?>
- See
- ShSo\Net\Gearman\Connection
- Author
- Joe Stump
- Category
- Net
- Copyright
- 2007-2008 Digg.com, Inc.
- License
- New BSD License
- Link
- http://www.danga.com/gearman/
- Version
- Release: @package_version@
Constants
JOB_START
= 1- Const
- integer JOB_START Ran when a job is started
- Const
- integer JOB_COMPLETE Ran when a job is finished
- Const
- integer JOB_FAIL Ran when a job fails
Properties

array $callback = array(self::JOB_START => array(), self::JOB_COMPLETE => array(), self::JOB_FAIL => array())
array $initParams = array()array()Details- Type
- array
Methods

addAbility(string $ability, integer $timeout = null, array $initParams = array()) : voidAnnounce an ability to the job server

attachCallback(callback $callback, integer $type = self::JOB_COMPLETE) : voidAttach a callback
| Name | Type | Description |
|---|---|---|
| $callback | callback | A valid PHP callback |
| $type | integer | Type of callback |
| Exception | Description |
|---|---|
| \ShSo\Net\Gearman\ShSo\Net\Gearman\Exception | When an invalid callback is specified. |
| \ShSo\Net\Gearman\ShSo\Net\Gearman\Exception | When an invalid type is specified. |

beginWork(callback $monitor = null) : voidBegin working
This starts the worker on its journey of actually working. The first argument is a PHP callback to a function that can be used to monitor the worker. If no callback is provided then the worker works until it is killed. The monitor is passed two arguments; whether or not the worker is idle and when the last job was ran.
| Name | Type | Description |
|---|---|---|
| $monitor | callback | Function to monitor work |

complete(string $handle, string $job, array $result) : voidRun the complete callbacks
| Name | Type | Description |
|---|---|---|
| $handle | string | The job's Gearman handle |
| $job | string | The name of the job |
| $result | array | The job's returned result |

doWork(resource $socket) : booleanListen on the socket for work
Sends the 'grab_job' command and then listens for either the 'noop' or the 'no_job' command to come back. If the 'job_assign' comes down the pipe then we run that job.
| Name | Type | Description |
|---|---|---|
| $socket | resource | The socket to work on |
| Type | Description |
|---|---|
| boolean | Returns true if work was done, false if not |
| Exception | Description |
|---|---|
| \ShSo\Net\Gearman\ShSo\Net\Gearman\Exception |

fail(string $handle, string $job, object $error) : voidRun the fail callbacks
| Name | Type | Description |
|---|---|---|
| $handle | string | The job's Gearman handle |
| $job | string | The name of the job |
| $error | object | The exception thrown |

start(string $handle, string $job, mixed $args) : voidRun the job start callbacks
| Name | Type | Description |
|---|---|---|
| $handle | string | The job's Gearman handle |
| $job | string | The name of the job |
| $args | mixed | The job's argument list |