11
11
use RdKafka \Message ;
12
12
use RdKafka \TopicPartition ;
13
13
use SimPod \Kafka \Clients \Consumer \Exception \IncompatibleStatus ;
14
- use SimPod \Kafka \Common \Exception \Wakeup ;
15
14
16
15
use function array_map ;
17
16
use function rd_kafka_err2str ;
@@ -30,6 +29,8 @@ final class KafkaConsumer extends RdKafkaConsumer
30
29
31
30
private LoggerInterface $ logger ;
32
31
32
+ private bool $ shouldRun = true ;
33
+
33
34
public function __construct (ConsumerConfig $ config , ?LoggerInterface $ logger = null )
34
35
{
35
36
$ this ->logger = $ logger ?? new NullLogger ();
@@ -144,50 +145,47 @@ function (Message $message) use (
144
145
}
145
146
146
147
/**
147
- * @param callable(Message) : void $onSuccess
148
- * @param callable() : void $onPartitionEof
149
- * @param callable() : void $onTimedOut
148
+ * @param callable(Message): void $onSuccess
149
+ * @param callable() : void $onPartitionEof
150
+ * @param callable() : void $onTimedOut
150
151
*/
151
152
private function doStart (
152
153
int $ timeoutMs ,
153
154
callable $ onSuccess ,
154
155
?callable $ onPartitionEof = null ,
155
156
?callable $ onTimedOut = null
156
157
): void {
157
- $ this ->registerSignals ();
158
+ $ this ->registerSignals ($ this -> shouldRun );
158
159
159
- try {
160
- while (true ) {
161
- pcntl_signal_dispatch ();
160
+ while ($ this ->shouldRun ) {
161
+ $ message = $ this ->consume ($ timeoutMs );
162
162
163
- $ message = $ this ->consume ($ timeoutMs );
163
+ switch ($ message ->err ) {
164
+ case RD_KAFKA_RESP_ERR_NO_ERROR :
165
+ $ onSuccess ($ message );
164
166
165
- switch ($ message ->err ) {
166
- case RD_KAFKA_RESP_ERR_NO_ERROR :
167
- $ onSuccess ($ message );
168
-
169
- break ;
170
- case RD_KAFKA_RESP_ERR__PARTITION_EOF :
171
- if ($ onPartitionEof !== null ) {
172
- $ onPartitionEof ();
173
- }
167
+ break ;
168
+ case RD_KAFKA_RESP_ERR__PARTITION_EOF :
169
+ if ($ onPartitionEof !== null ) {
170
+ $ onPartitionEof ();
171
+ }
174
172
175
- $ this ->logger ->info ('No more messages. Will wait for more ' );
173
+ $ this ->logger ->info ('No more messages. Will wait for more ' );
176
174
177
- break ;
178
- case RD_KAFKA_RESP_ERR__TIMED_OUT :
179
- $ this ->logger ->info (sprintf ('Timed out with timeout %d ms ' , $ timeoutMs ));
180
- if ($ onTimedOut !== null ) {
181
- $ onTimedOut ();
182
- }
175
+ break ;
176
+ case RD_KAFKA_RESP_ERR__TIMED_OUT :
177
+ $ this ->logger ->info (sprintf ('Timed out with timeout %d ms ' , $ timeoutMs ));
178
+ if ($ onTimedOut !== null ) {
179
+ $ onTimedOut ();
180
+ }
183
181
184
- break ;
185
- default :
186
- $ exception = IncompatibleStatus::fromMessage ($ message );
187
- $ this ->logger ->error ($ exception ->getMessage (), ['exception ' => $ exception ]);
188
- }
182
+ break ;
183
+ default :
184
+ $ exception = IncompatibleStatus::fromMessage ($ message );
185
+ $ this ->logger ->error ($ exception ->getMessage (), ['exception ' => $ exception ]);
189
186
}
190
- } catch (Wakeup $ wakeup ) {
187
+
188
+ pcntl_signal_dispatch ();
191
189
}
192
190
193
191
$ this ->degisterSignals ();
@@ -229,6 +227,6 @@ public function shutdown(): void
229
227
{
230
228
$ this ->logger ->info ('Shutting down ' );
231
229
232
- throw new Wakeup () ;
230
+ $ this -> shouldRun = false ;
233
231
}
234
232
}
0 commit comments