@@ -39,7 +39,14 @@ class Resque_Job_Status
39
39
*/
40
40
public function __construct ($ id )
41
41
{
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 ' ;
43
50
}
44
51
45
52
/**
@@ -53,9 +60,9 @@ public static function create($id)
53
60
$ statusPacket = array (
54
61
'status ' => self ::STATUS_WAITING ,
55
62
'updated ' => time (),
56
- 'started ' => time (),
63
+ 'started ' => time ()
57
64
);
58
- Resque::redis ()->set (' job: ' . $ id . ' :status ' , json_encode ($ statusPacket ));
65
+ Resque::redis ()->set (self :: generateId ( $ id) , json_encode ($ statusPacket ));
59
66
}
60
67
61
68
/**
@@ -70,7 +77,7 @@ public function isTracking()
70
77
return false ;
71
78
}
72
79
73
- if (!Resque::redis ()->exists (( string ) $ this )) {
80
+ if (!Resque::redis ()->exists ($ this -> id )) {
74
81
$ this ->isTracking = false ;
75
82
return false ;
76
83
}
@@ -86,19 +93,26 @@ public function isTracking()
86
93
*/
87
94
public function update ($ status )
88
95
{
96
+ $ status = (int )$ status ;
97
+
89
98
if (!$ this ->isTracking ()) {
90
99
return ;
91
100
}
92
101
102
+ if ($ status < 1 || $ status > 4 ) {
103
+ return ;
104
+ }
105
+
93
106
$ statusPacket = array (
94
107
'status ' => $ status ,
95
108
'updated ' => time (),
109
+ 'started ' => $ this ->fetch ('started ' )
96
110
);
97
- Resque::redis ()->set (( string ) $ this , json_encode ($ statusPacket ));
111
+ Resque::redis ()->set ($ this -> id , json_encode ($ statusPacket ));
98
112
99
113
// Expire the status for completed jobs after 24 hours
100
114
if (in_array ($ status , self ::$ completeStatuses )) {
101
- Resque::redis ()->expire (( string ) $ this , 86400 );
115
+ Resque::redis ()->expire ($ this -> id , 86400 );
102
116
}
103
117
}
104
118
@@ -110,24 +124,68 @@ public function update($status)
110
124
*/
111
125
public function get ()
112
126
{
113
- if (!$ this ->isTracking ()) {
114
- return false ;
115
- }
127
+ return $ this ->status ();
128
+ }
116
129
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
+ }
121
150
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 ;
123
181
}
124
182
125
183
/**
126
184
* Stop tracking the status of a job.
127
185
*/
128
186
public function stop ()
129
187
{
130
- Resque::redis ()->del (( string ) $ this );
188
+ Resque::redis ()->del ($ this -> id );
131
189
}
132
190
133
191
/**
@@ -137,7 +195,7 @@ public function stop()
137
195
*/
138
196
public function __toString ()
139
197
{
140
- return ' job: ' . $ this ->id . ' :status ' ;
198
+ return $ this ->id ;
141
199
}
142
200
}
143
- ?>
201
+ ?>
0 commit comments