@@ -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 ()->hmset ( self :: generateId ( $ id), $ 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,25 @@ 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
- 'updated ' => time (),
108
+ 'updated ' => time ()
96
109
);
97
- Resque::redis ()->set (( string ) $ this , json_encode ( $ statusPacket) );
110
+ Resque::redis ()->hmset ( $ this -> id , $ statusPacket );
98
111
99
112
// Expire the status for completed jobs after 24 hours
100
113
if (in_array ($ status , self ::$ completeStatuses )) {
101
- Resque::redis ()->expire (( string ) $ this , 86400 );
114
+ Resque::redis ()->expire ($ this -> id , 86400 );
102
115
}
103
116
}
104
117
@@ -110,24 +123,60 @@ public function update($status)
110
123
*/
111
124
public function get ()
112
125
{
113
- if (!$ this ->isTracking ()) {
114
- return false ;
115
- }
126
+ return $ this ->fetch ('status ' );
127
+ }
116
128
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
+ }
121
159
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 ;
123
172
}
124
173
125
174
/**
126
175
* Stop tracking the status of a job.
127
176
*/
128
177
public function stop ()
129
178
{
130
- Resque::redis ()->del (( string ) $ this );
179
+ Resque::redis ()->del ($ this -> id );
131
180
}
132
181
133
182
/**
@@ -137,7 +186,7 @@ public function stop()
137
186
*/
138
187
public function __toString ()
139
188
{
140
- return ' job: ' . $ this ->id . ' :status ' ;
189
+ return $ this ->id ;
141
190
}
142
191
}
143
- ?>
192
+ ?>
0 commit comments