Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

feat(scheduler): make scheduled job dispatch mode configurable

Andras Bacsaid54c03730d4b
diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php
index a1fb63b40b..b13d8c99c4 100644
--- a/app/Console/Kernel.php
+++ b/app/Console/Kernel.php
@@ -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)
diff --git a/config/constants.php b/config/constants.php
index 8c71e262dd..74b8232988 100644
--- a/config/constants.php
+++ b/config/constants.php
@@ -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' => [
diff --git a/tests/Feature/ScheduleOnOneServerTest.php b/tests/Feature/ScheduleOnOneServerTest.php
index 03aab32a84..2e4cdcf34e 100644
--- a/tests/Feature/ScheduleOnOneServerTest.php
+++ b/tests/Feature/ScheduleOnOneServerTest.php
@@ -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));
+
+ $events = collect($schedule->events());
+ expect($events->contains(fn ($event) => str_contains((string) $event->description, 'ScheduledJobManager')))->toBeFalse();
- expect($dispatchers->map(fn ($event) => str((string) $event->command)->after('--type=')->value())->values()->all())
- ->toBe(['backups', 'tasks', 'volume-backups', 'docker-cleanups']);
+ $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 () {