1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Resque\Job; |
4
|
|
|
|
5
|
|
|
use \Resque\Resque; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Status tracker/information for a job. |
9
|
|
|
* |
10
|
|
|
* @package Resque/Job |
11
|
|
|
* @author Chris Boulton <[email protected]> |
12
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php |
13
|
|
|
*/ |
14
|
|
|
class Status |
15
|
|
|
{ |
16
|
|
|
const STATUS_WAITING = 1; |
17
|
|
|
const STATUS_RUNNING = 2; |
18
|
|
|
const STATUS_FAILED = 3; |
19
|
|
|
const STATUS_COMPLETE = 4; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string The prefix of the job status id. |
23
|
|
|
*/ |
24
|
|
|
private $prefix; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string The ID of the job this status class refers back to. |
28
|
|
|
*/ |
29
|
|
|
private $id; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var mixed Cache variable if the status of this job is being monitored or not. |
33
|
|
|
* True/false when checked at least once or null if not checked yet. |
34
|
|
|
*/ |
35
|
|
|
private $isTracking = null; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array Array of statuses that are considered final/complete. |
39
|
|
|
*/ |
40
|
|
|
private static $completeStatuses = array( |
41
|
|
|
self::STATUS_FAILED, |
42
|
|
|
self::STATUS_COMPLETE |
43
|
|
|
); |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Setup a new instance of the job monitor class for the supplied job ID. |
47
|
|
|
* |
48
|
|
|
* @param string $id The ID of the job to manage the status for. |
49
|
|
|
*/ |
50
|
|
|
public function __construct($id, $prefix = '') |
51
|
|
|
{ |
52
|
|
|
$this->id = $id; |
53
|
|
|
$this->prefix = empty($prefix) ? '' : "${prefix}_"; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Create a new status monitor item for the supplied job ID. Will create |
58
|
|
|
* all necessary keys in Redis to monitor the status of a job. |
59
|
|
|
* |
60
|
|
|
* @param string $id The ID of the job to monitor the status of. |
61
|
|
|
*/ |
62
|
|
|
public static function create($id, $prefix = "") |
63
|
|
|
{ |
64
|
|
|
$status = new self($id, $prefix); |
65
|
|
|
$statusPacket = array( |
66
|
|
|
'status' => self::STATUS_WAITING, |
67
|
|
|
'updated' => time(), |
68
|
|
|
'started' => time(), |
69
|
|
|
'result' => null, |
70
|
|
|
); |
71
|
|
|
Resque::redis()->set((string) $status, json_encode($statusPacket)); |
|
|
|
|
72
|
|
|
|
73
|
|
|
return $status; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Check if we're actually checking the status of the loaded job status |
78
|
|
|
* instance. |
79
|
|
|
* |
80
|
|
|
* @return boolean True if the status is being monitored, false if not. |
81
|
|
|
*/ |
82
|
|
|
public function isTracking() |
83
|
|
|
{ |
84
|
|
|
if ($this->isTracking === false) { |
85
|
|
|
return false; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if (!Resque::redis()->exists((string)$this)) { |
|
|
|
|
89
|
|
|
$this->isTracking = false; |
90
|
|
|
return false; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$this->isTracking = true; |
94
|
|
|
return true; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Update the status indicator for the current job with a new status. |
99
|
|
|
* |
100
|
|
|
* @param int The status of the job (see constants in Resque\Job\Status) |
|
|
|
|
101
|
|
|
*/ |
102
|
|
|
public function update($status, $result = null) |
103
|
|
|
{ |
104
|
|
|
$status = (int) $status; |
105
|
|
|
|
106
|
|
|
if (!$this->isTracking()) { |
107
|
|
|
return; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if ($status < self::STATUS_WAITING || $status > self::STATUS_COMPLETE) { |
111
|
|
|
return; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$statusPacket = array( |
115
|
|
|
'status' => $status, |
116
|
|
|
'updated' => time(), |
117
|
|
|
'started' => $this->fetch('started'), |
118
|
|
|
'result' => $result, |
119
|
|
|
); |
120
|
|
|
Resque::redis()->set((string)$this, json_encode($statusPacket)); |
121
|
|
|
|
122
|
|
|
// Expire the status for completed jobs after 24 hours |
123
|
|
|
if (in_array($status, self::$completeStatuses)) { |
124
|
|
|
Resque::redis()->expire((string)$this, 86400); |
|
|
|
|
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Fetch the status for the job being monitored. |
130
|
|
|
* |
131
|
|
|
* @return mixed False if the status is not being monitored, otherwise the status |
132
|
|
|
* as an integer, based on the Resque\Job\Status constants. |
133
|
|
|
*/ |
134
|
|
|
public function get() |
135
|
|
|
{ |
136
|
|
|
return $this->status(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Fetch the status for the job being monitored. |
141
|
|
|
* |
142
|
|
|
* @return mixed False if the status is not being monitored, otherwise the status |
143
|
|
|
* as an integer, based on the Resque\Job\Status constants. |
144
|
|
|
*/ |
145
|
|
|
public function status() |
146
|
|
|
{ |
147
|
|
|
return $this->fetch('status'); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Fetch the last update timestamp of the job being monitored. |
152
|
|
|
* |
153
|
|
|
* @return mixed False if the job is not being monitored, otherwise the |
154
|
|
|
* update timestamp. |
155
|
|
|
*/ |
156
|
|
|
public function updated() |
157
|
|
|
{ |
158
|
|
|
return $this->fetch('updated'); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Fetch the start timestamp of the job being monitored. |
163
|
|
|
* |
164
|
|
|
* @return mixed False if the job is not being monitored, otherwise the |
165
|
|
|
* start timestamp. |
166
|
|
|
*/ |
167
|
|
|
public function started() |
168
|
|
|
{ |
169
|
|
|
return $this->fetch('started'); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Fetch the result of the job being monitored. |
174
|
|
|
* |
175
|
|
|
* @return mixed False if the job is not being monitored, otherwise the result |
176
|
|
|
* as mixed |
177
|
|
|
*/ |
178
|
|
|
public function result() |
179
|
|
|
{ |
180
|
|
|
return $this->fetch('result'); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Stop tracking the status of a job. |
185
|
|
|
*/ |
186
|
|
|
public function stop() |
187
|
|
|
{ |
188
|
|
|
Resque::redis()->del((string)$this); |
|
|
|
|
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Generate a string representation of this object. |
193
|
|
|
* |
194
|
|
|
* @return string String representation of the current job status class. |
195
|
|
|
*/ |
196
|
|
|
public function __toString() |
197
|
|
|
{ |
198
|
|
|
return 'job:' . $this->prefix . $this->id . ':status'; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Fetch a value from the status packet for the job being monitored. |
203
|
|
|
* |
204
|
|
|
* @return mixed False if the status is not being monitored, otherwise the |
205
|
|
|
* requested value from the status packet. |
206
|
|
|
*/ |
207
|
|
|
protected function fetch($value = null) |
208
|
|
|
{ |
209
|
|
|
if (!$this->isTracking()) { |
210
|
|
|
return false; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
$statusPacket = json_decode(Resque::redis()->get((string)$this), true); |
|
|
|
|
214
|
|
|
if (!$statusPacket) { |
215
|
|
|
return false; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
if (empty($value)) { |
219
|
|
|
return $statusPacket; |
220
|
|
|
} else { |
221
|
|
|
if (isset($statusPacket[$value])) { |
222
|
|
|
return $statusPacket[$value]; |
223
|
|
|
} else { |
224
|
|
|
return null; |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|