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
|
|
|
* Exec Record |
16
|
|
|
* |
17
|
|
|
* @property int $id |
18
|
|
|
* @property int $push_id |
19
|
|
|
* @property null|int $worker_id |
20
|
|
|
* @property int $attempt |
21
|
|
|
* @property int $started_at |
22
|
|
|
* @property null|int $finished_at |
23
|
|
|
* @property null|int $memory_usage |
24
|
|
|
* @property null|string $error |
25
|
|
|
* @property null|string|resource $result_data |
26
|
|
|
* @property null|bool $retry |
27
|
|
|
* |
28
|
|
|
* @property PushRecord $push |
29
|
|
|
* @property null|WorkerRecord $worker |
30
|
|
|
* |
31
|
|
|
* @property int $duration |
32
|
|
|
* @property false|string $errorMessage |
33
|
|
|
* |
34
|
|
|
* @author Roman Zhuravlev <[email protected]> |
35
|
|
|
*/ |
36
|
|
|
class ExecRecord extends ActiveRecord |
37
|
|
|
{ |
38
|
|
|
private $_errorMessage; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @inheritdoc |
42
|
|
|
* @return ExecQuery the active query used by this AR class. |
43
|
|
|
*/ |
44
|
|
|
public static function find() |
45
|
|
|
{ |
46
|
|
|
return Yii::createObject(ExecQuery::class, [get_called_class()]); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @inheritdoc |
51
|
|
|
*/ |
52
|
|
|
public static function getDb() |
53
|
|
|
{ |
54
|
|
|
return Env::ensure()->db; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @inheritdoc |
59
|
|
|
*/ |
60
|
|
|
public static function tableName() |
61
|
|
|
{ |
62
|
|
|
return Env::ensure()->execTableName; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return PushQuery |
67
|
|
|
*/ |
68
|
|
|
public function getPush() |
69
|
|
|
{ |
70
|
|
|
return $this->hasOne(PushRecord::class, ['id' => 'push_id']); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return WorkerQuery |
75
|
|
|
*/ |
76
|
|
|
public function getWorker() |
77
|
|
|
{ |
78
|
|
|
return $this->hasOne(WorkerRecord::class, ['id' => 'worker_id']); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return int |
83
|
|
|
*/ |
84
|
|
|
public function getDuration() |
85
|
|
|
{ |
86
|
|
|
if ($this->finished_at) { |
|
|
|
|
87
|
|
|
return $this->finished_at - $this->started_at; |
88
|
|
|
} |
89
|
|
|
return time() - $this->started_at; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
|
|
public function isDone() |
96
|
|
|
{ |
97
|
|
|
return $this->finished_at !== null; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return bool |
102
|
|
|
*/ |
103
|
|
|
public function isFailed() |
104
|
|
|
{ |
105
|
|
|
return $this->error !== null; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return false|string first error line |
110
|
|
|
*/ |
111
|
|
|
public function getErrorMessage() |
112
|
|
|
{ |
113
|
|
|
if ($this->_errorMessage === null) { |
114
|
|
|
$this->_errorMessage = false; |
115
|
|
|
if ($this->error !== null) { |
116
|
|
|
$this->_errorMessage = trim(explode("\n", $this->error, 2)[0]); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
return $this->_errorMessage; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function getResult() |
123
|
|
|
{ |
124
|
|
|
if (is_resource($this->result_data)) { |
125
|
|
|
$this->result_data = stream_get_contents($this->result_data); |
126
|
|
|
} |
127
|
|
|
if ($this->result_data) { |
128
|
|
|
return unserialize($this->result_data); |
129
|
|
|
} |
130
|
|
|
return null; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: