@@ -13,6 +13,11 @@ class Resque_Job_Status
13
13
const STATUS_FAILED = 3 ;
14
14
const STATUS_COMPLETE = 4 ;
15
15
16
+ /**
17
+ * @var string The prefix of the job status id.
18
+ */
19
+ private $ prefix ;
20
+
16
21
/**
17
22
* @var string The ID of the job this status class refers back to.
18
23
*/
@@ -37,9 +42,10 @@ class Resque_Job_Status
37
42
*
38
43
* @param string $id The ID of the job to manage the status for.
39
44
*/
40
- public function __construct ($ id )
45
+ public function __construct ($ id, $ prefix = '' )
41
46
{
42
47
$ this ->id = $ id ;
48
+ $ this ->prefix = empty ($ prefix ) ? '' : "$ {prefix}_ " ;
43
49
}
44
50
45
51
/**
@@ -48,14 +54,18 @@ public function __construct($id)
48
54
*
49
55
* @param string $id The ID of the job to monitor the status of.
50
56
*/
51
- public static function create ($ id )
57
+ public static function create ($ id, $ prefix = "" )
52
58
{
59
+ $ status = new self ($ id , $ prefix );
53
60
$ statusPacket = array (
54
- 'status ' => self ::STATUS_WAITING ,
61
+ 'status ' => self ::STATUS_WAITING ,
55
62
'updated ' => time (),
56
63
'started ' => time (),
64
+ 'result ' => null ,
57
65
);
58
- Resque::redis ()->set ('job: ' . $ id . ':status ' , json_encode ($ statusPacket ));
66
+ Resque::redis ()->set ((string ) $ status , json_encode ($ statusPacket ));
67
+
68
+ return $ status ;
59
69
}
60
70
61
71
/**
@@ -91,9 +101,10 @@ public function update($status, $result = null)
91
101
}
92
102
93
103
$ statusPacket = array (
94
- 'status ' => $ status ,
104
+ 'status ' => $ status ,
95
105
'updated ' => time (),
96
- 'result ' => $ result ,
106
+ 'started ' => $ this ->getValue ('started ' ),
107
+ 'result ' => $ result ,
97
108
);
98
109
Resque::redis ()->set ((string )$ this , json_encode ($ statusPacket ));
99
110
@@ -104,12 +115,12 @@ public function update($status, $result = null)
104
115
}
105
116
106
117
/**
107
- * Fetch the status for the job being monitored.
118
+ * Fetch a value from the status packet for the job being monitored.
108
119
*
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 .
111
122
*/
112
- public function get ( )
123
+ protected function getValue ( $ value = null )
113
124
{
114
125
if (!$ this ->isTracking ()) {
115
126
return false ;
@@ -120,7 +131,18 @@ public function get()
120
131
return false ;
121
132
}
122
133
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 ' );
124
146
}
125
147
126
148
/**
@@ -131,32 +153,9 @@ public function get()
131
153
*/
132
154
public function getResult ()
133
155
{
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 ' );
144
157
}
145
158
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
-
160
159
/**
161
160
* Stop tracking the status of a job.
162
161
*/
@@ -172,6 +171,6 @@ public function stop()
172
171
*/
173
172
public function __toString ()
174
173
{
175
- return 'job: ' . $ this ->id . ':status ' ;
174
+ return 'job: ' . $ this ->prefix . $ this -> id . ':status ' ;
176
175
}
177
176
}
0 commit comments