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