statamic / ssg

The official Statamic Static Site Generator
229 stars 23 forks source link

Slow generation via queue #162

Open Diewy opened 3 months ago

Diewy commented 3 months ago

Hi all,

I'm working on a website with multiple editors, aiming for high scalability. We're using Statamic along with this package to generate the static site and serve it via S3. It works beautifully, is highly secure, and scalable. However, there's a caveat: the developers have to run the command php please ssg:generate to publish changes, which cannot be executed by a site editor.

So, I'm in the process of creating a utility to allow site editors to initiate the static site generation. Since the generation process takes approximately 15 seconds, I'd like to trigger a queued job when a site editor clicks a button.

However, when handling the job with Horizon or using the database as the queue driver, the generation process takes over 45 minutes. When the queue driver is set to sync, it only takes around 15 seconds again.

I've attempted to debug the issue, and it seems like the delay is occurring at Page.php:58. However, I'm unsure where to proceed from here.

The job:

<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Statamic\StaticSite\Generator;

class StaticSiteGenerate implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable;

    /**
     * The number of seconds the job can run before timing out.
     *
     * @var int
     */
    public $timeout = 300;

    /**
     * @var Generator
     */
    protected $generator;

    /**
     * @var int
     */
    protected $workers = 2;

    public function handle(Generator $generator): void
    {
        $generator
            ->workers($this->workers)
            ->generate('*');
    }
}

Any ideas?