|
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\records; |
|
9
|
|
|
|
|
10
|
|
|
use Yii; |
|
11
|
|
|
use yii\db\ActiveRecord; |
|
12
|
|
|
use yii\helpers\ArrayHelper; |
|
13
|
|
|
use zhuravljov\yii\queue\monitor\Env; |
|
14
|
|
|
use zhuravljov\yii\queue\monitor\Module; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Worker Record |
|
18
|
|
|
* |
|
19
|
|
|
* @property int $id |
|
20
|
|
|
* @property string $sender_name |
|
21
|
|
|
* @property string $host |
|
22
|
|
|
* @property int $pid |
|
23
|
|
|
* @property int $started_at |
|
24
|
|
|
* @property int $pinged_at |
|
25
|
|
|
* @property null|int $stopped_at |
|
26
|
|
|
* @property null|int $finished_at |
|
27
|
|
|
* @property null|int $last_exec_id |
|
28
|
|
|
* |
|
29
|
|
|
* @property null|ExecRecord $lastExec |
|
30
|
|
|
* @property ExecRecord[] $execs |
|
31
|
|
|
* @property array $execTotal |
|
32
|
|
|
* |
|
33
|
|
|
* @property string $status |
|
34
|
|
|
* @property int $execTotalStarted |
|
35
|
|
|
* @property int $execTotalDone |
|
36
|
|
|
* @property int $duration |
|
37
|
|
|
* |
|
38
|
|
|
* @author Roman Zhuravlev <[email protected]> |
|
39
|
|
|
*/ |
|
40
|
|
|
class WorkerRecord extends ActiveRecord |
|
41
|
|
|
{ |
|
42
|
|
|
/** |
|
43
|
|
|
* @inheritdoc |
|
44
|
|
|
* @return WorkerQuery|object the active query used by this AR class. |
|
45
|
|
|
*/ |
|
46
|
|
|
public static function find() |
|
47
|
|
|
{ |
|
48
|
|
|
return Yii::createObject(WorkerQuery::class, [get_called_class()]); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @inheritdoc |
|
53
|
|
|
*/ |
|
54
|
|
|
public static function getDb() |
|
55
|
|
|
{ |
|
56
|
|
|
return Env::ensure()->db; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @inheritdoc |
|
61
|
|
|
*/ |
|
62
|
|
|
public static function tableName() |
|
63
|
|
|
{ |
|
64
|
|
|
return Env::ensure()->workerTableName; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function attributeLabels() |
|
68
|
|
|
{ |
|
69
|
|
|
return [ |
|
70
|
|
|
'id' => Module::t('main', 'ID'), |
|
71
|
|
|
'sender_name' => Module::t('main', 'Sender'), |
|
72
|
|
|
'host' => Module::t('main', 'Host'), |
|
73
|
|
|
'pid' => Module::t('main', 'PID'), |
|
74
|
|
|
'status' => Module::t('main', 'Status'), |
|
75
|
|
|
'started_at' => Module::t('main', 'Started At'), |
|
76
|
|
|
'execTotalStarted' => Module::t('main', 'Total Started'), |
|
77
|
|
|
'execTotalDone' => Module::t('main', 'Total Done'), |
|
78
|
|
|
]; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @return ExecQuery|\yii\db\ActiveQuery |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getLastExec() |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->hasOne(ExecRecord::class, ['id' => 'last_exec_id']); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return ExecQuery|\yii\db\ActiveQuery |
|
91
|
|
|
*/ |
|
92
|
|
|
public function getExecs() |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->hasMany(ExecRecord::class, ['worker_id' => 'id']); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @return ExecQuery|\yii\db\ActiveQuery |
|
99
|
|
|
*/ |
|
100
|
|
|
public function getExecTotal() |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->hasOne(ExecRecord::class, ['worker_id' => 'id']) |
|
103
|
|
|
->select([ |
|
104
|
|
|
'exec.worker_id', |
|
105
|
|
|
'started' => 'COUNT(*)', |
|
106
|
|
|
'done' => 'COUNT(exec.finished_at)', |
|
107
|
|
|
]) |
|
108
|
|
|
->groupBy('worker_id') |
|
109
|
|
|
->asArray(); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @return int |
|
114
|
|
|
*/ |
|
115
|
|
|
public function getExecTotalStarted() |
|
116
|
|
|
{ |
|
117
|
|
|
return ArrayHelper::getValue($this->execTotal, 'started', 0); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @return int |
|
122
|
|
|
*/ |
|
123
|
|
|
public function getExecTotalDone() |
|
124
|
|
|
{ |
|
125
|
|
|
return ArrayHelper::getValue($this->execTotal, 'done', 0); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @return int |
|
130
|
|
|
*/ |
|
131
|
|
|
public function getDuration() |
|
132
|
|
|
{ |
|
133
|
|
|
if ($this->finished_at) { |
|
|
|
|
|
|
134
|
|
|
return $this->finished_at - $this->started_at; |
|
135
|
|
|
} |
|
136
|
|
|
return time() - $this->started_at; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @return string |
|
141
|
|
|
*/ |
|
142
|
|
|
public function getStatus() |
|
143
|
|
|
{ |
|
144
|
|
|
$format = Module::getInstance()->formatter; |
|
145
|
|
|
if (!$this->lastExec) { |
|
146
|
|
|
return Module::t('main', 'Idle since {time}.', [ |
|
147
|
|
|
'time' => $format->asRelativeTime($this->started_at), |
|
148
|
|
|
]); |
|
149
|
|
|
} |
|
150
|
|
|
if ($this->lastExec->finished_at) { |
|
|
|
|
|
|
151
|
|
|
return Module::t('main', 'Idle after a job since {time}.', [ |
|
152
|
|
|
'time' => $format->asRelativeTime($this->lastExec->finished_at), |
|
153
|
|
|
]); |
|
154
|
|
|
} |
|
155
|
|
|
return Module::t('main', 'Busy since {time}.', [ |
|
156
|
|
|
'time' => $format->asRelativeTime($this->lastExec->started_at), |
|
157
|
|
|
]); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @return bool |
|
162
|
|
|
*/ |
|
163
|
|
|
public function isIdle() |
|
164
|
|
|
{ |
|
165
|
|
|
return !$this->lastExec || $this->lastExec->finished_at; |
|
|
|
|
|
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @return bool marked as stopped |
|
170
|
|
|
*/ |
|
171
|
|
|
public function isStopped() |
|
172
|
|
|
{ |
|
173
|
|
|
return !!$this->stopped_at; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Marks as stopped |
|
178
|
|
|
*/ |
|
179
|
|
|
public function stop() |
|
180
|
|
|
{ |
|
181
|
|
|
$this->stopped_at = time(); |
|
182
|
|
|
$this->save(false); |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: