-
Notifications
You must be signed in to change notification settings - Fork 0
/
Daemon.php
173 lines (151 loc) · 5.04 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
<?php
class Daemon {
protected $masterPid, $pid;
protected $daemonsCount;
protected $daemon;
protected $working=1;
protected $childs = [];
function __construct($count)
{
$this->masterPid = posix_getpid();
$this->initSharedComponents();
$this->daemonsCount = $count;
ignore_user_abort(false);
for($i=0;$i < $this->daemonsCount; $i++){
$this->fork();
}
$this->detectRole();
/*
for($i=0;$i < $this->daemonsCount; $i++){
$pid = pcntl_fork();
if($pid){//host
$this->childs[] = $pid;
} else {//child
if(posix_getppid() != $this->masterPid){ exit(); }//prevent recursion
}
}
if($this->isHost()){
$this->registerAsHost();
} else {
$this->registerAsDaemon();
}
$this->registerSignalsCallback();
$this->run();*/
}
public function fork(){
$pid = pcntl_fork();
if($pid){//host
$this->childs[] = $pid;
} else {//child
if(posix_getppid() != $this->masterPid){ exit(); }//prevent recursion
}
}
public function detectRole(){
if($this->isHost()){
$this->registerAsHost();
} else {
$this->registerAsDaemon();
}
$this->registerSignalsCallback();
$this->run();
}
/**
* Share nothing ideology
* We can use it, but can't close it
*/
protected function initSharedComponents(){
//TODO:: db, opened files - here
}
protected function registerAsDaemon(){
$this->daemon = new DaemonWorker();
unset($this->childs);//unregister for childrens
}
protected function registerAsHost(){
$this->daemon = new DaemonServer();
}
private function isHost(){
return $this->masterPid == getmypid();
}
function __destruct()
{
// var_dump('calling desctructor in daemon '.$this->pid);
// unlink($this->pid.'.pid');
}
protected function run(){
while($this->working){
pcntl_signal_dispatch();
if($this->isHost()){
$recreateCount=0;
foreach($this->childs as $position=>$pid){
/**
* someone die, and we begin re-create worker process
*/
if(pcntl_waitpid($pid,$status='',WNOHANG) == -1){
unset($this->childs[$position]);
var_dump('Childrens count in isHost='.(int)$this->isHost().' : '.count($this->childs).'. Died process - #'.$pid);
$recreateCount++;
/*$this->fork();
// $this->detectRole();
if(!$this->isHost()){
$this->registerAsDaemon();
} else {
exit();
}*/
}
}
for($i=0;$i<$recreateCount;$i++){
$this->fork();
if(posix_getppid() != $this->masterPid && getmypid() != $this->masterPid) {
exit();//prevent recursion
} else if(!$this->isHost()){
var_dump('Re-creating closed process with new pid #'.getmypid());
$this->registerAsDaemon();
break;
}
}
if($this->isHost()){
$this->daemon->serve($this->childs);
}
} else {
$this->daemon->serve();
}
}
}
protected function registerSignalsCallback(){
pcntl_signal(SIGTERM, [$this,"signalHandler"]);
pcntl_signal(SIGINT, [$this,"signalHandler"]);
}
// Обработчик
function signalHandler($signo) {
var_dump('Getting signal handler with $signo = '.$signo);
switch($signo) {
case SIGTERM: {
$this->working = false;
var_dump('Closing SIGTERM daemon #'.getmypid());
exit(0);
}
case SIGINT: { //ctrl+C - break the process
$this->working = false;
var_dump('Closing SIGINT daemon #'.getmypid());
exit(0);
}
case SIGCHLD: { //1 child change state
// $this->working = false;
// $this->__destruct();
// foreach($this->childs as $pid){
// pcntl_waitpid($pid);
// $res = pcntl_waitpid($pid, $status, WNOHANG);
// // If the process has already exited
// if($res == -1 || $res > 0){
// unset($childs[$key]);
// }
// }
var_dump('Get SIGCHLD in #'.getmypid());
exit(0);
}
default: {
//все остальные сигналы
}
}
}
}