@@ -39,7 +39,7 @@ class Resque_Job_Status
39
39
*/
40
40
public function __construct ($ id )
41
41
{
42
- $ this ->id = $ id ;
42
+ $ this ->id = self :: generateId ( $ id) ;
43
43
}
44
44
45
45
/**
@@ -53,9 +53,9 @@ public static function create($id)
53
53
$ statusPacket = array (
54
54
'status ' => self ::STATUS_WAITING ,
55
55
'updated ' => time (),
56
- 'started ' => time (),
56
+ 'started ' => time ()
57
57
);
58
- Resque::redis ()->set (' job: ' . $ id . ' :status ' , json_encode ($ statusPacket ));
58
+ Resque::redis ()->set (self :: generateId ( $ id) , json_encode ($ statusPacket ));
59
59
}
60
60
61
61
/**
@@ -70,7 +70,7 @@ public function isTracking()
70
70
return false ;
71
71
}
72
72
73
- if (!Resque::redis ()->exists (( string ) $ this )) {
73
+ if (!Resque::redis ()->exists ($ this -> id )) {
74
74
$ this ->isTracking = false ;
75
75
return false ;
76
76
}
@@ -86,19 +86,26 @@ public function isTracking()
86
86
*/
87
87
public function update ($ status )
88
88
{
89
+ $ status = (int )$ status ;
90
+
89
91
if (!$ this ->isTracking ()) {
90
92
return ;
91
93
}
92
94
95
+ if ($ status < 1 || $ status > 4 ) {
96
+ return ;
97
+ }
98
+
93
99
$ statusPacket = array (
94
100
'status ' => $ status ,
95
101
'updated ' => time (),
102
+ 'started ' => $ this ->fetch ('started ' )
96
103
);
97
- Resque::redis ()->set (( string ) $ this , json_encode ($ statusPacket ));
104
+ Resque::redis ()->set ($ this -> id , json_encode ($ statusPacket ));
98
105
99
106
// Expire the status for completed jobs after 24 hours
100
107
if (in_array ($ status , self ::$ completeStatuses )) {
101
- Resque::redis ()->expire (( string ) $ this , 86400 );
108
+ Resque::redis ()->expire ($ this -> id , 86400 );
102
109
}
103
110
}
104
111
@@ -110,24 +117,46 @@ public function update($status)
110
117
*/
111
118
public function get ()
112
119
{
113
- if (!$ this ->isTracking ()) {
114
- return false ;
115
- }
120
+ return $ this ->status ();
121
+ }
116
122
117
- $ statusPacket = json_decode (Resque::redis ()->get ((string )$ this ), true );
118
- if (!$ statusPacket ) {
119
- return false ;
120
- }
123
+ /**
124
+ * Fetch the status for the job being monitored.
125
+ *
126
+ * @return mixed False if the status is not being monitored, otherwise the status as
127
+ * as an integer, based on the Resque_Job_Status constants.
128
+ */
129
+ public function status ()
130
+ {
131
+ return $ this ->fetch ('status ' );
132
+ }
133
+
134
+ /**
135
+ * Fetch the updated timestamp for the job being monitored.
136
+ *
137
+ * @return mixed False if the status is not being monitored, otherwise the updated timestamp
138
+ */
139
+ public function updated ()
140
+ {
141
+ return $ this ->fetch ('updated ' );
142
+ }
121
143
122
- return $ statusPacket ['status ' ];
144
+ /**
145
+ * Fetch the started timestamp for the job being monitored.
146
+ *
147
+ * @return mixed False if the status is not being monitored, otherwise the created timestamp
148
+ */
149
+ public function started ()
150
+ {
151
+ return $ this ->fetch ('started ' );
123
152
}
124
153
125
154
/**
126
155
* Stop tracking the status of a job.
127
156
*/
128
157
public function stop ()
129
158
{
130
- Resque::redis ()->del (( string ) $ this );
159
+ Resque::redis ()->del ($ this -> id );
131
160
}
132
161
133
162
/**
@@ -137,6 +166,38 @@ public function stop()
137
166
*/
138
167
public function __toString ()
139
168
{
140
- return 'job: ' . $ this ->id . ':status ' ;
169
+ return $ this ->id ;
170
+ }
171
+
172
+ /**
173
+ * generate job status id key in a consistent manner
174
+ *
175
+ * @return string String redis key for the current job status
176
+ */
177
+ protected static function generateId ($ id )
178
+ {
179
+ return 'job: ' . $ id . ':status ' ;
180
+ }
181
+
182
+ /**
183
+ * Fetch the status packet for the job being monitored.
184
+ * @param optional string $field The field to get from the status packet
185
+ *
186
+ * @return mixed False if the status is not being monitored, otherwise the status packet array or the individual field
187
+ */
188
+ protected function fetch ($ field = false )
189
+ {
190
+ $ statusPacket = Resque::redis ()->get ($ this ->id );
191
+ if ($ statusPacket ) {
192
+ $ statusPacket = json_decode ($ statusPacket , true );
193
+ if ($ field ) {
194
+ if (isset ($ statusPacket [$ field ])) {
195
+ return (int )$ statusPacket [$ field ];
196
+ }
197
+ } else {
198
+ return $ statusPacket ;
199
+ }
200
+ }
201
+ return false ;
141
202
}
142
203
}
0 commit comments