laravel-addons / command-daemonizer
Laravel/Lumen console command daemonizer with graceful shutdown
Installs: 23 978
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=7.2.0
- illuminate/console: ^5.0|^6.0|^7.0
- illuminate/support: ^5.0|^6.0|^7.0
Requires (Dev)
- phpunit/phpunit: ^8.5
- roave/security-advisories: dev-master
README
Run a Laravel console command as a long-lived process with gracefully shutdown.
Installation
composer require laravel-addons/command-daemonizer
Laravel will automatically add the service provider CommandDaemonizerServiceProvider
to the file config/app.php
in providers
option.
In Lumen, you MUST manually register the service provider CommandDaemonizerServiceProvider
in bootstrap/app.php
file:
$app->register(LaravelAddons\CommandDaemonizer\CommandDaemonizerServiceProvider::class);
How to use
For example, run kafka consumer
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()); } } }
- You MUST implement method
daemon()
. This method will run in an endless loop. - You can use constructor to inject your dependency or inject dependency in
daemon()
method. Your dependencies will be resolved. - You can override the empty parent method
init()
to run some code before starting the daemon.
Options
DaemonCommand
append some options to you command:
--force : Force the worker to run even in maintenance mode
--memory=128 : The memory limit in megabytes
--sleep=0 : Number of seconds to sleep at each iteration in a loop
--timeout=60 : The number of seconds a child process can run
Gracefully shutdown
Since daemonized commands are long-lived processes, they will not pick up changes to your code without being restarted. So, the simplest way to deploy an application using daemonized commands is to restart the commands during your deployment process. You may gracefully restart all of the daemonized commands by issuing the daemon-command:restart
:
php artisan daemon-command:restart
This command will instruct all daemonized commands to gracefully "die" after they finish processing their current step in loop. Since the daemonized commands will die when the daemon-command:restart
command is executed, you should be running a process manager such as Supervisor to automatically restart the daemonized commands.
This library uses the cache to store restart signals, so you should verify a cache driver is properly configured for your application before using this feature.
Based on Illuminate Queue Worker.