1
|
|
|
<?php namespace Comodojo\Daemon\Listeners; |
2
|
|
|
|
3
|
|
|
use \League\Event\AbstractListener; |
4
|
|
|
use \League\Event\EventInterface; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* @package Comodojo Daemon |
8
|
|
|
* @author Marco Giovinazzi <[email protected]> |
9
|
|
|
* @license MIT |
10
|
|
|
* |
11
|
|
|
* LICENSE: |
12
|
|
|
* |
13
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
14
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
15
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
16
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
17
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
18
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
19
|
|
|
* THE SOFTWARE. |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
class WorkerWatchdog extends AbstractListener { |
23
|
|
|
|
24
|
|
|
public function handle(EventInterface $event) { |
25
|
|
|
|
26
|
|
|
$daemon = $event->getProcess(); |
|
|
|
|
27
|
|
|
|
28
|
|
|
$workers = $daemon->getWorkers(); |
29
|
|
|
$logger = $daemon->getLogger(); |
30
|
|
|
|
31
|
|
|
foreach ( $workers as $name => $worker ) { |
32
|
|
|
|
33
|
|
|
if ( $workers->running($worker->getPid()) ) { |
34
|
|
|
|
35
|
|
|
$logger->debug("Worker $name seems to be running"); |
36
|
|
|
|
37
|
|
|
} else { |
38
|
|
|
|
39
|
|
|
$logger->warning("Worker $name has exited"); |
40
|
|
|
|
41
|
|
|
if ( $worker->getForever() ) { |
42
|
|
|
$logger->warning("Attempting to restart $name"); |
43
|
|
|
$workers->start($name, true); |
44
|
|
|
} else { |
45
|
|
|
$logger->error("Worker $name has exited, shutting down daemon"); |
46
|
|
|
$daemon->stop(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
} |
56
|
|
|
|