1<?php
2
3/**
4 * Interface for Danga's Gearman job scheduling system
5 *
6 * PHP version 5.1.0+
7 *
8 * LICENSE: This source file is subject to the New BSD license that is
9 * available through the world-wide-web at the following URI:
10 * http://www.opensource.org/licenses/bsd-license.php. If you did not receive
11 * a copy of the New BSD License and are unable to obtain it through the web,
12 * please send a note to license@php.net so we can mail you a copy immediately.
13 *
14 * @category  Net
15 * @package   Net_Gearman
16 * @author    Joe Stump <joe@joestump.net>
17 * @copyright 2007-2008 Digg.com, Inc.
18 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
19 * @version   CVS: $Id$
20 * @link      http://pear.php.net/package/Net_Gearman
21 * @link      http://www.danga.com/gearman/
22 */
23
24require_once 'Net/Gearman/Job/Common.php';
25require_once 'Net/Gearman/Exception.php';
26
27// Define this if you want your Jobs to be stored in a different
28// path than the default.
29if (!defined('NET_GEARMAN_JOB_PATH')) {
30    define('NET_GEARMAN_JOB_PATH', 'Net/Gearman/Job');
31}
32
33// Define this if you want your Jobs to have a prefix requirement
34if (!defined('NET_GEARMAN_JOB_CLASS_PREFIX')) {
35    define('NET_GEARMAN_JOB_CLASS_PREFIX', 'Net_Gearman_Job_');
36}
37
38/**
39 * Job creation class
40 *
41 * @category  Net
42 * @package   Net_Gearman
43 * @author    Joe Stump <joe@joestump.net>
44 * @copyright 2007-2008 Digg.com, Inc.
45 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
46 * @link      http://www.danga.com/gearman/
47 * @see       Net_Gearman_Job_Common, Net_Gearman_Worker
48 */
49abstract class Net_Gearman_Job
50{
51    /**
52     * Create an instance of a job
53     *
54     * The Net_Geraman_Worker class creates connections to multiple job servers
55     * and then fires off jobs using this function. It hands off the connection
56     * which made the request for the job so that the job can communicate its
57     * status from there on out.
58     *
59     * @param string $job    Name of job (func in Gearman terms)
60     * @param object $conn   Instance of Net_Gearman_Connection
61     * @param string $handle Gearman job handle of job
62     *
63     * @return object Instance of Net_Gearman_Job_Common child
64     * @see Net_Gearman_Job_Common
65     * @throws Net_Gearman_Exception
66     */
67    static public function factory($job, $conn, $handle)
68    {
69        $file = NET_GEARMAN_JOB_PATH . '/' . $job . '.php';
70        include_once $file;
71        $class = NET_GEARMAN_JOB_CLASS_PREFIX . $job;
72        if (!class_exists($class)) {
73            throw new Net_Gearman_Job_Exception('Invalid Job class');
74        }
75
76        $instance = new $class($conn, $handle);
77        if (!$instance instanceof Net_Gearman_Job_Common) {
78            throw new Net_Gearman_Job_Exception('Job is of invalid type');
79        }
80
81        return $instance;
82    }
83}
84
85?>
86