Skip to content

Commit 2ca68e9

Browse files
authored
Merge pull request #32 from PATROMO/add-signal-handling
Add signal handling to scheduler worker
2 parents 7a47571 + 6ab7940 commit 2ca68e9

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

lib/ResqueScheduler/Worker.php

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
declare(ticks = 1);
3+
24
/**
35
* ResqueScheduler worker to handle scheduling of delayed tasks.
46
*
@@ -22,7 +24,12 @@ class ResqueScheduler_Worker
2224
* @var int Interval to sleep for between checking schedules.
2325
*/
2426
protected $interval = 5;
25-
27+
28+
/**
29+
* @var boolean True if on the next iteration, the worker should shutdown.
30+
*/
31+
private $shutdown = false;
32+
2633
/**
2734
* The primary loop for a worker.
2835
*
@@ -38,8 +45,12 @@ public function work($interval = null)
3845
}
3946

4047
$this->updateProcLine('Starting');
41-
48+
$this->registerSigHandlers();
49+
4250
while (true) {
51+
if($this->shutdown) {
52+
break;
53+
}
4354
$this->handleDelayedItems();
4455
$this->sleep();
4556
}
@@ -124,4 +135,27 @@ public function log($message)
124135
fwrite(STDOUT, "** [" . strftime('%T %Y-%m-%d') . "] " . $message . "\n");
125136
}
126137
}
138+
139+
/**
140+
* Register signal handlers that a worker should respond to.
141+
*
142+
* TERM: Shutdown after the current timestamp was processed.
143+
* INT: Shutdown after the current timestamp was processed.
144+
* QUIT: Shutdown after the current timestamp was processed.
145+
*/
146+
private function registerSigHandlers()
147+
{
148+
if(!function_exists('pcntl_signal')) {
149+
return;
150+
}
151+
152+
pcntl_signal(SIGTERM, array($this, 'shutdown'));
153+
pcntl_signal(SIGINT, array($this, 'shutdown'));
154+
pcntl_signal(SIGQUIT, array($this, 'shutdown'));
155+
}
156+
157+
public function shutdown()
158+
{
159+
$this->shutdown = true;
160+
}
127161
}

0 commit comments

Comments
 (0)