@@ -96,14 +96,20 @@ public function isTracking()
96
96
*/
97
97
public function update ($ status , $ result = null )
98
98
{
99
+ $ status = (int ) $ status ;
100
+
99
101
if (!$ this ->isTracking ()) {
100
102
return ;
101
103
}
102
104
105
+ if ($ status < self ::STATUS_WAITING || $ status > self ::STATUS_COMPLETE ) {
106
+ return ;
107
+ }
108
+
103
109
$ statusPacket = array (
104
110
'status ' => $ status ,
105
111
'updated ' => time (),
106
- 'started ' => $ this ->getValue ('started ' ),
112
+ 'started ' => $ this ->fetch ('started ' ),
107
113
'result ' => $ result ,
108
114
);
109
115
Resque::redis ()->set ((string )$ this , json_encode ($ statusPacket ));
@@ -115,23 +121,14 @@ public function update($status, $result = null)
115
121
}
116
122
117
123
/**
118
- * Fetch a value from the status packet for the job being monitored.
124
+ * Fetch the status for the job being monitored.
119
125
*
120
- * @return mixed False if the status is not being monitored, otherwise the
121
- * requested value from the status packet .
126
+ * @return mixed False if the status is not being monitored, otherwise the status
127
+ * as an integer, based on the Resque_Job_Status constants .
122
128
*/
123
- protected function getValue ( $ value = null )
129
+ public function get ( )
124
130
{
125
- if (!$ this ->isTracking ()) {
126
- return false ;
127
- }
128
-
129
- $ statusPacket = json_decode (Resque::redis ()->get ((string )$ this ), true );
130
- if (!$ statusPacket ) {
131
- return false ;
132
- }
133
-
134
- return empty ($ value ) ? $ statusPacket : $ statusPacket [$ value ];
131
+ return $ this ->status ();
135
132
}
136
133
137
134
/**
@@ -140,20 +137,42 @@ protected function getValue($value = null)
140
137
* @return mixed False if the status is not being monitored, otherwise the status
141
138
* as an integer, based on the Resque_Job_Status constants.
142
139
*/
143
- public function get ()
140
+ public function status ()
144
141
{
145
- return $ this ->getValue ('status ' );
142
+ return $ this ->fetch ('status ' );
146
143
}
147
144
145
+ /**
146
+ * Fetch the last update timestamp of the job being monitored.
147
+ *
148
+ * @return mixed False if the job is not being monitored, otherwise the
149
+ * update timestamp.
150
+ */
151
+ public function updated ()
152
+ {
153
+ return $ this ->fetch ('updated ' );
154
+ }
155
+
156
+ /**
157
+ * Fetch the start timestamp of the job being monitored.
158
+ *
159
+ * @return mixed False if the job is not being monitored, otherwise the
160
+ * start timestamp.
161
+ */
162
+ public function started ()
163
+ {
164
+ return $ this ->fetch ('started ' );
165
+ }
166
+
148
167
/**
149
168
* Fetch the result of the job being monitored.
150
169
*
151
170
* @return mixed False if the job is not being monitored, otherwise the result
152
171
* as mixed
153
172
*/
154
- public function getResult ()
173
+ public function result ()
155
174
{
156
- return $ this ->getValue ('result ' );
175
+ return $ this ->fetch ('result ' );
157
176
}
158
177
159
178
/**
@@ -173,4 +192,33 @@ public function __toString()
173
192
{
174
193
return 'job: ' . $ this ->prefix . $ this ->id . ':status ' ;
175
194
}
195
+
196
+ /**
197
+ * Fetch a value from the status packet for the job being monitored.
198
+ *
199
+ * @return mixed False if the status is not being monitored, otherwise the
200
+ * requested value from the status packet.
201
+ */
202
+ protected function fetch ($ value = null )
203
+ {
204
+ if (!$ this ->isTracking ()) {
205
+ return false ;
206
+ }
207
+
208
+ $ statusPacket = json_decode (Resque::redis ()->get ((string )$ this ), true );
209
+ if (!$ statusPacket ) {
210
+ return false ;
211
+ }
212
+
213
+ if (empty ($ value )) {
214
+ return $ statusPacket ;
215
+ } else {
216
+ if (isset ($ statusPacket [$ value ])) {
217
+ return $ statusPacket [$ value ];
218
+ } else {
219
+ return null ;
220
+ }
221
+ }
222
+
223
+ }
176
224
}
0 commit comments