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) |
|
|
|
|
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
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.