@@ -39,7 +39,14 @@ class Resque_Job_Status
3939 */
4040 public function __construct ($ id )
4141 {
42- $ this ->id = $ id ;
42+ $ this ->id = self ::generateId ($ id );
43+ }
44+
45+ /**
46+ * generate job id consistently
47+ */
48+ private static function generateId ($ id ) {
49+ return 'job: ' . $ id . ':status ' ;
4350 }
4451
4552 /**
@@ -53,9 +60,9 @@ public static function create($id)
5360 $ statusPacket = array (
5461 'status ' => self ::STATUS_WAITING ,
5562 'updated ' => time (),
56- 'started ' => time (),
63+ 'started ' => time ()
5764 );
58- Resque::redis ()->set (' job: ' . $ id . ' :status ' , json_encode ($ statusPacket ));
65+ Resque::redis ()->set (self :: generateId ( $ id) , json_encode ($ statusPacket ));
5966 }
6067
6168 /**
@@ -70,7 +77,7 @@ public function isTracking()
7077 return false ;
7178 }
7279
73- if (!Resque::redis ()->exists (( string ) $ this )) {
80+ if (!Resque::redis ()->exists ($ this -> id )) {
7481 $ this ->isTracking = false ;
7582 return false ;
7683 }
@@ -86,19 +93,26 @@ public function isTracking()
8693 */
8794 public function update ($ status )
8895 {
96+ $ status = (int )$ status ;
97+
8998 if (!$ this ->isTracking ()) {
9099 return ;
91100 }
92101
102+ if ($ status < 1 || $ status > 4 ) {
103+ return ;
104+ }
105+
93106 $ statusPacket = array (
94107 'status ' => $ status ,
95108 'updated ' => time (),
109+ 'started ' => $ this ->fetch ('started ' )
96110 );
97- Resque::redis ()->set (( string ) $ this , json_encode ($ statusPacket ));
111+ Resque::redis ()->set ($ this -> id , json_encode ($ statusPacket ));
98112
99113 // Expire the status for completed jobs after 24 hours
100114 if (in_array ($ status , self ::$ completeStatuses )) {
101- Resque::redis ()->expire (( string ) $ this , 86400 );
115+ Resque::redis ()->expire ($ this -> id , 86400 );
102116 }
103117 }
104118
@@ -110,24 +124,68 @@ public function update($status)
110124 */
111125 public function get ()
112126 {
113- if (!$ this ->isTracking ()) {
114- return false ;
115- }
127+ return $ this ->status ();
128+ }
116129
117- $ statusPacket = json_decode (Resque::redis ()->get ((string )$ this ), true );
118- if (!$ statusPacket ) {
119- return false ;
120- }
130+ /**
131+ * Fetch the status for the job being monitored.
132+ *
133+ * @return mixed False if the status is not being monitored, otherwise the status as
134+ * as an integer, based on the Resque_Job_Status constants.
135+ */
136+ public function status ()
137+ {
138+ return $ this ->fetch ('status ' );
139+ }
140+
141+ /**
142+ * Fetch the updated timestamp for the job being monitored.
143+ *
144+ * @return mixed False if the status is not being monitored, otherwise the updated timestamp
145+ */
146+ public function updated ()
147+ {
148+ return $ this ->fetch ('updated ' );
149+ }
121150
122- return $ statusPacket ['status ' ];
151+ /**
152+ * Fetch the started timestamp for the job being monitored.
153+ *
154+ * @return mixed False if the status is not being monitored, otherwise the created timestamp
155+ */
156+ public function started ()
157+ {
158+ return $ this ->fetch ('started ' );
159+ }
160+
161+ /**
162+ * Fetch the status packet for the job being monitored.
163+ * @param optional string $field The field to get from the status packet
164+ *
165+ * @return mixed False if the status is not being monitored, otherwise the status packet array or the individual field
166+ */
167+ private function fetch ($ field = false )
168+ {
169+ $ statusPacket = Resque::redis ()->get ($ this ->id );
170+ if ($ statusPacket ) {
171+ $ statusPacket = json_decode ($ statusPacket , true );
172+ if ($ field ) {
173+ if (isset ($ statusPacket [$ field ])) {
174+ return (int )$ statusPacket [$ field ];
175+ }
176+ } else {
177+ return $ statusPacket ;
178+ }
179+ }
180+ return false ;
123181 }
124182
125183 /**
126184 * Stop tracking the status of a job.
127185 */
128186 public function stop ()
129187 {
130- Resque::redis ()->del (( string ) $ this );
188+ Resque::redis ()->del ($ this -> id );
131189 }
132190
133191 /**
@@ -137,7 +195,7 @@ public function stop()
137195 */
138196 public function __toString ()
139197 {
140- return ' job: ' . $ this ->id . ' :status ' ;
198+ return $ this ->id ;
141199 }
142200}
143- ?>
201+ ?>
0 commit comments