Permalink
Browse files

Adding max job executions limit as suggested by Etsy

  • Loading branch information...
1 parent 3ef2789 commit 1d2615f9463a904cec1eea42eecfef7cd9bed57e @brianlmoon committed Oct 23, 2011
Showing with 35 additions and 1 deletion.
  1. +21 −1 GearmanManager.php
  2. +6 −0 pear-manager.php
  3. +8 −0 pecl-manager.php
View
@@ -164,6 +164,21 @@
*/
protected $max_run_time = 3600;
+ /**
+ * Maximum number of jobs this worker will do before quitting
+ */
+ protected $max_job_count = 0;
+
+ /**
+ * Maximum job iterations per worker
+ */
+ protected $max_runs_per_worker = null;
+
+ /**
+ * Number of times this worker has run a job
+ */
+ protected $job_execution_count = 0;
+
/**
* Servers that workers connect to
*/
@@ -310,7 +325,7 @@ public function __destruct() {
*/
protected function getopt() {
- $opts = getopt("ac:dD:h:Hl:o:p:P:u:v::w:x:Z");
+ $opts = getopt("ac:dD:h:Hl:o:p:P:u:v::w:r:x:Z");
if(isset($opts["H"])){
$this->show_help();
@@ -355,6 +370,10 @@ protected function getopt() {
$this->config['max_worker_lifetime'] = (int)$opts['x'];
}
+ if (isset($opts['r'])) {
+ $this->config['max_runs_per_worker'] = (int)$opts['r'];
+ }
+
if (isset($opts['D'])) {
$this->config['count'] = (int)$opts['D'];
}
@@ -1049,6 +1068,7 @@ protected function show_help($msg = "") {
echo " -u USERNAME Run wokers as USERNAME\n";
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 " -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
@@ -64,6 +64,7 @@ protected function start_lib_worker($worker_list) {
$worker->attachCallback(array($this, 'job_fail'), Net_Gearman_Worker::JOB_FAIL);
$this->start_time = time();
+ $this->job_execution_count++;
$worker->beginWork(array($this, "monitor"));
@@ -88,6 +89,11 @@ public function monitor($idle, $lastJob) {
$this->log("Worker's last job $time seconds ago", GearmanManager::LOG_LEVEL_CRAZY);
+ if(!empty($this->config["max_runs_per_worker"]) && $this->job_execution_count >= $this->config["max_runs_per_worker"]) {
+ $this->log("Ran $this->job_execution_count jobs which is over the maximum({$this->config['max_runs_per_worker']}), exiting", GearmanManager::LOG_LEVEL_WORKER_INFO);
+ $this->stop_work = true;
+ }
+
return $this->stop_work;
}
View
@@ -71,6 +71,11 @@ protected function start_lib_worker($worker_list) {
$this->stop_work = true;
}
+ if(!empty($this->config["max_runs_per_worker"]) && $this->job_execution_count >= $this->config["max_runs_per_worker"]) {
+ $this->log("Ran $this->job_execution_count jobs which is over the maximum({$this->config['max_runs_per_worker']}), exiting", GearmanManager::LOG_LEVEL_WORKER_INFO);
+ $this->stop_work = true;
+ }
+
}
$thisWorker->unregisterAll();
@@ -184,6 +189,9 @@ public function do_job($job) {
$type = gettype($result);
settype($result, $type);
+
+ $this->job_execution_count++;
+
return $result;
}

0 comments on commit 1d2615f

Please sign in to comment.