From dd1c2c876366afd008f4d74166263d013b9b860c Mon Sep 17 00:00:00 2001 From: Leo Lutz Date: Tue, 10 Sep 2024 01:37:43 -0600 Subject: [PATCH] Add configuration (#10) * Add configuration with enabled and custom schedule * Fix bug using .env variable * Add to README and make it easier to publish the config file * Style --------- Co-authored-by: Ralph J. Smit <59207045+ralphjsmit@users.noreply.github.com> --- README.md | 25 ++++++++++++++++++++ config/horizon-cron-supervisor.php | 13 ++++++++++ src/Supervisor/SupervisorServiceProvider.php | 16 ++++++++++--- 3 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 config/horizon-cron-supervisor.php diff --git a/README.md b/README.md index 42ed6d4..db4a6bb 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,31 @@ composer require ralphjsmit/laravel-horizon-cron-supervisor The package works without any configuration. **Note that you need to have the Laravel Scheduler configured correctly.** +## Publishing the config file + +You can publish the config file or just use the environment variables. + +```shell +php artisan vendor:publish --tag="horizon-cron-supervisor-config" +``` + +The config file contents. +```php + env('HORIZON_CRON_SUPERVISOR_ENABLED', true), + + /** + * Run every X minutes or define a cron expression like "0,20,40 * * * *" + */ + 'schedule' => env('HORIZON_CRON_SUPERVISOR_SCHEDULE', 3), +]; +``` + ## How does this work with new deployments? When you deploy a new version of your app, you usually shut down your queues and Horizon instances in order let them use the files. The Laravel Scheduler doesn't run any commands when your application is in **maintenance mode** (`php artisan down`). Thus as long as your application is in maintenance mode, you don't need to worry about this package restarting your queues when you don't want that. diff --git a/config/horizon-cron-supervisor.php b/config/horizon-cron-supervisor.php new file mode 100644 index 0000000..9971a80 --- /dev/null +++ b/config/horizon-cron-supervisor.php @@ -0,0 +1,13 @@ + env('HORIZON_CRON_SUPERVISOR_ENABLED', true), + + /** + * Run every X minutes or define a cron expression like "0,20,40 * * * *" + */ + 'schedule' => env('HORIZON_CRON_SUPERVISOR_SCHEDULE', 3), +]; diff --git a/src/Supervisor/SupervisorServiceProvider.php b/src/Supervisor/SupervisorServiceProvider.php index c011e82..ab97afb 100644 --- a/src/Supervisor/SupervisorServiceProvider.php +++ b/src/Supervisor/SupervisorServiceProvider.php @@ -2,6 +2,7 @@ namespace RalphJSmit\LaravelHorizonCron\Supervisor; +use Illuminate\Console\Scheduling\Event; use Illuminate\Console\Scheduling\Schedule; use Illuminate\Support\ServiceProvider; @@ -9,19 +10,28 @@ class SupervisorServiceProvider extends ServiceProvider { public function boot(): void { + $this->publishes([ + __DIR__ . '/../../config/horizon-cron-supervisor.php' => config_path('horizon-cron-supervisor.php'), + ], 'horizon-cron-supervisor-config'); + $this->commands([ \RalphJSmit\LaravelHorizonCron\Supervisor\Console\RestartHorizon::class, ]); $this->app->booted(function () { - $schedule = $this->app->make(Schedule::class); + if (config('horizon-cron-supervisor.enabled')) { + $expression = config('horizon-cron-supervisor.schedule'); + $schedule = $this->app->make(Schedule::class); - $schedule->command('supervisor:check')->everyThreeMinutes(); + $schedule->command('supervisor:check')->tap( + fn (Event $event) => $event->expression = is_numeric($expression) ? "*/{$expression} * * * *" : $expression + ); + } }); } public function register(): void { - // + $this->mergeConfigFrom(__DIR__ . '/../../config/horizon-cron-supervisor.php', 'horizon-cron-supervisor'); } }