WorkerWatchdog::handle()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 14
nc 4
nop 1
dl 0
loc 23
ccs 0
cts 15
cp 0
crap 20
rs 9.7998
c 0
b 0
f 0
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();
0 ignored issues
show
Bug introduced by
The method getProcess() does not exist on League\Event\EventInterface. It seems like you code against a sub-type of League\Event\EventInterface such as Comodojo\Daemon\Events\PosixEvent or Comodojo\Daemon\Events\SocketEvent. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

26
        /** @scrutinizer ignore-call */ 
27
        $daemon = $event->getProcess();
Loading history...
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