|
1
|
|
|
<?php namespace Comodojo\Extender\Base; |
|
2
|
|
|
|
|
3
|
|
|
use \Comodojo\Extender\Components\PidLock; |
|
4
|
|
|
use \Comodojo\Extender\Components\RunLock; |
|
5
|
|
|
use \Comodojo\Extender\Components\Niceness; |
|
6
|
|
|
use \Comodojo\Extender\Events\DaemonEvent; |
|
7
|
|
|
use \Comodojo\Extender\Listeners\PauseDaemon; |
|
8
|
|
|
use \Comodojo\Extender\Listeners\ResumeDaemon; |
|
9
|
|
|
use \Comodojo\Dispatcher\Components\Configuration; |
|
10
|
|
|
use \Comodojo\Cache\Cache; |
|
11
|
|
|
use \League\Event\Emitter; |
|
12
|
|
|
use \Psr\Log\LoggerInterface; |
|
13
|
|
|
use \Exception; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Basic signaling for extender components |
|
17
|
|
|
* |
|
18
|
|
|
* @package Comodojo Framework |
|
19
|
|
|
* @author Marco Giovinazzi <[email protected]> |
|
20
|
|
|
* @license GPL-3.0+ |
|
21
|
|
|
* |
|
22
|
|
|
* LICENSE: |
|
23
|
|
|
* |
|
24
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
25
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
26
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
27
|
|
|
* License, or (at your option) any later version. |
|
28
|
|
|
* |
|
29
|
|
|
* This program is distributed in the hope that it will be useful, |
|
30
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
31
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
32
|
|
|
* GNU Affero General Public License for more details. |
|
33
|
|
|
* |
|
34
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
35
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
36
|
|
|
*/ |
|
37
|
|
|
|
|
38
|
|
|
abstract class Daemon extends Process { |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @todo exit condition if not in command line |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct( |
|
44
|
|
|
Configuration $configuration, |
|
45
|
|
|
LoggerInterface $logger, |
|
46
|
|
|
Emitter $events, |
|
47
|
|
|
$looptime = 1, |
|
48
|
|
|
$niceness = null) |
|
49
|
|
|
{ |
|
50
|
|
|
|
|
51
|
|
|
parent::__construct($configuration, $logger, $events, $niceness); |
|
52
|
|
|
|
|
53
|
|
|
$this->looptime = self::getLoopTime($looptime); |
|
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
$this->loopcount = 0; |
|
|
|
|
|
|
56
|
|
|
$this->loopactive = true; |
|
|
|
|
|
|
57
|
|
|
$this->loopelapsed = 0; |
|
|
|
|
|
|
58
|
|
|
$this->cleanup = true; |
|
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
$lockfile = $this->configuration->get('pid-file'); |
|
|
|
|
|
|
61
|
|
|
$runfile = $this->configuration->get('run-file'); |
|
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
$this->pidlock = new PidLock($this->pid, $lockfile); |
|
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
$this->runlock = new RunLock($runfile); |
|
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
// attach signal handlers |
|
68
|
|
|
$this->events->subscribe('extender.signal.'.SIGTSTP, '\Comodojo\Extender\Listeners\PauseDaemon'); |
|
|
|
|
|
|
69
|
|
|
$this->events->subscribe('extender.signal.'.SIGCONT, '\Comodojo\Extender\Listeners\ResumeDaemon'); |
|
|
|
|
|
|
70
|
|
|
$this->events->subscribe('extender.signal.'.SIGINT, '\Comodojo\Extender\Listeners\StopDaemon'); |
|
|
|
|
|
|
71
|
|
|
$this->events->subscribe('extender.signal.'.SIGTERM, '\Comodojo\Extender\Listeners\StopDaemon'); |
|
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
// notify that everything is ready |
|
74
|
|
|
$this->logger->info("Daemon ready (pid: ".$this->pid.")"); |
|
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
abstract public function loop(); |
|
79
|
|
|
|
|
80
|
|
|
public function daemonize() { |
|
81
|
|
|
|
|
82
|
|
|
$pid = pcntl_fork(); |
|
83
|
|
|
|
|
84
|
|
|
if ( $pid == -1 ) { |
|
85
|
|
|
$this->logger->error('Could not create daemon (fork error)'); |
|
|
|
|
|
|
86
|
|
|
$this->cleanup = false; |
|
|
|
|
|
|
87
|
|
|
$this->end(1); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
if ( $pid ) { |
|
91
|
|
|
$this->logger->info("Daemon created with pid $pid"); |
|
|
|
|
|
|
92
|
|
|
$this->cleanup = false; |
|
|
|
|
|
|
93
|
|
|
$this->end(0); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
// become a session leader |
|
97
|
|
|
posix_setsid(); |
|
98
|
|
|
|
|
99
|
|
|
// autostart daemon |
|
100
|
|
|
$this->start(); |
|
101
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function start() { |
|
105
|
|
|
|
|
106
|
|
|
// create lockfiles |
|
107
|
|
|
$this->runlock->lock(); |
|
|
|
|
|
|
108
|
|
|
$this->pidlock->lock(); |
|
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
$this->logger->notice("Starting daemon (looping each ".$this->looptime." secs, pid: ".$this->pid.")"); |
|
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
$this->events->emit( new DaemonEvent('start', $this) ); |
|
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
while ($this->loopactive) { |
|
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
$start = microtime(true); |
|
117
|
|
|
|
|
118
|
|
|
pcntl_signal_dispatch(); |
|
119
|
|
|
|
|
120
|
|
|
$this->events->emit( new DaemonEvent('preloop', $this) ); |
|
|
|
|
|
|
121
|
|
|
|
|
122
|
|
|
if ( $this->runlock->check() && $this->loopactive) { |
|
|
|
|
|
|
123
|
|
|
|
|
124
|
|
|
$this->events->emit( new DaemonEvent('loopstart', $this) ); |
|
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
$this->loop(); |
|
127
|
|
|
|
|
128
|
|
|
$this->events->emit( new DaemonEvent('loopstop', $this) ); |
|
|
|
|
|
|
129
|
|
|
|
|
130
|
|
|
$this->loopcount++; |
|
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
$this->events->emit( new DaemonEvent('postloop', $this) ); |
|
|
|
|
|
|
135
|
|
|
|
|
136
|
|
|
$this->loopelapsed = (microtime(true) - $start); |
|
|
|
|
|
|
137
|
|
|
|
|
138
|
|
|
$lefttime = $this->looptime - $this->loopelapsed; |
|
|
|
|
|
|
139
|
|
|
|
|
140
|
|
|
if ( $lefttime > 0 ) usleep($lefttime * 1000000); |
|
141
|
|
|
|
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
$this->logger->notice("Stopping daemon (pid: ".$this->pid.")"); |
|
|
|
|
|
|
145
|
|
|
|
|
146
|
|
|
$this->events->emit( new DaemonEvent('stop', $this) ); |
|
|
|
|
|
|
147
|
|
|
|
|
148
|
|
|
$this->end(0); |
|
149
|
|
|
|
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
public function stop() { |
|
153
|
|
|
|
|
154
|
|
|
// just in case daemon will not execute the stop order |
|
155
|
|
|
$this->loopactive = false; |
|
|
|
|
|
|
156
|
|
|
$this->end(0); |
|
157
|
|
|
|
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
public function pause() { |
|
161
|
|
|
|
|
162
|
|
|
return $this->runlock->pause(); |
|
|
|
|
|
|
163
|
|
|
|
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
public function resume() { |
|
167
|
|
|
|
|
168
|
|
|
return $this->runlock->resume(); |
|
|
|
|
|
|
169
|
|
|
|
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
public function isLooping() { |
|
173
|
|
|
|
|
174
|
|
|
$this->runlock->check(); |
|
|
|
|
|
|
175
|
|
|
|
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
public function shutdown() { |
|
179
|
|
|
|
|
180
|
|
|
// release lockfiles |
|
181
|
|
|
if ( $this->cleanup ) { |
|
|
|
|
|
|
182
|
|
|
$this->runlock->release(); |
|
|
|
|
|
|
183
|
|
|
$this->pidlock->release(); |
|
|
|
|
|
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
View Code Duplication |
public static function getLoopTime($looptime) { |
|
|
|
|
|
|
189
|
|
|
|
|
190
|
|
|
return filter_var($looptime, FILTER_VALIDATE_INT, array( |
|
191
|
|
|
'options' => array( |
|
192
|
|
|
'default' => 1, |
|
193
|
|
|
'min_range' => 1 |
|
194
|
|
|
) |
|
195
|
|
|
)); |
|
196
|
|
|
|
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
} |
|
200
|
|
|
|
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.