9
9
10
10
final class BatchConsumer extends BaseAmqp implements DequeuerInterface
11
11
{
12
- /**
13
- * @var int
14
- */
15
- private $ consumed = 0 ;
16
-
17
12
/**
18
13
* @var \Closure|callable
19
14
*/
@@ -75,43 +70,32 @@ public function consume()
75
70
{
76
71
$ this ->setupConsumer ();
77
72
78
- $ isConsuming = false ;
79
- $ timeoutWanted = $ this ->getTimeoutWait ();
80
73
while (count ($ this ->getChannel ()->callbacks )) {
81
- $ this ->maybeStopConsumer ();
82
- if (!$ this ->forceStop ) {
83
- try {
84
- $ this ->getChannel ()->wait (null , false , $ timeoutWanted );
85
- $ isConsuming = true ;
86
- } catch (AMQPTimeoutException $ e ) {
87
- $ this ->batchConsume ();
88
- if ($ isConsuming ) {
89
- $ isConsuming = false ;
90
- } elseif (null !== $ this ->getIdleTimeoutExitCode ()) {
91
- return $ this ->getIdleTimeoutExitCode ();
92
- } else {
93
- throw $ e ;
94
- }
95
- }
96
- } else {
74
+ if ($ this ->isCompleteBatch ()) {
97
75
$ this ->batchConsume ();
98
76
}
99
77
100
- if ( $ this ->isCompleteBatch ( $ isConsuming )) {
101
- $ this -> batchConsume ();
102
- }
78
+ $ this ->maybeStopConsumer ();
79
+
80
+ $ timeout = $ this -> isEmptyBatch () ? $ this -> getIdleTimeout () : $ this -> getTimeoutWait ();
103
81
104
- $ timeoutWanted = $ isConsuming ? $ this ->getTimeoutWait () : $ this ->getIdleTimeout ();
82
+ try {
83
+ $ this ->getChannel ()->wait (null , false , $ timeout );
84
+ } catch (AMQPTimeoutException $ e ) {
85
+ if (!$ this ->isEmptyBatch ()) {
86
+ $ this ->batchConsume ();
87
+ } elseif (null !== $ this ->getIdleTimeoutExitCode ()) {
88
+ return $ this ->getIdleTimeoutExitCode ();
89
+ } else {
90
+ throw $ e ;
91
+ }
92
+ }
105
93
}
106
94
}
107
95
108
- public function batchConsume ()
96
+ private function batchConsume ()
109
97
{
110
- if ($ this ->batchCounter === 0 ) {
111
- return ;
112
- }
113
-
114
- try {
98
+ try {
115
99
$ processFlags = call_user_func ($ this ->callback , $ this ->messages );
116
100
$ this ->handleProcessMessages ($ processFlags );
117
101
$ this ->logger ->debug ('Queue message processed ' , array (
@@ -129,6 +113,7 @@ public function batchConsume()
129
113
'stacktrace ' => $ e ->getTraceAsString ()
130
114
)
131
115
));
116
+ $ this ->resetBatch ();
132
117
$ this ->stopConsuming ();
133
118
} catch (\Exception $ e ) {
134
119
$ this ->logger ->error ($ e ->getMessage (), array (
@@ -166,9 +151,6 @@ protected function handleProcessMessages($processFlags = null)
166
151
foreach ($ processFlags as $ deliveryTag => $ processFlag ) {
167
152
$ this ->handleProcessFlag ($ deliveryTag , $ processFlag );
168
153
}
169
-
170
- $ this ->consumed ++;
171
- $ this ->maybeStopConsumer ();
172
154
}
173
155
174
156
/**
@@ -177,7 +159,7 @@ protected function handleProcessMessages($processFlags = null)
177
159
*
178
160
* @return void
179
161
*/
180
- private function handleProcessFlag ($ deliveryTag , $ processFlag )
162
+ private function handleProcessFlag ($ deliveryTag , $ processFlag )
181
163
{
182
164
if ($ processFlag === ConsumerInterface::MSG_REJECT_REQUEUE || false === $ processFlag ) {
183
165
// Reject and requeue message to RabbitMQ
@@ -195,13 +177,19 @@ private function handleProcessFlag ($deliveryTag, $processFlag)
195
177
}
196
178
197
179
/**
198
- * @param bool $isConsuming
199
- *
200
180
* @return bool
201
181
*/
202
- protected function isCompleteBatch ($ isConsuming )
182
+ protected function isCompleteBatch ()
203
183
{
204
- return $ isConsuming && $ this ->batchCounter === $ this ->prefetchCount ;
184
+ return $ this ->batchCounter === $ this ->prefetchCount ;
185
+ }
186
+
187
+ /**
188
+ * @return bool
189
+ */
190
+ protected function isEmptyBatch ()
191
+ {
192
+ return $ this ->batchCounter === 0 ;
205
193
}
206
194
207
195
/**
@@ -300,7 +288,9 @@ private function getMessageChannel($deliveryTag)
300
288
*/
301
289
public function stopConsuming ()
302
290
{
303
- $ this ->batchConsume ();
291
+ if (!$ this ->isEmptyBatch ()) {
292
+ $ this ->batchConsume ();
293
+ }
304
294
305
295
$ this ->getChannel ()->basic_cancel ($ this ->getConsumerTag ());
306
296
}
0 commit comments