Follow these steps to use Laravel Horizon for Job Queuing in Statamic CMS when hosted with Laravel Forge. This guide assumes that Redis is already installed via Forge.

  • Add Horizon to the project  composer require laravel/horizon and publish its assets php artisan horizon:install
  • Allow one or more Statamic/Laravel user(s) to authenticate against Horizon by adding their account email(s) to the HorizonServiceProvider.php
    protected function gate()
    {
        Gate::define('viewHorizon', function ($user) {
            return in_array($user->email, [
                'user@host.com'
            ]);
        });
    }
  • Configure a dedicated queue per environment in config/horizon.php
'environments' => [
    'production' => [
        'supervisor-1' => [
            'maxProcesses' => 10,
            'balanceMaxShift' => 1,
            'balanceCooldown' => 3,
            'queue' => ['daarchitektur_production'],
        ],
    ],
'staging' => [
    'supervisor-1' => [
        'maxProcesses' => 10,
        'balanceMaxShift' => 1,
        'balanceCooldown' => 3,
        'queue' => ['daarchitektur_staging'],
    ],
],

'local' => [
    'supervisor-1' => [
        'maxProcesses' => 3,
    ],
],

],

⚠️
Beware of maxProcesses greater than 1 when using the Statamic Shopify addon, or make sure to use another a dedicated queue for that.
  • Using the Forge UI, add a new Daemon to run php artisan horizon (This will make sure the horizon command is always run and will restart it if it fails unexpectedly)
Pasted image 20220211111626.png

The active daemon looks like this:

Pasted image 20220211111946.png

  • Add php artisan horizon:terminate to the end of the deployment script. This will make sure the old Horizon workers are gracefully terminated on each deployment, and fresh Horizon workers will be started thanks to our daemon configuration.
  • Tell Statamic to use Redis as the queue connection (.env): QUEUE_CONNECTION=redis
  • Tell Horizon which queue to use (.env) according to your environment settings made previously in config/horizon.php, e.g. REDIS_QUEUE=daarchitektur_staging

Test if it works

One way to test if everything works is by enablin  Git integration with dispatch delay.

STATAMIC_GIT_ENABLED=true
STATAMIC_GIT_PUSH=true
STATAMIC_GIT_DISPATCH_DELAY=10
STATAMIC_GIT_AUTOMATIC=true

Clear the config cache, log into Statamic CP, make some content changes and open the Horizon Dashboard (you’ll have access as soon as you’re logged in with a user who has been added to HorizonServiceProvider.php). There’s even a convenient link in the Statamic panel:

CleanShot 2022-02-11 at 13.53.45@2x.png

You should see a pending task being spawned as soon as you save content changes. After the amount of minutes you’ve set for STATAMIC_GIT_DISPATCH_DELAY, Pending Jobs should be processed and move over to Completed Jobs:

Screenshot showing Pending Jobs in Laravel Horizon

Resources: