trypost/app/Console/Commands/Automation/FireScheduleTriggers.php
Paulo Castellano a129b6b5cd Denormalize automation trigger_type into an indexed column
The scheduler command ran every minute and loaded all active automations,
then filtered by trigger_type in PHP because that value lived buried in the
nodes JSON array — effectively a full-table scan plus a JSON decode per row
each minute, discarding every non-schedule automation.

Derive trigger_type into a real, indexed column on save (recomputed in the
existing saving() hook so it can never drift from nodes) and filter on it in
SQL. Applies to both the schedule firer and the post-trigger dispatcher.
2026-06-13 15:26:54 -03:00

32 lines
966 B
PHP

<?php
declare(strict_types=1);
namespace App\Console\Commands\Automation;
use App\Actions\Automation\Trigger\FireScheduleTrigger;
use App\Enums\Automation\Status;
use App\Enums\Automation\Trigger\Type as TriggerType;
use App\Models\Automation;
use Illuminate\Console\Attributes\Description;
use Illuminate\Console\Attributes\Signature;
use Illuminate\Console\Command;
#[Signature('automation:fire-schedule')]
#[Description('Fire scheduled automations whose cron matches now')]
class FireScheduleTriggers extends Command
{
public function handle(FireScheduleTrigger $fire): int
{
Automation::query()
->where('status', Status::Active)
->where('trigger_type', TriggerType::Schedule->value)
->chunkById(50, function ($automations) use ($fire) {
foreach ($automations as $automation) {
$fire($automation);
}
});
return self::SUCCESS;
}
}