Files
ABSDEV/Modules/Automation/Console/DispatchScheduledAutomations.php
2026-07-24 14:59:44 +01:00

31 lines
1.1 KiB
PHP

<?php
namespace Modules\Automation\Console;
use Illuminate\Console\Command;
use Modules\Automation\Models\Automation;
use Modules\Automation\Services\AutomationRunner;
final class DispatchScheduledAutomations extends Command
{
protected $signature = 'automation:dispatch-scheduled';
protected $description = 'Dispatch active automations whose next run time is due.';
public function handle(AutomationRunner $runner): int
{
$count = 0;
Automation::query()->where('status', 'active')->where('trigger_type', 'schedule')->whereNotNull('next_run_at')->where('next_run_at', '<=', now())->chunkById(100, function ($items) use ($runner, &$count) {
foreach ($items as $automation) {
$runner->dispatch($automation, ['scheduled_at' => now()->toIso8601String()], 'schedule');
$interval = max(1, (int) ($automation->trigger_config['interval_minutes'] ?? 60));
$automation->update(['next_run_at' => now()->addMinutes($interval)]);
$count++;
}
});
$this->info("Dispatched {$count} scheduled automation(s).");
return self::SUCCESS;
}
}