Worker.phpView Source

Show: PublicProtectedPrivateinherited
Table of Contents
Interface for Danga's Gearman job scheduling system

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

Package: ShSo\Net\Gearman
Gearman worker class

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

>VConstant  JOB_START = 1
Callback types
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  
>VConstant  JOB_COMPLETE = 2
>VConstant  JOB_FAIL = 3

Properties

>VPropertyprotectedarray $abilities = array()
Pool of worker abilities
Default valuearray()Details
Type
array
>VPropertyprotectedarray $callback = array(self::JOB_START => array(), self::JOB_COMPLETE => array(), self::JOB_FAIL => array())
Callbacks registered for this worker
Default valuearray(self::JOB_START => array(), self::JOB_COMPLETE => array(), self::JOB_FAIL => array())Details
Type
array
See
 
See
 
See
 
>VPropertyprotectedarray $conn = array()
Pool of connections to Gearman servers
Default valuearray()Details
Type
array
>VPropertyprotectedstring $id = ""
Unique id for this worker
Default value""Details
Type
string
>VPropertyprotectedarray $initParams = array()
Parameters for job contructors, indexed by ability name
Default valuearray()Details
Type
array
>VPropertyprotectedarray $retryConn = array()
Pool of retry connections
Default valuearray()Details
Type
array

Methods

methodpublic__construct(array $servers = null, string $id = "") : void

Constructor

Parameters
NameTypeDescription
$serversarray

List of servers to connect to

$idstring

Optional unique id for this worker

Throws
ExceptionDescription
\ShSo\Net\Gearman\ShSo\Net\Gearman\Exception
Details
See
 
methodpublic__destruct() : void

Destructor

Details
See
 
methodpublicaddAbility(string $ability, integer $timeout = null, array $initParams = array()) : void

Announce an ability to the job server

Parameters
NameTypeDescription
$abilitystring

Name of functcion/ability

$timeoutinteger

How long to give this job

$initParamsarray

Parameters for job constructor

Details
See
 
methodpublicattachCallback(callback $callback, integer $type = self::JOB_COMPLETE) : void

Attach a callback

Parameters
NameTypeDescription
$callbackcallback

A valid PHP callback

$typeinteger

Type of callback

Throws
ExceptionDescription
\ShSo\Net\Gearman\ShSo\Net\Gearman\ExceptionWhen an invalid callback is specified.
\ShSo\Net\Gearman\ShSo\Net\Gearman\ExceptionWhen an invalid type is specified.
methodpublicbeginWork(callback $monitor = null) : void

Begin 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.

Parameters
NameTypeDescription
$monitorcallback

Function to monitor work

Details
See
ShSo\Net\Gearman\Connection::connect()  
See
ShSo\Net\Gearman\Worker::addAbility()  
methodprotectedcomplete(string $handle, string $job, array $result) : void

Run the complete callbacks

Parameters
NameTypeDescription
$handlestring

The job's Gearman handle

$jobstring

The name of the job

$resultarray

The job's returned result

methodprotecteddoWork(resource $socket) : boolean

Listen 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.

Parameters
NameTypeDescription
$socketresource

The socket to work on

Returns
TypeDescription
booleanReturns true if work was done, false if not
Throws
ExceptionDescription
\ShSo\Net\Gearman\ShSo\Net\Gearman\Exception
Details
See
 
methodpublicendWork() : void

Stop working

methodprotectedfail(string $handle, string $job, object $error) : void

Run the fail callbacks

Parameters
NameTypeDescription
$handlestring

The job's Gearman handle

$jobstring

The name of the job

$errorobject

The exception thrown

methodprotectedstart(string $handle, string $job, mixed $args) : void

Run the job start callbacks

Parameters
NameTypeDescription
$handlestring

The job's Gearman handle

$jobstring

The name of the job

$argsmixed

The job's argument list

methodpublicstopWork() : boolean

Should we stop work?

Returns
TypeDescription
boolean
Documentation was generated by phpDocumentor 2.8.5.