@@ -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 ()->hmset ( self :: generateId ( $ id), $ 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,25 @@ 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 ,
95- 'updated ' => time (),
108+ 'updated ' => time ()
96109 );
97- Resque::redis ()->set (( string ) $ this , json_encode ( $ statusPacket) );
110+ Resque::redis ()->hmset ( $ this -> id , $ statusPacket );
98111
99112 // Expire the status for completed jobs after 24 hours
100113 if (in_array ($ status , self ::$ completeStatuses )) {
101- Resque::redis ()->expire (( string ) $ this , 86400 );
114+ Resque::redis ()->expire ($ this -> id , 86400 );
102115 }
103116 }
104117
@@ -110,24 +123,60 @@ public function update($status)
110123 */
111124 public function get ()
112125 {
113- if (!$ this ->isTracking ()) {
114- return false ;
115- }
126+ return $ this ->fetch ('status ' );
127+ }
116128
117- $ statusPacket = json_decode (Resque::redis ()->get ((string )$ this ), true );
118- if (!$ statusPacket ) {
119- return false ;
120- }
129+ /**
130+ * Fetch the status for the job being monitored.
131+ *
132+ * @return mixed False if the status is not being monitored, otherwise the status as
133+ * as an integer, based on the Resque_Job_Status constants.
134+ */
135+ public function status ()
136+ {
137+ return $ this ->fetch ('status ' );
138+ }
139+
140+ /**
141+ * Fetch the updated timestamp for the job being monitored.
142+ *
143+ * @return mixed False if the status is not being monitored, otherwise the updated timestamp
144+ */
145+ public function updated ()
146+ {
147+ return $ this ->fetch ('updated ' );
148+ }
149+
150+ /**
151+ * Fetch the created timestamp for the job being monitored.
152+ *
153+ * @return mixed False if the status is not being monitored, otherwise the created timestamp
154+ */
155+ public function created ()
156+ {
157+ return $ this ->fetch ('created ' );
158+ }
121159
122- return $ statusPacket ['status ' ];
160+ /**
161+ * Fetch the created timestamp for the job being monitored.
162+ *
163+ * @return mixed False if the status is not being monitored, otherwise the created timestamp
164+ */
165+ private function fetch ($ key )
166+ {
167+ $ val = Resque::redis ()->hget ($ this ->id , $ key );
168+ if ($ val ) {
169+ return (int )$ val ;
170+ }
171+ return false ;
123172 }
124173
125174 /**
126175 * Stop tracking the status of a job.
127176 */
128177 public function stop ()
129178 {
130- Resque::redis ()->del (( string ) $ this );
179+ Resque::redis ()->del ($ this -> id );
131180 }
132181
133182 /**
@@ -137,7 +186,7 @@ public function stop()
137186 */
138187 public function __toString ()
139188 {
140- return ' job: ' . $ this ->id . ' :status ' ;
189+ return $ this ->id ;
141190 }
142191}
143- ?>
192+ ?>
0 commit comments