/
daemon.php
280 lines (241 loc) · 6.12 KB
/
daemon.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?php
namespace Kiki;
use Kiki\Core;
use Kiki\Log;
declare( ticks = 1 );
/**
* Abstract class for daemon processes.
*
* @class Daemon
* @package Kiki
* @author Rob Kaper <http://robkaper.nl/>
* @copyright 2011 Rob Kaper <http://robkaper.nl/>
* @license Released under the terms of the MIT license.
*/
abstract class Daemon
{
protected $pid = 0;
protected $db = null;
private $childPids = array();
private $killPids = array();
private $shutdown = false;
private $killTime = 0;
public function __construct()
{
$this->db = Core::getDb();
$this->pid = getmypid();
}
protected function childInit()
{
// Reinit database connection... seems to avoid 'gone away' errors after forking...
$this->db = Core::getDb(true);
// Re-init log to create a unique ID.
Log::init();
}
abstract protected function main();
abstract protected function cleanup( $pid );
public function start( $numChildren=3 )
{
$this->closeFileHandles();
$this->setHandlers();
// Prior to forking children, otherwise they end up as orphans.
$this->toBackground();
$this->detach();
Log::info( "started, pid $this->pid" );
for( $i=0 ; $i<$numChildren ; $i++ )
{
$pid = $this->createChild();
if ( $pid > 0 )
$this->childPids[]=$pid;
}
$this->run();
Log::debug( "exiting" );
exit(0);
}
private function closeFileHandles()
{
fclose(STDIN);
fclose(STDOUT);
fclose(STDERR);
}
private function detach()
{
$rv = posix_setsid();
if ( $rv === -1 )
{
Log::error( "detach failed" );
die();
}
}
private function setHandlers()
{
pcntl_signal( SIGTERM, array(&$this, "signalHandler") );
pcntl_signal(SIGTSTP, SIG_IGN);
pcntl_signal(SIGTTOU, SIG_IGN);
pcntl_signal(SIGTTIN, SIG_IGN);
pcntl_signal(SIGHUP, SIG_IGN);
}
public function signalHandler( $signal )
{
switch( $signal )
{
case SIGTERM:
Log::info( "received SIGTERM" );
if ( count($this->childPids) )
{
// Parent
$this->shutdown = true;
$this->killTime = time()+5;
$this->reapChildren();
}
else
{
// Child
$this->shutdown = true;
}
break;
default:;
}
}
private function reapChildren( $kill = false )
{
$signalStr = $kill ? "SIGKILL" : "SIGTERM";
for( $i=0 ; $i<count($this->childPids) ; ++$i )
{
$pid = $this->childPids[$i];
Log::info( "sending $signalStr to child $pid" );
$rv = posix_kill($pid, ($kill ? SIGKILL : SIGTERM) );
if ( $kill )
$this->killPids[] = $pid;
if ( !$rv )
Log::error( "signal $signalStr to child $pid failed" );
}
}
private function toBackground()
{
// Fork a child and exit parent process: this is a daemon. Child continues.
$pid = pcntl_fork();
$this->pid = getmypid();
switch( $pid )
{
case -1:
// Error
Log::error( "fork to background failed" );
die();
case 0:
// Child
break;
default:
// Parent
exit(0);
}
}
protected function run()
{
$timeToKill = -1;
for(;;)
{
// Check up on children
for( $i=0 ; $i<count($this->childPids) ; ++$i )
{
$pid = $this->childPids[$i];
// Check child status
$status = null;
$rv = pcntl_waitpid( $pid, $status, WNOHANG );
if ( $rv == -1 )
{
// Child disappeared
$ok = pcntl_wifexited($status);
$exitstatus = pcntl_wexitstatus($status);
if ( $ok )
{
// Normal exit
Log::info( "child $pid exited" );
}
else
{
// Unforeseen exit
Log::error( "error with child $pid" );
// $this->cleanup($pid);
}
array_splice( $this->childPids, $i, 1 );
if ( !$this->shutdown )
{
Log::debug( "creating a new child" );
// Start a new child to keep the desired number intact
$pid = $this->createChild();
if ( $pid > 0 )
$this->childPids[] = $pid;
}
// Child was removed from array, so decrement index
--$i; continue;
}
}
$count = count($this->childPids);
if ( $count )
{
if ( $this->shutdown )
{
$newTimeToKill = $this->killTime - time();
if ( $newTimeToKill > 0 && $newTimeToKill != $timeToKill )
{
// Wait for children to exit
$timeToKill = $newTimeToKill;
Log::info( "waiting $timeToKill seconds for $count children to exit" );
}
else if ( $newTimeToKill == 0 )
$this->reapChildren(true);
}
sleep(1);
}
else
{
for( $i=0 ; $i<count($this->killPids) ; ++$i )
{
$pid = $this->killPids[$i];
// Log::info( "cleaning up for pid=[$pid]" );
// $this->cleanup( $pid );
}
Log::info( "shutdown: no more children" );
return;
}
}
}
function createChild()
{
$pid = pcntl_fork();
if ( $pid == -1 )
Log::error( "fork failed" );
else if ( $pid )
{
Log::info( "forked a child, pid $pid" );
// pcntl_wait($status); // Protect against Zombie children
}
else
{
// Forked child starts execution here.
$this->pid = getmypid();
// Explicitly give each child its own database instance, using the
// same object gives headahes for forked processes.
$this->db = new Database( Config::$db, true );
$this->childInit();
// Reset list of children, as a child we have none.
if ( count($this->childPids) )
$this->childPids = array();
for( ;; )
{
$usleep = (int) $this->main();
if ( $this->shutdown )
{
$this->cleanup($this->pid);
Log::info( "child shutting down, pid $this->pid" );
exit(0);
}
usleep($usleep);
}
Log::error( "SNH: post-loop exit" );
exit(0);
}
return $pid;
}
}