feat(scheduler): make scheduled job dispatch mode configurable

Default to sequential dispatch on self-hosted instances and concurrent
per-type dispatch on Coolify Cloud. Allow SCHEDULED_JOBS_DISPATCH_MODE
to override the default, falling back for invalid values.

Cover defaults, overrides, and fallback behavior in scheduler tests.
This commit is contained in:
Andras Bacsai committed 2026-10-06 23:16:40 +02:00
1 parent cd70dbf03a
commit d54c03730d
3 files changed
+68 -12

No files matched your search

+24 -4
View File
@@ -124,13 +124,19 @@ class Kernel extends ConsoleKernel
/**
* Run the manager from the scheduler, not from a queue worker. A busy queue could delay it
* past the catch-up window, and then due backups and tasks would be skipped.
* Each schedule type runs in its own process with its own overlap lock, so the types run in
* parallel and a slow type cannot make another type skip a run.
*
* Sequential mode runs all schedule types after each other in one process. Concurrent mode runs
* each type in its own process with its own overlap lock, so a slow type cannot make another type
* skip a run, but each process loads the full application every minute (more CPU and memory).
*/
private function scheduleScheduledJobManager(): void
{
foreach (array_keys(ScheduledJobManager::TYPES) as $type) {
$this->scheduleInstance->command("scheduled:dispatch --type={$type}")
$commands = $this->scheduledJobsDispatchMode() === 'concurrent'
? array_map(fn (string $type) => "scheduled:dispatch --type={$type}", array_keys(ScheduledJobManager::TYPES))
: ['scheduled:dispatch'];
foreach ($commands as $command) {
$this->scheduleInstance->command($command)
->everyMinute()
->onOneServer()
->withoutOverlapping(5)
@@ -138,6 +144,20 @@ class Kernel extends ConsoleKernel
}
}
/**
* SCHEDULED_JOBS_DISPATCH_MODE, or the default: sequential on self-hosted, concurrent on Coolify Cloud.
*/
private function scheduledJobsDispatchMode(): string
{
$mode = strtolower(trim((string) config('constants.coolify.scheduled_jobs_dispatch_mode')));
if (in_array($mode, ['sequential', 'concurrent'], true)) {
return $mode;
}
return isCloud() ? 'concurrent' : 'sequential';
}
private function scheduleUpdates(): void
{
$this->scheduleInstance->job(new CheckForUpdatesJob)
+2
View File
@@ -22,6 +22,8 @@ return [
'versions_url' => env('VERSIONS_URL', env('CDN_URL', 'https://cdn.coollabs.io').'/coolify/versions.json'),
'upgrade_script_url' => env('UPGRADE_SCRIPT_URL', env('CDN_URL', 'https://cdn.coollabs.io').'/coolify/upgrade.sh'),
'releases_url' => env('RELEASES_URL', 'https://cdn.coollabs.io/coolify/releases.json'),
// "sequential" or "concurrent". Empty uses sequential on self-hosted and concurrent on Coolify Cloud.
'scheduled_jobs_dispatch_mode' => env('SCHEDULED_JOBS_DISPATCH_MODE'),
],
'urls' => [
+42 -8
View File
@@ -4,6 +4,7 @@ use App\Models\InstanceSettings;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Collection;
uses(RefreshDatabase::class);
@@ -74,22 +75,55 @@ it('schedules stuck resource cleanup in the background once per day', function (
->and($event->runInBackground)->toBeTrue();
});
it('runs one scheduled job dispatcher for each schedule type from the scheduler instead of a queue', function () {
$events = collect(app(Schedule::class)->events());
function scheduledJobDispatchersForMode(?string $mode, bool $selfHosted = true): Collection
{
config()->set('constants.coolify.self_hosted', $selfHosted);
config()->set('constants.coolify.scheduled_jobs_dispatch_mode', $mode);
$dispatchers = $events->filter(fn ($event) => str_contains((string) $event->command, 'scheduled:dispatch'));
$schedule = new Schedule;
(fn () => $this->schedule($schedule))->call(app(Kernel::class));
expect($dispatchers->map(fn ($event) => str((string) $event->command)->after('--type=')->value())->values()->all())
->toBe(['backups', 'tasks', 'volume-backups', 'docker-cleanups']);
$events = collect($schedule->events());
expect($events->contains(fn ($event) => str_contains((string) $event->description, 'ScheduledJobManager')))->toBeFalse();
$dispatchers = $events->filter(fn ($event) => str_contains((string) $event->command, 'scheduled:dispatch'))->values();
$dispatchers->each(function ($event) {
expect($event->expression)->toBe('* * * * *')
->and($event->onOneServer)->toBeTrue()
->and($event->withoutOverlapping)->toBeTrue()
->and($event->runInBackground)->toBeTrue();
});
// Each type has its own overlap lock.
expect($dispatchers->map->mutexName()->unique())->toHaveCount(4)
->and($events->contains(fn ($event) => str_contains((string) $event->description, 'ScheduledJobManager')))->toBeFalse();
return $dispatchers;
}
it('runs all scheduled job types in one dispatcher process by default on self-hosted', function () {
$dispatchers = scheduledJobDispatchersForMode(null);
expect($dispatchers)->toHaveCount(1)
->and((string) $dispatchers->first()->command)->toEndWith('scheduled:dispatch')
->and((string) $dispatchers->first()->command)->not->toContain('--type=');
});
it('runs one scheduled job dispatcher for each schedule type in concurrent mode', function (?string $mode, bool $selfHosted) {
$dispatchers = scheduledJobDispatchersForMode($mode, $selfHosted);
expect($dispatchers->map(fn ($event) => str((string) $event->command)->after('--type=')->value())->all())
->toBe(['backups', 'tasks', 'volume-backups', 'docker-cleanups'])
// Each type has its own overlap lock.
->and($dispatchers->map->mutexName()->unique())->toHaveCount(4);
})->with([
'set on self-hosted' => ['concurrent', true],
'default on Coolify Cloud' => [null, false],
]);
it('runs one dispatcher process when sequential mode is set on Coolify Cloud', function () {
expect(scheduledJobDispatchersForMode('sequential', false))->toHaveCount(1);
});
it('falls back to the default dispatch mode for an unknown value', function () {
expect(scheduledJobDispatchersForMode('parallel'))->toHaveCount(1)
->and(scheduledJobDispatchersForMode('parallel', false))->toHaveCount(4);
});
it('schedules GitHub runner reconciliation every minute on Coolify Cloud', function () {