-
Notifications
You must be signed in to change notification settings - Fork 740
Expand file tree
/
Copy pathStatus.php
More file actions
203 lines (182 loc) · 4.59 KB
/
Copy pathStatus.php
File metadata and controls
203 lines (182 loc) · 4.59 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
<?php
/**
* 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 Resque_Job_Status
{
const STATUS_WAITING = 1;
const STATUS_RUNNING = 2;
const STATUS_FAILED = 3;
const STATUS_COMPLETE = 4;
/**
* @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)
{
$this->id = self::generateId($id);
}
/**
* 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)
{
$statusPacket = array(
'status' => self::STATUS_WAITING,
'updated' => time(),
'started' => time()
);
Resque::redis()->set(self::generateId($id), json_encode($statusPacket));
}
/**
* 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($this->id)) {
$this->isTracking = false;
return false;
}
$this->isTracking = true;
return true;
}
/**
* Update the status indicator for the current job with a new status.
*
* @param int The status of the job (see constants in Resque_Job_Status)
*/
public function update($status)
{
$status = (int)$status;
if(!$this->isTracking()) {
return;
}
if($status < 1 || $status > 4) {
return;
}
$statusPacket = array(
'status' => $status,
'updated' => time(),
'started' => $this->fetch('started')
);
Resque::redis()->set($this->id, json_encode($statusPacket));
// Expire the status for completed jobs after 24 hours
if(in_array($status, self::$completeStatuses)) {
Resque::redis()->expire($this->id, 86400);
}
}
/**
* Fetch the status for the job being monitored.
*
* @return mixed False if the status is not being monitored, otherwise the status as
* 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
* as an integer, based on the Resque_Job_Status constants.
*/
public function status()
{
return $this->fetch('status');
}
/**
* Fetch the updated timestamp for the job being monitored.
*
* @return mixed False if the status is not being monitored, otherwise the updated timestamp
*/
public function updated()
{
return $this->fetch('updated');
}
/**
* Fetch the started timestamp for the job being monitored.
*
* @return mixed False if the status is not being monitored, otherwise the created timestamp
*/
public function started()
{
return $this->fetch('started');
}
/**
* Stop tracking the status of a job.
*/
public function stop()
{
Resque::redis()->del($this->id);
}
/**
* Generate a string representation of this object.
*
* @return string String representation of the current job status class.
*/
public function __toString()
{
return $this->id;
}
/**
* generate job status id key in a consistent manner
*
* @return string String redis key for the current job status
*/
protected static function generateId($id)
{
return 'job:' . $id . ':status';
}
/**
* Fetch the status packet for the job being monitored.
* @param optional string $field The field to get from the status packet
*
* @return mixed False if the status is not being monitored, otherwise the status packet array or the individual field
*/
protected function fetch($field = false)
{
$statusPacket = Resque::redis()->get($this->id);
if($statusPacket) {
$statusPacket = json_decode($statusPacket, true);
if($field) {
if(isset($statusPacket[$field])) {
return (int)$statusPacket[$field];
}
} else {
return $statusPacket;
}
}
return false;
}
}