|
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 zhuravljov\yii\queue\monitor\Env; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class ExecRecord |
|
16
|
|
|
* |
|
17
|
|
|
* @property int $id |
|
18
|
|
|
* @property int $push_id |
|
19
|
|
|
* @property null|int $worker_id |
|
20
|
|
|
* @property int $attempt |
|
21
|
|
|
* @property int $reserved_at |
|
22
|
|
|
* @property null|int $done_at |
|
23
|
|
|
* @property null|string $error |
|
24
|
|
|
* @property null|bool $retry |
|
25
|
|
|
* |
|
26
|
|
|
* @property PushRecord $push |
|
27
|
|
|
* @property null|WorkerRecord $worker |
|
28
|
|
|
* |
|
29
|
|
|
* @property int $duration |
|
30
|
|
|
* @property false|string $errorMessage |
|
31
|
|
|
* |
|
32
|
|
|
* @author Roman Zhuravlev <[email protected]> |
|
33
|
|
|
*/ |
|
34
|
|
|
class ExecRecord extends ActiveRecord |
|
35
|
|
|
{ |
|
36
|
|
|
/** |
|
37
|
|
|
* @inheritdoc |
|
38
|
|
|
* @return ExecQuery the active query used by this AR class. |
|
39
|
|
|
*/ |
|
40
|
|
|
public static function find() |
|
41
|
|
|
{ |
|
42
|
|
|
return new ExecQuery(get_called_class()); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @inheritdoc |
|
47
|
|
|
*/ |
|
48
|
|
|
public static function getDb() |
|
49
|
|
|
{ |
|
50
|
|
|
return Yii::$container->get(Env::class)->db; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @inheritdoc |
|
55
|
|
|
*/ |
|
56
|
|
|
public static function tableName() |
|
57
|
|
|
{ |
|
58
|
|
|
return Yii::$container->get(Env::class)->execTableName; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return PushQuery |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getPush() |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->hasOne(PushRecord::class, ['id' => 'push_id']); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @return WorkerQuery |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getWorker() |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->hasOne(WorkerRecord::class, ['id' => 'worker_id']); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return int |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getDuration() |
|
81
|
|
|
{ |
|
82
|
|
|
if ($this->done_at) { |
|
|
|
|
|
|
83
|
|
|
return $this->done_at - $this->reserved_at; |
|
84
|
|
|
} else { |
|
85
|
|
|
return time() - $this->reserved_at; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return bool |
|
91
|
|
|
*/ |
|
92
|
|
|
public function isDone() |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->done_at !== null; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @return bool |
|
99
|
|
|
*/ |
|
100
|
|
|
public function isFailed() |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->error !== null; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @return false|string first error line |
|
107
|
|
|
*/ |
|
108
|
|
|
public function getErrorMessage() |
|
109
|
|
|
{ |
|
110
|
|
|
if ($this->_errorMessage === null) { |
|
111
|
|
|
$this->_errorMessage = false; |
|
112
|
|
|
if ($this->error !== null) { |
|
113
|
|
|
$this->_errorMessage = trim(explode("\n", $this->error, 2)[0]); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
return $this->_errorMessage; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
private $_errorMessage; |
|
120
|
|
|
} |
|
121
|
|
|
|
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: