|||

Install & Configure Laravel Horizon with Statamic and Run it on Forge

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)

Adding a new daemon in Forge

The active daemon looks like this:

Horizon Demon in Forge

  • 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 enabling 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:

Horizon link in the Statamic Panel

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:

Up next Towards a More Sustainable and Mobile Office Statamic CMS talk at Frontend Meetup Freiburg 2022 This week, I had the chance to tell some nice people why I enjoy working with Statamic CMS. As promised, here come the slides, a couple of links
Latest posts Back to MyMind Sending Articles from Feedbin to Readwise Reader In Praise of Sublime App Why I Ended Up Using Todoist Yet Again Pros and Cons of Reflect Notes App A Love Letter to Sublime Internet Lazy.so is an amazing tool. Here's why I don't keep it Carl Sagan has shown the absurdity of not acting against climate change back in 1990 A success message that communicates the opposite of success Epomaker TH80 Pro Polyamor, aber mit PKM apps 🫣 Ironie, die Fabi Rommel Helft der Natur Tiny Garden Paradise Schwiegerfuchs Same Vibe Tailwind and Web Craftsmanship Buttons with CSS Expand Basic Markdown Formatting in Apple Notes Cannot Divide by Zero in Peak Picture Partial Applying a Growth Mindset (Doodle) Using the AVIF format for CSS background images Workaround for Email Validation Issue in Statamic A First Quick Look at Cosmos.so – and Why I'll Stick with MyMind Install a Starter Kit from local repo to get the latest Peak working in Statamic 4 beta Set sensible validations for Statamic image uploads Render first heading inside Statamic Page Builder as `h1` My PKM App Stack for 2023 Favorite apps in recent weeks: mymind & Craft First List Item inside CSS Multi Column Layout is offset