@@ -39,7 +39,7 @@ class Resque_Job_Status
3939 */
4040 public function __construct ($ id )
4141 {
42- $ this ->id = $ id ;
42+ $ this ->id = self :: generateId ( $ id) ;
4343 }
4444
4545 /**
@@ -53,9 +53,9 @@ public static function create($id)
5353 $ statusPacket = array (
5454 'status ' => self ::STATUS_WAITING ,
5555 'updated ' => time (),
56- 'started ' => time (),
56+ 'started ' => time ()
5757 );
58- Resque::redis ()->set (' job: ' . $ id . ' :status ' , json_encode ($ statusPacket ));
58+ Resque::redis ()->set (self :: generateId ( $ id) , json_encode ($ statusPacket ));
5959 }
6060
6161 /**
@@ -70,7 +70,7 @@ public function isTracking()
7070 return false ;
7171 }
7272
73- if (!Resque::redis ()->exists (( string ) $ this )) {
73+ if (!Resque::redis ()->exists ($ this -> id )) {
7474 $ this ->isTracking = false ;
7575 return false ;
7676 }
@@ -86,19 +86,26 @@ public function isTracking()
8686 */
8787 public function update ($ status )
8888 {
89+ $ status = (int )$ status ;
90+
8991 if (!$ this ->isTracking ()) {
9092 return ;
9193 }
9294
95+ if ($ status < 1 || $ status > 4 ) {
96+ return ;
97+ }
98+
9399 $ statusPacket = array (
94100 'status ' => $ status ,
95101 'updated ' => time (),
102+ 'started ' => $ this ->fetch ('started ' )
96103 );
97- Resque::redis ()->set (( string ) $ this , json_encode ($ statusPacket ));
104+ Resque::redis ()->set ($ this -> id , json_encode ($ statusPacket ));
98105
99106 // Expire the status for completed jobs after 24 hours
100107 if (in_array ($ status , self ::$ completeStatuses )) {
101- Resque::redis ()->expire (( string ) $ this , 86400 );
108+ Resque::redis ()->expire ($ this -> id , 86400 );
102109 }
103110 }
104111
@@ -110,24 +117,46 @@ public function update($status)
110117 */
111118 public function get ()
112119 {
113- if (!$ this ->isTracking ()) {
114- return false ;
115- }
120+ return $ this ->status ();
121+ }
116122
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+ }
121143
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 ' );
123152 }
124153
125154 /**
126155 * Stop tracking the status of a job.
127156 */
128157 public function stop ()
129158 {
130- Resque::redis ()->del (( string ) $ this );
159+ Resque::redis ()->del ($ this -> id );
131160 }
132161
133162 /**
@@ -137,6 +166,38 @@ public function stop()
137166 */
138167 public function __toString ()
139168 {
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 ;
141202 }
142203}
0 commit comments