11<?php
2+
23/**
34 * Base Resque class.
45 *
@@ -10,7 +11,7 @@ class Resque
1011{
1112 const VERSION = '1.2 ' ;
1213
13- const DEFAULT_INTERVAL = 5 ;
14+ const DEFAULT_INTERVAL = 5 ;
1415
1516 /**
1617 * @var Resque_Redis Instance of Resque_Redis that talks to redis.
@@ -28,9 +29,9 @@ class Resque
2829 */
2930 protected static $ redisDatabase = 0 ;
3031
31- /**
32- * @var string auth of Redis database
33- */
32+ /**
33+ * @var string auth of Redis database
34+ */
3435 protected static $ auth ;
3536
3637 /**
@@ -42,7 +43,7 @@ class Resque
4243 * and returns a Resque_Redis instance, or
4344 * a nested array of servers with host/port pairs.
4445 * @param int $database
45- * @param string $auth
46+ * @param string $auth
4647 */
4748 public static function setBackend ($ server , $ database = 0 , $ auth = null )
4849 {
@@ -70,8 +71,8 @@ public static function redis()
7071 }
7172
7273 if (!empty (self ::$ auth )) {
73- self ::$ redis ->auth (self ::$ auth );
74- }
74+ self ::$ redis ->auth (self ::$ auth );
75+ }
7576
7677 return self ::$ redis ;
7778 }
@@ -87,7 +88,7 @@ public static function redis()
8788 */
8889 public static function fork ()
8990 {
90- if (!function_exists ('pcntl_fork ' )) {
91+ if (!function_exists ('pcntl_fork ' )) {
9192 return false ;
9293 }
9394
@@ -96,7 +97,7 @@ public static function fork()
9697 self ::$ redis = null ;
9798
9899 $ pid = pcntl_fork ();
99- if ($ pid === -1 ) {
100+ if ($ pid === -1 ) {
100101 throw new RuntimeException ('Unable to fork child worker. ' );
101102 }
102103
@@ -133,9 +134,9 @@ public static function push($queue, $item)
133134 */
134135 public static function pop ($ queue )
135136 {
136- $ item = self ::redis ()->lpop ('queue: ' . $ queue );
137+ $ item = self ::redis ()->lpop ('queue: ' . $ queue );
137138
138- if (!$ item ) {
139+ if (!$ item ) {
139140 return ;
140141 }
141142
@@ -149,13 +150,13 @@ public static function pop($queue)
149150 * @param array $items
150151 * @return integer number of deleted items
151152 */
152- public static function dequeue ($ queue , $ items = Array ())
153+ public static function dequeue ($ queue , $ items = array ())
153154 {
154- if (count ($ items ) > 0 ) {
155+ if (count ($ items ) > 0 ) {
155156 return self ::removeItems ($ queue , $ items );
156- } else {
157+ } else {
157158 return self ::removeList ($ queue );
158- }
159+ }
159160 }
160161
161162 /**
@@ -166,9 +167,9 @@ public static function dequeue($queue, $items = Array())
166167 */
167168 public static function removeQueue ($ queue )
168169 {
169- $ num = self ::removeList ($ queue );
170- self ::redis ()->srem ('queues ' , $ queue );
171- return $ num ;
170+ $ num = self ::removeList ($ queue );
171+ self ::redis ()->srem ('queues ' , $ queue );
172+ return $ num ;
172173 }
173174
174175 /**
@@ -181,28 +182,28 @@ public static function removeQueue($queue)
181182 */
182183 public static function blpop (array $ queues , $ timeout )
183184 {
184- $ list = array ();
185- foreach ($ queues AS $ queue ) {
186- $ list [] = 'queue: ' . $ queue ;
187- }
185+ $ list = array ();
186+ foreach ($ queues as $ queue ) {
187+ $ list [] = 'queue: ' . $ queue ;
188+ }
188189
189- $ item = self ::redis ()->blpop ($ list , (int )$ timeout );
190+ $ item = self ::redis ()->blpop ($ list , (int )$ timeout );
190191
191- if (!$ item ) {
192- return ;
193- }
192+ if (!$ item ) {
193+ return ;
194+ }
194195
195- /**
196- * Normally the Resque_Redis class returns queue names without the prefix
197- * But the blpop is a bit different. It returns the name as prefix:queue:name
198- * So we need to strip off the prefix:queue: part
199- */
200- $ queue = substr ($ item [0 ], strlen (self ::redis ()->getPrefix () . 'queue: ' ));
196+ /**
197+ * Normally the Resque_Redis class returns queue names without the prefix
198+ * But the blpop is a bit different. It returns the name as prefix:queue:name
199+ * So we need to strip off the prefix:queue: part
200+ */
201+ $ queue = substr ($ item [0 ], strlen (self ::redis ()->getPrefix () . 'queue: ' ));
201202
202- return array (
203+ return array (
203204 'queue ' => $ queue ,
204205 'payload ' => json_decode ($ item [1 ], true )
205- );
206+ );
206207 }
207208
208209 /**
@@ -239,8 +240,7 @@ public static function enqueue($queue, $class, $args = null, $trackStatus = fals
239240 );
240241 try {
241242 Resque_Event::trigger ('beforeEnqueue ' , $ hookParams );
242- }
243- catch (Resque_Job_DontCreate $ e ) {
243+ } catch (Resque_Job_DontCreate $ e ) {
244244 return false ;
245245 }
246246
@@ -269,7 +269,7 @@ public static function reserve($queue)
269269 public static function queues ()
270270 {
271271 $ queues = self ::redis ()->smembers ('queues ' );
272- if (!is_array ($ queues )) {
272+ if (!is_array ($ queues )) {
273273 $ queues = array ();
274274 }
275275 return $ queues ;
@@ -283,7 +283,7 @@ public static function queues()
283283 public static function items ($ queue , $ start = 0 , $ stop = -1 )
284284 {
285285 $ list = self ::redis ()->lrange ('queue: ' . $ queue , $ start , $ stop );
286- if (!is_array ($ list )) {
286+ if (!is_array ($ list )) {
287287 $ list = array ();
288288 }
289289 return $ list ;
@@ -301,20 +301,20 @@ public static function items($queue, $start = 0, $stop = -1)
301301 * @param array $items
302302 * @return integer number of deleted items
303303 */
304- private static function removeItems ($ queue , $ items = Array ())
304+ private static function removeItems ($ queue , $ items = array ())
305305 {
306306 $ counter = 0 ;
307- $ originalQueue = 'queue: ' . $ queue ;
308- $ tempQueue = $ originalQueue . ':temp: ' . time ();
309- $ requeueQueue = $ tempQueue . ':requeue ' ;
307+ $ originalQueue = 'queue: ' . $ queue ;
308+ $ tempQueue = $ originalQueue . ':temp: ' . time ();
309+ $ requeueQueue = $ tempQueue . ':requeue ' ;
310310
311311 // move each item from original queue to temp queue and process it
312312 $ finished = false ;
313313 while (!$ finished ) {
314314 $ string = self ::redis ()->rpoplpush ($ originalQueue , self ::redis ()->getPrefix () . $ tempQueue );
315315
316316 if (!empty ($ string )) {
317- if (self ::matchItem ($ string , $ items )) {
317+ if (self ::matchItem ($ string , $ items )) {
318318 self ::redis ()->rpop ($ tempQueue );
319319 $ counter ++;
320320 } else {
@@ -328,9 +328,9 @@ private static function removeItems($queue, $items = Array())
328328 // move back from temp queue to original queue
329329 $ finished = false ;
330330 while (!$ finished ) {
331- $ string = self ::redis ()->rpoplpush ($ requeueQueue , self ::redis ()->getPrefix () .$ originalQueue );
331+ $ string = self ::redis ()->rpoplpush ($ requeueQueue , self ::redis ()->getPrefix () . $ originalQueue );
332332 if (empty ($ string )) {
333- $ finished = true ;
333+ $ finished = true ;
334334 }
335335 }
336336
@@ -353,29 +353,31 @@ private static function removeItems($queue, $items = Array())
353353 */
354354 private static function matchItem ($ string , $ items )
355355 {
356- $ decoded = json_decode ($ string , true );
356+ $ decoded = json_decode ($ string , true );
357357
358- foreach ($ items as $ key => $ val ) {
358+ foreach ($ items as $ key => $ val ) {
359359 # class name only ex: item[0] = ['class']
360360 if (is_numeric ($ key )) {
361- if ($ decoded ['class ' ] == $ val ) {
361+ if ($ decoded ['class ' ] == $ val ) {
362362 return true ;
363- }
363+ }
364364 # class name with args , example: item[0] = ['class' => {'foo' => 1, 'bar' => 2}]
365365 } elseif (is_array ($ val )) {
366- $ decodedArgs = (array )$ decoded ['args ' ][0 ];
367- if ($ decoded ['class ' ] == $ key &&
368- count ($ decodedArgs ) > 0 && count (array_diff ($ decodedArgs , $ val )) == 0 ) {
366+ $ decodedArgs = (array )$ decoded ['args ' ][0 ];
367+ if (
368+ $ decoded ['class ' ] == $ key &&
369+ count ($ decodedArgs ) > 0 && count (array_diff ($ decodedArgs , $ val )) == 0
370+ ) {
369371 return true ;
370372 }
371373 # class name with ID, example: item[0] = ['class' => 'id']
372374 } else {
373- if ($ decoded ['class ' ] == $ key && $ decoded ['id ' ] == $ val ) {
375+ if ($ decoded ['class ' ] == $ key && $ decoded ['id ' ] == $ val ) {
374376 return true ;
375- }
377+ }
376378 }
377- }
378- return false ;
379+ }
380+ return false ;
379381 }
380382
381383 /**
@@ -388,9 +390,9 @@ private static function matchItem($string, $items)
388390 */
389391 private static function removeList ($ queue )
390392 {
391- $ counter = self ::size ($ queue );
392- $ result = self ::redis ()->del ('queue: ' . $ queue );
393- return ($ result == 1 ) ? $ counter : 0 ;
393+ $ counter = self ::size ($ queue );
394+ $ result = self ::redis ()->del ('queue: ' . $ queue );
395+ return ($ result == 1 ) ? $ counter : 0 ;
394396 }
395397
396398 /*
0 commit comments