|
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->host = $this->env->getHost(); |
|
72
|
|
|
$this->record->pid = $event->sender->getWorkerPid(); |
|
73
|
|
|
$this->record->started_at = time(); |
|
74
|
|
|
$this->record->pinged_at = time(); |
|
75
|
|
|
$this->record->save(false); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param WorkerEvent $event |
|
80
|
|
|
*/ |
|
81
|
|
|
public function workerLoop(WorkerEvent $event) |
|
82
|
|
|
{ |
|
83
|
|
|
if ($this->record->pinged_at + $this->env->workerPingInterval > time()) { |
|
84
|
|
|
return; |
|
85
|
|
|
} |
|
86
|
|
|
if (!$this->record->refresh()) { |
|
87
|
|
|
$this->record->setIsNewRecord(true); |
|
88
|
|
|
} |
|
89
|
|
|
$this->record->pinged_at = time(); |
|
90
|
|
|
$this->record->save(false); |
|
91
|
|
|
|
|
92
|
|
|
if ($this->record->isStopped()) { |
|
93
|
|
|
$event->exitCode = ExitCode::OK; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param WorkerEvent $event |
|
99
|
|
|
*/ |
|
100
|
|
|
public function workerStop(WorkerEvent $event) |
|
|
|
|
|
|
101
|
|
|
{ |
|
102
|
|
|
if (!$this->env->canListenWorkerLoop()) { |
|
103
|
|
|
$this->env->db->close(); // To reopen a lost connection |
|
104
|
|
|
} |
|
105
|
|
|
$this->record->finished_at = time(); |
|
106
|
|
|
$this->record->save(false); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param WorkerEvent $event |
|
111
|
|
|
* @throws |
|
112
|
|
|
* @return string |
|
113
|
|
|
*/ |
|
114
|
|
|
protected function getSenderName(WorkerEvent $event) |
|
115
|
|
|
{ |
|
116
|
|
|
foreach (Yii::$app->getComponents(false) as $id => $component) { |
|
117
|
|
|
if ($component === $event->sender) { |
|
118
|
|
|
return $id; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
throw new InvalidConfigException('Queue must be an application component.'); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.