Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
Checking mergeability… Don't worry, you can still create the pull request.
  • 11 commits
  • 16 files changed
  • 0 commit comments
  • 3 contributors
View
1  .gitignore
@@ -0,0 +1 @@
+tests/test-config.php
View
49 Net/Gearman/Client.php
@@ -35,6 +35,7 @@
* @author Joe Stump <joe@joestump.net>
* @copyright 2007-2008 Digg.com, Inc.
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
+ * @version Release: @package_version@
* @link http://www.danga.com/gearman/
*/
class Net_Gearman_Client
@@ -138,6 +139,15 @@ public function __call($func, array $args = array())
protected function submitTask(Net_Gearman_Task $task)
{
switch ($task->type) {
+ case Net_Gearman_Task::JOB_LOW:
+ $type = 'submit_job_low';
+ break;
+ case Net_Gearman_Task::JOB_LOW_BACKGROUND:
+ $type = 'submit_job_low_bg';
+ break;
+ case Net_Gearman_Task::JOB_HIGH_BACKGROUND:
+ $type = 'submit_job_high_bg';
+ break;
case Net_Gearman_Task::JOB_BACKGROUND:
$type = 'submit_job_bg';
break;
@@ -177,23 +187,56 @@ protected function submitTask(Net_Gearman_Task $task)
* Run a set of tasks
*
* @param object $set A set of tasks to run
+ * @param int $timeout Time in seconds for the socket timeout. Max is 10 seconds
*
* @return void
* @see Net_Gearman_Set, Net_Gearman_Task
*/
- public function runSet(Net_Gearman_Set $set)
+ public function runSet(Net_Gearman_Set $set, $timeout = null)
{
$totalTasks = $set->tasksCount;
$taskKeys = array_keys($set->tasks);
$t = 0;
+ if ($timeout !== null){
+ $socket_timeout = min(10, (int)$timeout);
+ } else {
+ $socket_timeout = 10;
+ }
+
while (!$set->finished()) {
+
+ if ($timeout !== null) {
+
+ if (empty($start)) {
+
+ $start = microtime(true);
+
+ } else {
+
+ $now = microtime(true);
+
+ if ($now - $start >= $timeout) {
+ break;
+ }
+ }
+
+ }
+
+
if ($t < $totalTasks) {
$k = $taskKeys[$t];
$this->submitTask($set->tasks[$k]);
- if ($set->tasks[$k]->type == Net_Gearman_Task::JOB_BACKGROUND) {
+ if ($set->tasks[$k]->type == Net_Gearman_Task::JOB_BACKGROUND ||
+ $set->tasks[$k]->type == Net_Gearman_Task::JOB_HIGH_BACKGROUND ||
+ $set->tasks[$k]->type == Net_Gearman_Task::JOB_LOW_BACKGROUND) {
+
$set->tasks[$k]->finished = true;
$set->tasksCount--;
+ } else {
+ if($set->tasks[$k]->timeout !== null) {
+ $started = microtime(true);
+ }
}
$t++;
@@ -202,7 +245,7 @@ public function runSet(Net_Gearman_Set $set)
$write = null;
$except = null;
$read = $this->conn;
- socket_select($read, $write, $except, 10);
+ socket_select($read, $write, $except, $socket_timeout);
foreach ($read as $socket) {
$resp = Net_Gearman_Connection::read($socket);
if (count($resp)) {
View
26 Net/Gearman/Connection.php
@@ -31,6 +31,7 @@
* @author Joe Stump <joe@joestump.net>
* @copyright 2007-2008 Digg.com, Inc.
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
+ * @version Release: @package_version@
* @link http://www.danga.com/gearman/
*/
class Net_Gearman_Connection
@@ -57,6 +58,9 @@ class Net_Gearman_Connection
'submit_job' => array(7, array('func', 'uniq', 'arg')),
'submit_job_high' => array(21, array('func', 'uniq', 'arg')),
'submit_job_bg' => array(18, array('func', 'uniq', 'arg')),
+ 'submit_job_high_bg' => array(32, array('func', 'uniq', 'arg')),
+ 'submit_job_low' => array(33, array('func', 'uniq', 'arg')),
+ 'submit_job_low_bg' => array(34, array('func', 'uniq', 'arg')),
'job_created' => array(8, array('handle')),
'grab_job' => array(9, array()),
'no_job' => array(10, array()),
@@ -139,7 +143,7 @@ static public function connect($host, $timeout = 2000)
$err = '';
$errno = 0;
- $port = 7003;
+ $port = 4730;
if (strpos($host, ':')) {
list($host, $port) = explode(':', $host);
@@ -148,16 +152,20 @@ static public function connect($host, $timeout = 2000)
$start = microtime(true);
do {
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
- @socket_connect($socket, $host, $port);
- $errorCode = socket_last_error($socket);
-
+ $socket_connected = @socket_connect($socket, $host, $port);
+ if($socket_connected){
socket_set_nonblock($socket);
socket_set_option($socket, SOL_TCP, 1, 1);
$timeLeft = ((microtime(true) - $start) * 1000);
- } while (!is_resource($socket) && $timeLeft < $timeout);
+ }
+ } while (!$socket_connected && $timeLeft < $timeout);
- if ($errorCode == 111) {
- throw new Net_Gearman_Exception("Can't connect to server");
+ if (!$socket_connected) {
+ $errno = socket_last_error($socket);
+ $errstr = socket_strerror($errno);
+ throw new Net_Gearman_Exception(
+ "Can't connect to server ($errno: $errstr)"
+ );
}
self::$waiting[(int)$socket] = array();
@@ -225,8 +233,10 @@ static public function send($socket, $command, array $params = array())
} while ($written < $cmdLength);
if ($error === true) {
+ $errno = socket_last_error($socket);
+ $errstr = socket_strerror($errno);
throw new Net_Gearman_Exception(
- 'Could not write command to socket'
+ "Could not write command to socket ($errno: $errstr)"
);
}
}
View
2  Net/Gearman/Exception.php
@@ -32,11 +32,11 @@
* @copyright 2007-2008 Digg.com, Inc.
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @link http://www.danga.com/gearman/
+ * @version Release: @package_version@
* @see PEAR_Exception
*/
class Net_Gearman_Exception extends PEAR_Exception
{
-
}
?>
View
5 Net/Gearman/Job.php
@@ -44,6 +44,7 @@
* @copyright 2007-2008 Digg.com, Inc.
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @link http://www.danga.com/gearman/
+ * @version Release: @package_version@
* @see Net_Gearman_Job_Common, Net_Gearman_Worker
*/
abstract class Net_Gearman_Job
@@ -64,7 +65,7 @@
* @see Net_Gearman_Job_Common
* @throws Net_Gearman_Exception
*/
- static public function factory($job, $conn, $handle)
+ static public function factory($job, $conn, $handle, $params=array())
{
$file = NET_GEARMAN_JOB_PATH . '/' . $job . '.php';
include_once $file;
@@ -73,7 +74,7 @@ static public function factory($job, $conn, $handle)
throw new Net_Gearman_Job_Exception('Invalid Job class');
}
- $instance = new $class($conn, $handle);
+ $instance = new $class($conn, $handle, $params);
if (!$instance instanceof Net_Gearman_Job_Common) {
throw new Net_Gearman_Job_Exception('Job is of invalid type');
}
View
9 Net/Gearman/Job/Common.php
@@ -31,6 +31,7 @@
* @author Joe Stump <joe@joestump.net>
* @copyright 2007-2008 Digg.com, Inc.
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
+ * @version Release: @package_version@
* @link http://www.danga.com/gearman/
* @see Net_Gearman_Job_Common, Net_Gearman_Worker
*/
@@ -52,6 +53,11 @@
protected $conn = null;
/**
+ * Parameters for Job instantiation
+ * @var array $params
+ */
+
+ /**
* Constructor
*
* @param resource $conn Connection to communicate with
@@ -59,10 +65,11 @@
*
* @return void
*/
- public function __construct($conn, $handle)
+ public function __construct($conn, $handle, array $params=array())
{
$this->conn = $conn;
$this->handle = $handle;
+ $this->params = $params;
}
/**
View
5 Net/Gearman/Job/Exception.php
@@ -21,7 +21,7 @@
* @link http://www.danga.com/gearman/
*/
-require_once 'PEAR/Exception.php';
+require_once 'Net/Gearman/Exception.php';
/**
* Exception class for Gearman jobs
@@ -34,10 +34,11 @@
* @author Joe Stump <joe@joestump.net>
* @copyright 2007-2008 Digg.com, Inc.
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
+ * @version Release: @package_version@
* @link http://www.danga.com/gearman/
* @see Net_Gearman_Job_Common, Net_Gearman_Worker
*/
-class Net_Gearman_Job_Exception extends PEAR_Exception
+class Net_Gearman_Job_Exception extends Net_Gearman_Exception
{
}
View
3  Net/Gearman/Manager.php
@@ -35,6 +35,7 @@
* @author Joe Stump <joe@joestump.net>
* @copyright 2007-2008 Digg.com, Inc.
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
+ * @version Release: @package_version@
* @link http://www.danga.com/gearman/
*/
class Net_Gearman_Manager
@@ -74,7 +75,7 @@ public function __construct($server, $timeout = 5)
list($host, $port) = explode(':', $server);
} else {
$host = $server;
- $port = 7003;
+ $port = 4730;
}
$errCode = 0;
View
1  Net/Gearman/Set.php
@@ -62,6 +62,7 @@
* @author Joe Stump <joe@joestump.net>
* @copyright 2007-2008 Digg.com, Inc.
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
+ * @version Release: @package_version@
* @link http://www.danga.com/gearman/
* @see Net_Gearman_Job_Common, Net_Gearman_Worker
*/
View
29 Net/Gearman/Task.php
@@ -29,6 +29,7 @@
* @author Joe Stump <joe@joestump.net>
* @copyright 2007-2008 Digg.com, Inc.
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
+ * @version Release: @package_version@
* @link http://www.danga.com/gearman/
* @see Net_Gearman_Set, Net_Gearman_Client
*/
@@ -59,6 +60,9 @@ class Net_Gearman_Task
* @see Net_Gearman_Task::JOB_NORMAL
* @see Net_Gearman_Task::JOB_BACKGROUND
* @see Net_Gearman_Task::JOB_HIGH
+ * @see Net_Gearman_Task::JOB_HIGH_BACKGROUND
+ * @see Net_Gearman_Task::JOB_LOW
+ * @see Net_Gearman_Task::JOB_LOW_BACKGROUND
*/
public $type = self::JOB_NORMAL;
@@ -144,7 +148,28 @@ class Net_Gearman_Task
*
* @var integer JOB_HIGH
*/
- const JOB_HIGH = 2;
+ const JOB_HIGH = 3;
+
+ /**
+ * High priority, background job
+ *
+ * @var integer JOB_HIGH
+ */
+ const JOB_HIGH_BACKGROUND = 4;
+
+ /**
+ * LOW priority job
+ *
+ * @var integer JOB_LOW
+ */
+ const JOB_LOW = 5;
+
+ /**
+ * Low priority, background job
+ *
+ * @var integer JOB_LOW_BACKGROUND
+ */
+ const JOB_LOW_BACKGROUND = 6;
/**
* Callback of type complete
@@ -184,7 +209,7 @@ class Net_Gearman_Task
* Constructor
*
* @param string $func Name of job to run
- * @param array $arg List of arguments for job
+ * @param mixed $arg List of arguments for job
* @param string $uniq The unique id of the job
* @param integer $type Type of job to run task as
*
View
19 Net/Gearman/Worker.php
@@ -1,5 +1,4 @@
<?php
-
/**
* Interface for Danga's Gearman job scheduling system
*
@@ -59,6 +58,7 @@
* @author Joe Stump <joe@joestump.net>
* @copyright 2007-2008 Digg.com, Inc.
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
+ * @version Release: @package_version@
* @link http://www.danga.com/gearman/
* @see Net_Gearman_Job, Net_Gearman_Connection
*/
@@ -347,14 +347,17 @@ protected function doWork($socket)
* @param integer $type Type of callback
*
* @return void
- * @throws Net_Gearman_Exception
+ * @throws Net_Gearman_Exception When an invalid callback is specified.
+ * @throws Net_Gearman_Exception When an invalid type is specified.
*/
public function attachCallback($callback, $type = self::JOB_COMPLETE)
{
if (!is_callable($callback)) {
throw new Net_Gearman_Exception('Invalid callback specified');
}
-
+ if (!isset($this->callback[$type])) {
+ throw new Net_Gearman_Exception('Invalid callback type specified.');
+ }
$this->callback[$type][] = $callback;
}
@@ -369,7 +372,7 @@ public function attachCallback($callback, $type = self::JOB_COMPLETE)
*/
protected function start($handle, $job, $args)
{
- if (!count($this->callback[self::JOB_START])) {
+ if (count($this->callback[self::JOB_START]) == 0) {
return; // No callbacks to run
}
@@ -389,7 +392,7 @@ protected function start($handle, $job, $args)
*/
protected function complete($handle, $job, array $result)
{
- if (!count($this->callback[self::JOB_COMPLETE])) {
+ if (count($this->callback[self::JOB_COMPLETE]) == 0) {
return; // No callbacks to run
}
@@ -409,7 +412,7 @@ protected function complete($handle, $job, array $result)
*/
protected function fail($handle, $job, PEAR_Exception $error)
{
- if (!count($this->callback[self::JOB_FAIL])) {
+ if (count($this->callback[self::JOB_FAIL]) == 0) {
return; // No callbacks to run
}
@@ -450,6 +453,4 @@ public function stopWork()
{
return false;
}
-}
-
-?>
+}
View
8 tests/001-echo_req.phpt
@@ -1,9 +1,13 @@
--TEST--
echo_req
+--SKIPIF--
+<?php
+if (!file_exists(dirname(__FILE__) . '/tests-config.php')) {
+ die('skip This test requires a test-config.php file.');
+}
--FILE--
<?php
-
-require_once 'tests-config.php';
+require_once dirname(__FILE__) . '/tests-config.php';
require_once 'Net/Gearman/Connection.php';
$s = Net_Gearman_Connection::connect(array_pop($servers));
View
7 tests/010-client-call.phpt
@@ -1,9 +1,14 @@
--TEST--
Net_Gearman_Client::__call()
+--SKIPIF--
+<?php
+if (!file_exists(dirname(__FILE__) . '/tests-config.php')) {
+ die('skip This test requires a test-config.php file.');
+}
--FILE--
<?php
-require_once 'tests-config.php';
+require_once dirname(__FILE__) . '/tests-config.php';
require_once 'Net/Gearman/Client.php';
$gearman = new Net_Gearman_Client($servers);
View
9 tests/012-client-runSet.phpt
@@ -1,8 +1,13 @@
--TEST--
+Net_Gearman_Set, Net_Gearman_Client::runSet()
+--SKIPIF--
+<?php
+if (!file_exists(dirname(__FILE__) . '/tests-config.php')) {
+ die('skip This test requires a test-config.php file.');
+}
--FILE--
<?php
-
-require_once 'tests-config.php';
+require_once dirname(__FILE__) . '/tests-config.php';
require_once 'Net/Gearman/Client.php';
$sums = array(
View
129 tests/AllTests.php
@@ -0,0 +1,129 @@
+<?php
+/**
+ * Master Unit Test Suite file for Net_Gearman
+ *
+ * This top-level test suite file organizes
+ * all class test suite files,
+ * so that the full suite can be run
+ * by PhpUnit or via "pear run-tests -u".
+ *
+ * PHP version 5
+ *
+ * @category Net
+ * @package Net_Gearman
+ * @subpackage Net_Gearman_AllTests
+ * @author Till Klampaeckel <till@php.net>
+ * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
+ * @version CVS: $Id$
+ * @link http://pear.php.net/package/Net_Gearman
+ * @since 0.2.4
+ */
+
+
+/**
+ * Check PHP version... PhpUnit v3+ requires at least PHP v5.1.4
+ */
+if (version_compare(PHP_VERSION, "5.1.4") < 0) {
+ // Cannnot run test suites
+ echo 'Cannot run test suite via PhpUnit... requires at least PHP v5.1.4.' . PHP_EOL;
+ echo 'Use "pear run-tests -p Net_Gearman" to run the PHPT tests directly.' . PHP_EOL;
+ exit(1);
+}
+
+
+/**
+ * Derive the "main" method name
+ * @internal PhpUnit would have to rename PHPUnit_MAIN_METHOD to PHPUNIT_MAIN_METHOD
+ * to make this usage meet the PEAR CS... we cannot rename it here.
+ */
+if (!defined('PHPUnit_MAIN_METHOD')) {
+ define('PHPUnit_MAIN_METHOD', 'Net_Gearman_AllTests::main');
+}
+
+
+/*
+ * Files needed by PhpUnit
+ */
+require_once 'PHPUnit/Framework.php';
+require_once 'PHPUnit/TextUI/TestRunner.php';
+require_once 'PHPUnit/Extensions/PhptTestSuite.php';
+
+/*
+ * You must add each additional class-level test suite file here
+ */
+// there are no PhpUnit test files... only PHPTs.. so nothing is listed here
+
+/**
+ * directory where PHPT tests are located
+ */
+define('Net_Gearman_DIR_PHPT', dirname(__FILE__));
+
+/**
+ * Master Unit Test Suite class for Net_Gearman
+ *
+ * This top-level test suite class organizes
+ * all class test suite files,
+ * so that the full suite can be run
+ * by PhpUnit or via "pear run-tests -up Net_Gearman".
+ *
+ * @category Net
+ * @package Net_Gearman
+ * @subpackage Net_Gearman_AllTests
+ * @author Till Klampaeckel <till@php.net>
+ * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
+ * @version Release: @package_version@
+ * @link http://pear.php.net/package/Net_Gearman
+ * @since 0.2.4
+ */
+class Net_Gearman_AllTests
+{
+
+ /**
+ * Launches the TextUI test runner
+ *
+ * @return void
+ * @uses PHPUnit_TextUI_TestRunner
+ */
+ public static function main()
+ {
+ PHPUnit_TextUI_TestRunner::run(self::suite());
+ }
+
+
+ /**
+ * Adds all class test suites into the master suite
+ *
+ * @return PHPUnit_Framework_TestSuite a master test suite
+ * containing all class test suites
+ * @uses PHPUnit_Framework_TestSuite
+ */
+ public static function suite()
+ {
+ $suite = new PHPUnit_Framework_TestSuite(
+ 'Net_Gearman Full Suite of Unit Tests');
+
+ /*
+ * You must add each additional class-level test suite name here
+ */
+ // there are no PhpUnit test files... only PHPTs.. so nothing is listed here
+
+ /**
+ * @desc add PHPT tests
+ */
+ $phpt = new PHPUnit_Extensions_PhptTestSuite(Net_Gearman_DIR_PHPT);
+ $suite->addTestSuite($phpt);
+
+ return $suite;
+ }
+}
+
+/**
+ * Call the main method if this file is executed directly
+ * @internal PhpUnit would have to rename PHPUnit_MAIN_METHOD to PHPUNIT_MAIN_METHOD
+ * to make this usage meet the PEAR CS... we cannot rename it here.
+ */
+if (PHPUnit_MAIN_METHOD == 'Net_Gearman_AllTests::main') {
+ Net_Gearman_AllTests::main();
+}
+
+/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
View
0  tests/tests-config.php → tests/tests-config.php-dist
File renamed without changes

No commit comments for this range

Something went wrong with that request. Please try again.