1
1
<?php
2
+ declare (ticks = 1 );
3
+
2
4
/**
3
5
* ResqueScheduler worker to handle scheduling of delayed tasks.
4
6
*
@@ -22,7 +24,12 @@ class ResqueScheduler_Worker
22
24
* @var int Interval to sleep for between checking schedules.
23
25
*/
24
26
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
+
26
33
/**
27
34
* The primary loop for a worker.
28
35
*
@@ -38,8 +45,12 @@ public function work($interval = null)
38
45
}
39
46
40
47
$ this ->updateProcLine ('Starting ' );
41
-
48
+ $ this ->registerSigHandlers ();
49
+
42
50
while (true ) {
51
+ if ($ this ->shutdown ) {
52
+ break ;
53
+ }
43
54
$ this ->handleDelayedItems ();
44
55
$ this ->sleep ();
45
56
}
@@ -124,4 +135,27 @@ public function log($message)
124
135
fwrite (STDOUT , "** [ " . strftime ('%T %Y-%m-%d ' ) . "] " . $ message . "\n" );
125
136
}
126
137
}
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
+ }
127
161
}
0 commit comments