Skip to content

Commit

Permalink
Add configuration (#10)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
skeemer and ralphjsmit authored Sep 10, 2024
1 parent e624336 commit dd1c2c8
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<?php

return [
/**
* Enables Horizon Cron Supervisor
*/
'enabled' => 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.
Expand Down
13 changes: 13 additions & 0 deletions config/horizon-cron-supervisor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

return [
/**
* Enables Horizon Cron Supervisor
*/
'enabled' => 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),
];
16 changes: 13 additions & 3 deletions src/Supervisor/SupervisorServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,36 @@

namespace RalphJSmit\LaravelHorizonCron\Supervisor;

use Illuminate\Console\Scheduling\Event;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Support\ServiceProvider;

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');
}
}

0 comments on commit dd1c2c8

Please sign in to comment.