@@ -21,12 +21,42 @@ class RedisConsumer implements Consumer
2121 */
2222 private $ context ;
2323
24+ /**
25+ * @var int
26+ */
27+ private $ retryDelay ;
28+
29+ /**
30+ * @var RedisQueueConsumer
31+ */
32+ private $ queueConsumer ;
33+
2434 public function __construct (RedisContext $ context , RedisDestination $ queue )
2535 {
2636 $ this ->context = $ context ;
2737 $ this ->queue = $ queue ;
2838 }
2939
40+ /**
41+ * @return int
42+ */
43+ public function getRetryDelay (): ?int
44+ {
45+ return $ this ->retryDelay ;
46+ }
47+
48+ /**
49+ * @param int $retryDelay
50+ */
51+ public function setRetryDelay (int $ retryDelay ): void
52+ {
53+ $ this ->retryDelay = $ retryDelay ;
54+
55+ if ($ this ->queueConsumer ) {
56+ $ this ->queueConsumer ->setRetryDelay ($ this ->retryDelay );
57+ }
58+ }
59+
3060 /**
3161 * @return RedisDestination
3262 */
@@ -40,40 +70,37 @@ public function getQueue(): Queue
4070 */
4171 public function receive (int $ timeout = 0 ): ?Message
4272 {
43- $ timeout = (int ) ($ timeout / 1000 );
44- if (empty ($ timeout )) {
73+ $ timeout = (int ) ceil ($ timeout / 1000 );
74+
75+ if ($ timeout <= 0 ) {
4576 while (true ) {
4677 if ($ message = $ this ->receive (5000 )) {
4778 return $ message ;
4879 }
4980 }
5081 }
5182
52- if ($ result = $ this ->getRedis ()->brpop ([$ this ->queue ->getName ()], $ timeout )) {
53- return RedisMessage ::jsonUnserialize ($ result ->getMessage ());
54- }
83+ $ this ->initQueueConsumer ();
5584
56- return null ;
85+ return $ this -> queueConsumer -> receiveMessage ( $ timeout ) ;
5786 }
5887
5988 /**
6089 * @return RedisMessage
6190 */
6291 public function receiveNoWait (): ?Message
6392 {
64- if ($ result = $ this ->getRedis ()->rpop ($ this ->queue ->getName ())) {
65- return RedisMessage ::jsonUnserialize ($ result ->getMessage ());
66- }
93+ $ this ->initQueueConsumer ();
6794
68- return null ;
95+ return $ this -> queueConsumer -> receiveMessageNoWait ( $ this -> queue ) ;
6996 }
7097
7198 /**
7299 * @param RedisMessage $message
73100 */
74101 public function acknowledge (Message $ message ): void
75102 {
76- // do nothing. redis transport always works in auto ack mode
103+ $ this -> getRedis ()-> zrem ( $ this -> queue -> getName (). ' :reserved ' , $ message -> getReservedKey ());
77104 }
78105
79106 /**
@@ -83,15 +110,33 @@ public function reject(Message $message, bool $requeue = false): void
83110 {
84111 InvalidMessageException ::assertMessageInstanceOf ($ message , RedisMessage::class);
85112
86- // do nothing on reject. redis transport always works in auto ack mode
113+ $ this -> acknowledge ( $ message );
87114
88115 if ($ requeue ) {
89- $ this ->context ->createProducer ()->send ($ this ->queue , $ message );
116+ $ message = RedisMessage ::jsonUnserialize ($ message ->getReservedKey ());
117+ $ message ->setHeader ('attempts ' , 0 );
118+
119+ if ($ message ->getTimeToLive ()) {
120+ $ message ->setHeader ('expires_at ' , time () + $ message ->getTimeToLive ());
121+ }
122+
123+ $ this ->getRedis ()->lpush ($ this ->queue ->getName (), json_encode ($ message ));
90124 }
91125 }
92126
93127 private function getRedis (): Redis
94128 {
95129 return $ this ->context ->getRedis ();
96130 }
131+
132+ private function initQueueConsumer (): void
133+ {
134+ if (null === $ this ->queueConsumer ) {
135+ $ this ->queueConsumer = new RedisQueueConsumer ($ this ->getRedis (), [$ this ->queue ]);
136+
137+ if ($ this ->retryDelay ) {
138+ $ this ->queueConsumer ->setRetryDelay ($ this ->retryDelay );
139+ }
140+ }
141+ }
97142}
0 commit comments