PHP code example of laravel-addons / command-daemonizer

1. Go to this page and download the library: Download laravel-addons/command-daemonizer library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

laravel-addons / command-daemonizer example snippets


$app->register(LaravelAddons\CommandDaemonizer\CommandDaemonizerServiceProvider::class);

use LaravelAddons\CommandDaemonizer\DaemonCommand;

class KafkaMessageConsumer extends DaemonCommand
{
    private $config;
    private $consumer;

    protected $signature = 'kafka-consumer';
    
    public function __construct(array $config)
    {
        parent::__construct();

        $this->config = $config;
    }

    protected function init(): void
    {
        $this->consumer = ... //initialisation of consumer
    }

    public function daemon(MyHandler $handler, LoggerInterface $logger): void
    {
        $message = $this->consumer->receive();

        try {
            if ($message instanceof RdKafkaMessage) {
                $handler->handle($message);
                $this->consumer->acknowledge($message);
            }
        } catch (Throwable $e) {
            $logger->error($e->getMessage());
        }
    }
}
shell script
php artisan daemon-command:restart