Skip to content
This repository has been archived by the owner on May 27, 2019. It is now read-only.

Add signal handling to queue worker for clean shutdown #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions vendors/shells/queue.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(ticks = 1);

/**
* @author [email protected]
Expand All @@ -22,6 +23,8 @@ class QueueShell extends Shell {

protected $_verbose = false;

private $exit;

/**
* Overwrite shell initialize to dynamically load all Queue Related Tasks.
*/
Expand Down Expand Up @@ -124,19 +127,20 @@ public function runworker() {
if (function_exists('gc_enable')) {
gc_enable();
}
$exit = false;
pcntl_signal(SIGTERM, array(&$this, "_exit"));
$this->exit = false;
$starttime = time();
$group = null;
if (isset($this->params['group']) && !empty($this->params['group'])) {
$group = $this->params['group'];
}
while (!$exit) {
while (!$this->exit) {
if($this->_verbose) {
$this->out('Looking for Job....');
}
$data = $this->QueuedTask->requestJob($this->getTaskConf(), $group);
if ($this->QueuedTask->exit === true) {
$exit = true;
$this->exit = true;
} else {
if ($data !== false) {
$this->out('Running Job of type "' . $data['jobtype'] . '"');
Expand All @@ -155,7 +159,7 @@ public function runworker() {
}
} elseif (Configure::read('queue.exitwhennothingtodo')) {
$this->out('nothing to do, exiting.');
$exit = true;
$this->exit = true;
} else {
if($this->_verbose) {
$this->out('nothing to do, sleeping.');
Expand All @@ -165,10 +169,10 @@ public function runworker() {

// check if we are over the maximum runtime and end processing if so.
if (Configure::read('queue.workermaxruntime') != 0 && (time() - $starttime) >= Configure::read('queue.workermaxruntime')) {
$exit = true;
$this->exit = true;
$this->out('Reached runtime of ' . (time() - $starttime) . ' Seconds (Max ' . Configure::read('queue.workermaxruntime') . '), terminating.');
}
if ($exit || rand(0, 100) > (100 - Configure::read('queue.gcprop'))) {
if ($this->exit || rand(0, 100) > (100 - Configure::read('queue.gcprop'))) {
$this->out('Performing Old job cleanup.');
$this->QueuedTask->cleanOldJobs();
}
Expand Down Expand Up @@ -255,5 +259,9 @@ function out($str='') {
return parent::out($str);
}

function _exit($signal) {
$this->exit = true;
}

}
?>
?>