Passed
Push — master ( 1d1c44...e0e6c6 )
by Roman
02:44
created

WorkerMonitor   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 13
dl 0
loc 98
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A workerStop() 0 7 2
A events() 0 10 2
A workerLoop() 0 13 4
A workerStart() 0 8 1
A getSenderName() 0 8 3
1
<?php
2
/**
3
 * @link https://github.com/zhuravljov/yii2-queue-monitor
4
 * @copyright Copyright (c) 2017 Roman Zhuravlev
5
 * @license http://opensource.org/licenses/BSD-3-Clause
6
 */
7
8
namespace zhuravljov\yii\queue\monitor;
9
10
use Yii;
11
use yii\base\Behavior;
12
use yii\base\InvalidConfigException;
13
use yii\console\ExitCode;
14
use yii\queue\cli\Queue;
15
use yii\queue\cli\WorkerEvent;
16
use zhuravljov\yii\queue\monitor\records\WorkerRecord;
17
18
/**
19
 * Queue Worker Monitor
20
 *
21
 * @author Roman Zhuravlev <[email protected]>
22
 */
23
class WorkerMonitor extends Behavior
24
{
25
    /**
26
     * @var Queue
27
     * @inheritdoc
28
     */
29
    public $owner;
30
    /**
31
     * @var Env
32
     */
33
    protected $env;
34
    /**
35
     * @var WorkerRecord
36
     */
37
    private $record;
38
39
    /**
40
     * @param Env $env
41
     * @param array $config
42
     */
43
    public function __construct(Env $env, $config = [])
44
    {
45
        $this->env = $env;
46
        parent::__construct($config);
47
    }
48
49
    /**
50
     * @inheritdoc
51
     */
52
    public function events()
53
    {
54
        $events = [
55
            Queue::EVENT_WORKER_START => 'workerStart',
56
            Queue::EVENT_WORKER_STOP => 'workerStop',
57
        ];
58
        if ($this->env->canListenWorkerLoop()) {
59
            $events[Queue::EVENT_WORKER_LOOP] = 'workerLoop';
60
        }
61
        return $events;
62
    }
63
64
    /**
65
     * @param WorkerEvent $event
66
     */
67
    public function workerStart(WorkerEvent $event)
68
    {
69
        $this->record = new WorkerRecord();
70
        $this->record->sender_name = $this->getSenderName($event);
71
        $this->record->pid = $event->sender->getWorkerPid();
72
        $this->record->started_at = time();
73
        $this->record->pinged_at = time();
74
        $this->record->save(false);
75
    }
76
77
    /**
78
     * @param WorkerEvent $event
79
     */
80
    public function workerLoop(WorkerEvent $event)
81
    {
82
        if ($this->record->pinged_at < time() - $this->env->workerPingInterval) {
83
            return;
84
        }
85
        if (!$this->record->refresh()) {
86
            $this->record->setIsNewRecord(true);
87
        }
88
        $this->record->pinged_at = time();
89
        $this->record->save(false);
90
91
        if ($this->record->isStopped()) {
92
            $event->exitCode = ExitCode::OK;
93
        }
94
    }
95
96
    /**
97
     * @param WorkerEvent $event
98
     */
99
    public function workerStop(WorkerEvent $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed. ( Ignorable by Annotation )

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

99
    public function workerStop(/** @scrutinizer ignore-unused */ WorkerEvent $event)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
100
    {
101
        if (!$this->env->canListenWorkerLoop()) {
102
            $this->env->db->close(); // To reopen a lost connection
103
        }
104
        $this->record->finished_at = time();
105
        $this->record->save(false);
106
    }
107
108
    /**
109
     * @param WorkerEvent $event
110
     * @throws
111
     * @return string
112
     */
113
    protected function getSenderName(WorkerEvent $event)
114
    {
115
        foreach (Yii::$app->getComponents(false) as $id => $component) {
116
            if ($component === $event->sender) {
117
                return $id;
118
            }
119
        }
120
        throw new InvalidConfigException('Queue must be an application component.');
121
    }
122
}
123