Skip to content

Commit dd1c2c8

Browse files
skeemerralphjsmit
andauthored
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>
1 parent e624336 commit dd1c2c8

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,31 @@ composer require ralphjsmit/laravel-horizon-cron-supervisor
2020

2121
The package works without any configuration. **Note that you need to have the Laravel Scheduler configured correctly.**
2222

23+
## Publishing the config file
24+
25+
You can publish the config file or just use the environment variables.
26+
27+
```shell
28+
php artisan vendor:publish --tag="horizon-cron-supervisor-config"
29+
```
30+
31+
The config file contents.
32+
```php
33+
<?php
34+
35+
return [
36+
/**
37+
* Enables Horizon Cron Supervisor
38+
*/
39+
'enabled' => env('HORIZON_CRON_SUPERVISOR_ENABLED', true),
40+
41+
/**
42+
* Run every X minutes or define a cron expression like "0,20,40 * * * *"
43+
*/
44+
'schedule' => env('HORIZON_CRON_SUPERVISOR_SCHEDULE', 3),
45+
];
46+
```
47+
2348
## How does this work with new deployments?
2449

2550
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.

config/horizon-cron-supervisor.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
return [
4+
/**
5+
* Enables Horizon Cron Supervisor
6+
*/
7+
'enabled' => env('HORIZON_CRON_SUPERVISOR_ENABLED', true),
8+
9+
/**
10+
* Run every X minutes or define a cron expression like "0,20,40 * * * *"
11+
*/
12+
'schedule' => env('HORIZON_CRON_SUPERVISOR_SCHEDULE', 3),
13+
];

src/Supervisor/SupervisorServiceProvider.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,36 @@
22

33
namespace RalphJSmit\LaravelHorizonCron\Supervisor;
44

5+
use Illuminate\Console\Scheduling\Event;
56
use Illuminate\Console\Scheduling\Schedule;
67
use Illuminate\Support\ServiceProvider;
78

89
class SupervisorServiceProvider extends ServiceProvider
910
{
1011
public function boot(): void
1112
{
13+
$this->publishes([
14+
__DIR__ . '/../../config/horizon-cron-supervisor.php' => config_path('horizon-cron-supervisor.php'),
15+
], 'horizon-cron-supervisor-config');
16+
1217
$this->commands([
1318
\RalphJSmit\LaravelHorizonCron\Supervisor\Console\RestartHorizon::class,
1419
]);
1520

1621
$this->app->booted(function () {
17-
$schedule = $this->app->make(Schedule::class);
22+
if (config('horizon-cron-supervisor.enabled')) {
23+
$expression = config('horizon-cron-supervisor.schedule');
24+
$schedule = $this->app->make(Schedule::class);
1825

19-
$schedule->command('supervisor:check')->everyThreeMinutes();
26+
$schedule->command('supervisor:check')->tap(
27+
fn (Event $event) => $event->expression = is_numeric($expression) ? "*/{$expression} * * * *" : $expression
28+
);
29+
}
2030
});
2131
}
2232

2333
public function register(): void
2434
{
25-
//
35+
$this->mergeConfigFrom(__DIR__ . '/../../config/horizon-cron-supervisor.php', 'horizon-cron-supervisor');
2636
}
2737
}

0 commit comments

Comments
 (0)