Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Changes to support the ability to set a timeout for a job #50

Merged
merged 4 commits into from

2 participants

@rswarthout

Changes support the ability to set a "timeout" config value to be sent to the gearmand server at a global level as well as a per job level.

@brianlmoon
Owner

Sorry for the crazy lag getting to this. Can you fix the typo in the -h help and then I will merge. Thanks.

@brianlmoon
Owner

Wow, that was fast.

@brianlmoon brianlmoon merged commit f92d1f5 into brianlmoon:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
21 GearmanManager.php
@@ -384,6 +384,10 @@ protected function getopt() {
$this->config['count'] = (int)$opts['D'];
}
+ if (isset($opts['t'])) {
+ $this->config['timeout'] = $opts['t'];
+ }
+
if (isset($opts['h'])) {
$this->config['host'] = $opts['h'];
}
@@ -567,7 +571,7 @@ protected function parse_config($file) {
$this->log("Loading configuration from $file");
- if(substr($file, -4) == ".php"){
+ if (substr($file, -4) == ".php"){
require $file;
@@ -577,7 +581,7 @@ protected function parse_config($file) {
}
- if(empty($gearman_config)){
+ if (empty($gearman_config)){
$this->show_help("No configuration found in $file");
}
@@ -853,6 +857,16 @@ protected function start_worker($worker="all") {
$worker_list = array($worker);
}
+ $timeouts = array();
+ $default_timeout = ((isset($this->config['timeout'])) ?
+ (int) $this->config['timeout'] : null);
+
+ // build up the list of worker timeouts
+ foreach ($worker_list as $worker) {
+ $timeouts[$worker] = ((isset($this->config['functions'][$worker]['timeout'])) ?
+ (int) $this->config['functions'][$worker]['timeout'] : $default_timeout);
+ }
+
$pid = pcntl_fork();
switch($pid) {
@@ -874,7 +888,7 @@ protected function start_worker($worker="all") {
uasort($worker_list, array($this, "sort_priority"));
}
- $this->start_lib_worker($worker_list);
+ $this->start_lib_worker($worker_list, $timeouts);
$this->log("Child exiting", GearmanManager::LOG_LEVEL_WORKER_INFO);
@@ -1109,6 +1123,7 @@ protected function show_help($msg = "") {
echo " -v Increase verbosity level by one\n";
echo " -w DIR Directory where workers are located, defaults to ./workers. If you are using PECL, you can provide multiple directories separated by a comma.\n";
echo " -r NUMBER Maximum job iterations per worker\n";
+ echo " -t SECONDS Maximum number of seconds gearmand server should wait for a worker to complete work before timing out and reissuing work to another worker.\n";
echo " -x SECONDS Maximum seconds for a worker to live\n";
echo " -Z Parse the command line and config file then dump it to the screen and exit.\n";
echo "\n";
View
7 config-advanced.ini
@@ -29,6 +29,9 @@ max_worker_lifetime=3600
; Reload workers as new code is available
auto_update=1
+; Timeout n seconds for all jobs before work is reissued to another worker
+timeout = 300
+
[reverse_string]
; We are guaranteed 3 workers that can do job reverse_string
count = 3
@@ -42,3 +45,7 @@ dedicated_only=1
[fetch_url]
; There will be a minimum 15 workers that do only the fetch_url job
count=15
+
+; Timeout to override the default global timeout
+timeout = 30
+
View
10 pear-manager.php
@@ -32,10 +32,11 @@ class GearmanPearManager extends GearmanManager {
* Starts a worker for the PEAR library
*
* @param array $worker_list List of worker functions to add
+ * @param array $timeouts list of worker timeouts to pass to server
* @return void
*
*/
- protected function start_lib_worker($worker_list) {
+ protected function start_lib_worker($worker_list, $timeouts = array()) {
/**
* Require PEAR Net_Gearman libs
@@ -55,8 +56,9 @@ protected function start_lib_worker($worker_list) {
$worker = new Net_Gearman_Worker($this->servers);
foreach($worker_list as $w){
- $this->log("Adding job $w", GearmanManager::LOG_LEVEL_WORKER_INFO);
- $worker->addAbility($w);
+ $timeout = (isset($timeouts[$w]) ? $timeouts[$w] : null);
+ $this->log("Adding job $w ; timeout: " . $timeout, GearmanManager::LOG_LEVEL_WORKER_INFO);
+ $worker->addAbility($w, $timeout);
}
$worker->attachCallback(array($this, 'job_start'), Net_Gearman_Worker::JOB_START);
@@ -218,4 +220,4 @@ protected function validate_lib_workers() {
$mgr = new GearmanPearManager();
-?>
+?>
View
8 pecl-manager.php
@@ -23,10 +23,11 @@ class GearmanPeclManager extends GearmanManager {
* Starts a worker for the PECL library
*
* @param array $worker_list List of worker functions to add
+ * @param array $timeouts list of worker timeouts to pass to server
* @return void
*
*/
- protected function start_lib_worker($worker_list) {
+ protected function start_lib_worker($worker_list, $timeouts = array()) {
$thisWorker = new GearmanWorker();
@@ -40,8 +41,9 @@ protected function start_lib_worker($worker_list) {
}
foreach($worker_list as $w){
- $this->log("Adding job $w", GearmanManager::LOG_LEVEL_WORKER_INFO);
- $thisWorker->addFunction($w, array($this, "do_job"), $this);
+ $timeout = (isset($timeouts[$w]) ? $timeouts[$w] : null);
+ $this->log("Adding job $w ; timeout: " . $timeout, GearmanManager::LOG_LEVEL_WORKER_INFO);
+ $thisWorker->addFunction($w, array($this, "do_job"), $this, $timeout);
}
$start = time();
Something went wrong with that request. Please try again.