Graceful shutdown

Stopping consumers gracefully ensures you don't kill a process halfway through processing a consumed message.

Consumers listen to the SIGTERM, SIGINT and SIGQUIT signals. When one of them is received, the consumer finishes the message it is handling, stops consuming, and closes, committing the stored offsets and leaving the consumer group. Listening to signals requires the Process Control extension .

Sponsorship

Support Laravel Kafka by sponsoring me!

Laravel Kafka is free and Open Source software, built to empower developers like you. Your support helps maintain and enhance the project. If you find it valuable, please consider sponsoring me on GitHub. Every contribution makes a difference and keeps the development going strong! Thank you!

Become a Sponsor Want to hide this message? Sponsor at any tier of $10/month or more!

If the process running the consumer had already registered a handler for one of these signals, for example a Laravel queue worker running a consumer inside a queued job, that handler is still invoked, once the consumer has closed and the original handlers are restored, right before consume() returns. A queue worker therefore keeps honouring the graceful shutdown of queue:work after it has run a consumer, and a handler that exits the process, like the one of a Symfony console application dispatching signal events, only exits once the offsets are committed and the consumer left its group.

When a signal arrives while a failed message is being retried , the remaining retries are skipped and the message is handled as failed right away.

Process monitors such as Supervisor send a signal and wait for the process to exit before killing it. Make sure they wait longer than the time your handler takes to process a message, see running consumers in production .

If your app needs to run some code when the consumer stops consuming messages, use the onStopConsuming method of the consumer builder. The callback runs when the consumer stops normally, for instance after a signal or once it reached the limits set with stopAfterMessages or stopAfterSeconds, but not when it stops because of an exception:

use Junges\Kafka\Facades\Kafka;

$consumer = Kafka::consumer(['topic'])
    ->withHandler(new Handler)
    ->onStopConsuming(static function () {
        // Do something when the consumer stops consuming messages
    })
    ->build();

$consumer->consume();

To run code whenever any consumer stops, including when it stops because of an exception, listen to the ConsumerStopped event . Its reason property tells why the consumer stopped.