@@ -13,6 +13,11 @@ class Resque_Job_Status
1313 const STATUS_FAILED = 3 ;
1414 const STATUS_COMPLETE = 4 ;
1515
16+ /**
17+ * @var string The prefix of the job status id.
18+ */
19+ private $ prefix ;
20+
1621 /**
1722 * @var string The ID of the job this status class refers back to.
1823 */
@@ -37,9 +42,10 @@ class Resque_Job_Status
3742 *
3843 * @param string $id The ID of the job to manage the status for.
3944 */
40- public function __construct ($ id )
45+ public function __construct ($ id, $ prefix = '' )
4146 {
4247 $ this ->id = $ id ;
48+ $ this ->prefix = empty ($ prefix ) ? '' : "$ {prefix}_ " ;
4349 }
4450
4551 /**
@@ -48,14 +54,18 @@ public function __construct($id)
4854 *
4955 * @param string $id The ID of the job to monitor the status of.
5056 */
51- public static function create ($ id )
57+ public static function create ($ id, $ prefix = "" )
5258 {
59+ $ status = new self ($ id , $ prefix );
5360 $ statusPacket = array (
54- 'status ' => self ::STATUS_WAITING ,
61+ 'status ' => self ::STATUS_WAITING ,
5562 'updated ' => time (),
5663 'started ' => time (),
64+ 'result ' => null ,
5765 );
58- Resque::redis ()->set ('job: ' . $ id . ':status ' , json_encode ($ statusPacket ));
66+ Resque::redis ()->set ((string ) $ status , json_encode ($ statusPacket ));
67+
68+ return $ status ;
5969 }
6070
6171 /**
@@ -91,9 +101,10 @@ public function update($status, $result = null)
91101 }
92102
93103 $ statusPacket = array (
94- 'status ' => $ status ,
104+ 'status ' => $ status ,
95105 'updated ' => time (),
96- 'result ' => $ result ,
106+ 'started ' => $ this ->getValue ('started ' ),
107+ 'result ' => $ result ,
97108 );
98109 Resque::redis ()->set ((string )$ this , json_encode ($ statusPacket ));
99110
@@ -104,12 +115,12 @@ public function update($status, $result = null)
104115 }
105116
106117 /**
107- * Fetch the status for the job being monitored.
118+ * Fetch a value from the status packet for the job being monitored.
108119 *
109- * @return mixed False if the status is not being monitored, otherwise the status
110- * as an integer, based on the Resque_Job_Status constants .
120+ * @return mixed False if the status is not being monitored, otherwise the
121+ * requested value from the status packet .
111122 */
112- public function get ( )
123+ protected function getValue ( $ value = null )
113124 {
114125 if (!$ this ->isTracking ()) {
115126 return false ;
@@ -120,7 +131,18 @@ public function get()
120131 return false ;
121132 }
122133
123- return $ statusPacket ['status ' ];
134+ return empty ($ value ) ? $ statusPacket : $ statusPacket [$ value ];
135+ }
136+
137+ /**
138+ * Fetch the status for the job being monitored.
139+ *
140+ * @return mixed False if the status is not being monitored, otherwise the status
141+ * as an integer, based on the Resque_Job_Status constants.
142+ */
143+ public function get ()
144+ {
145+ return $ this ->getValue ('status ' );
124146 }
125147
126148 /**
@@ -131,32 +153,9 @@ public function get()
131153 */
132154 public function getResult ()
133155 {
134- if (!$ this ->isTracking ()) {
135- return false ;
136- }
137-
138- $ statusPacket = json_decode (Resque::redis ()->get ((string )$ this ), true );
139- if (!$ statusPacket ) {
140- return false ;
141- }
142-
143- return $ statusPacket ['result ' ];
156+ return $ this ->getValue ('result ' );
144157 }
145158
146- /**
147- * Delete the job monitoring from the queue
148- *
149- * @return boolean|int
150- */
151- public function del ()
152- {
153- if (!$ this ->isTracking ()) {
154- return false ;
155- }
156-
157- return Resque::redis ()->del ((string )$ this );
158- }
159-
160159 /**
161160 * Stop tracking the status of a job.
162161 */
@@ -172,6 +171,6 @@ public function stop()
172171 */
173172 public function __toString ()
174173 {
175- return 'job: ' . $ this ->id . ':status ' ;
174+ return 'job: ' . $ this ->prefix . $ this -> id . ':status ' ;
176175 }
177176}
0 commit comments