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.

fix(scheduler): send at most one missed-run notification per hour per schedule

Andras Bacsai8c2725a3f8d8
diff --git a/app/Services/ScheduledJobDeliveryService.php b/app/Services/ScheduledJobDeliveryService.php
index 330889c151..a2046484f0 100644
--- a/app/Services/ScheduledJobDeliveryService.php
+++ b/app/Services/ScheduledJobDeliveryService.php
@@ -9,6 +9,7 @@ use App\Jobs\DockerCleanupJob;
use App\Jobs\ScheduledTaskJob;
use App\Jobs\VolumeBackupJob;
use App\Models\DockerCleanupExecution;
+use App\Models\NotificationThrottle;
use App\Models\ScheduledDatabaseBackup;
use App\Models\ScheduledJobDelivery;
use App\Models\ScheduledJobState;
@@ -19,7 +20,9 @@ use App\Models\Server;
use App\Models\Team;
use App\Notifications\ScheduledTask\TaskFailed;
use App\Notifications\Server\DockerCleanupFailed;
+use Closure;
use Cron\CronExpression;
+use Illuminate\Database\Eloquent\Model;
use Illuminate\Queue\Middleware\WithoutOverlapping;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
@@ -36,6 +39,10 @@ class ScheduledJobDeliveryService
*/
public const ENQUEUED_STALE_AFTER_MINUTES = 60;
+ public const MISSED_OCCURRENCE_NOTIFICATION = 'scheduled-occurrence-missed';
+
+ public const MISSED_OCCURRENCE_NOTIFICATION_INTERVAL_MINUTES = 60;
+
/**
* Job types that run once when they are late. A lost task or Docker cleanup is not run; it is
* recorded as a failed execution and the team is notified.
@@ -265,7 +272,7 @@ class ScheduledJobDeliveryService
'finished_at' => now(),
]);
ScheduledTaskDone::dispatch($task->team_id);
- Team::find($task->team_id)?->notify(new TaskFailed($task, $message));
+ $this->notifyMissedOccurrence($task, fn () => Team::find($task->team_id)?->notify(new TaskFailed($task, $message)));
} elseif ($occurrence->job_type === 'docker-cleanup') {
$server = Server::find($occurrence->resource_id);
if (! $server) {
@@ -279,7 +286,7 @@ class ScheduledJobDeliveryService
'finished_at' => now(),
]);
event(new DockerCleanupDone($execution));
- $server->team?->notify(new DockerCleanupFailed($server, "Docker cleanup job failed with the following error: {$message}"));
+ $this->notifyMissedOccurrence($server, fn () => $server->team?->notify(new DockerCleanupFailed($server, "Docker cleanup job failed with the following error: {$message}")));
}
} catch (\Throwable $e) {
Log::channel('scheduled-errors')->error('Failed to report missed scheduled occurrence', [
@@ -290,6 +297,15 @@ class ScheduledJobDeliveryService
}
}
+ /**
+ * Every missed run gets its own failed execution, but the team gets at most one missed-run
+ * notification per schedule each hour, so dead queue workers do not cause a notification storm.
+ */
+ private function notifyMissedOccurrence(Model $schedule, Closure $send): void
+ {
+ NotificationThrottle::sendOnce($schedule, self::MISSED_OCCURRENCE_NOTIFICATION, now()->subMinutes(self::MISSED_OCCURRENCE_NOTIFICATION_INTERVAL_MINUTES), $send);
+ }
+
public function deleteOldOccurrences(): void
{
ScheduledJobDelivery::query()
diff --git a/tests/Feature/ScheduledJobManagerDispatchTest.php b/tests/Feature/ScheduledJobManagerDispatchTest.php
index 9c84560399..44201e0d4c 100644
--- a/tests/Feature/ScheduledJobManagerDispatchTest.php
+++ b/tests/Feature/ScheduledJobManagerDispatchTest.php
@@ -750,6 +750,46 @@ it('records a missed Docker cleanup as a failed execution and notifies the team
Event::assertDispatched(DockerCleanupDone::class, 1);
});
+it('sends at most one missed-run notification per hour for the same schedule', function (string $jobType) {
+ config(['constants.coolify.self_hosted' => true]);
+ Carbon::setTestNow(Carbon::create(2026, 9, 17, 12, 0, 0, 'UTC'));
+ Queue::fake();
+ Notification::fake();
+ Event::fake([DockerCleanupDone::class]);
+ if ($jobType === 'scheduled-task') {
+ $task = createScheduledApplicationTask(createScheduledTaskApplication(), ['frequency' => 'daily']);
+ InstanceSettings::unguarded(fn () => InstanceSettings::firstOrCreate(['id' => 0]));
+ $team = Team::find($task->team_id);
+ $team->emailNotificationSettings->update(['smtp_enabled' => true, 'scheduled_task_failure_email_notifications' => true]);
+ [$scheduleKey, $resourceId, $notification] = ["scheduled-task:{$task->id}", $task->id, TaskFailed::class];
+ $executions = fn () => ScheduledTaskExecution::query()->where('scheduled_task_id', $task->id)->count();
+ } else {
+ $server = createScheduledTaskApplication()->destination->server;
+ $team = $server->team;
+ $team->emailNotificationSettings->update(['smtp_enabled' => true, 'docker_cleanup_failure_email_notifications' => true]);
+ [$scheduleKey, $resourceId, $notification] = ["docker-cleanup:{$server->id}", $server->id, DockerCleanupFailed::class];
+ $executions = fn () => DockerCleanupExecution::query()->where('server_id', $server->id)->count();
+ }
+
+ // Workers are down: three runs of the same schedule were missed.
+ foreach ([0, 1, 2] as $minute) {
+ createStaleEnqueuedOccurrence($scheduleKey, $jobType, $resourceId, Carbon::create(2026, 9, 17, 10, $minute, 0, 'UTC'));
+ }
+ (new ScheduledJobManager)->handle();
+
+ expect($executions())->toBe(3);
+ Notification::assertSentToTimes($team, $notification, 1);
+
+ // More than an hour later, a new missed run is reported again.
+ Carbon::setTestNow(Carbon::create(2026, 9, 17, 13, 5, 0, 'UTC'));
+ createStaleEnqueuedOccurrence($scheduleKey, $jobType, $resourceId, Carbon::create(2026, 9, 17, 11, 30, 0, 'UTC'));
+ (new ScheduledJobManager)->handle();
+
+ // Each missed run still gets its own failed execution (Docker cleanup also has its own 12:00 run).
+ expect($executions())->toBeGreaterThanOrEqual(4);
+ Notification::assertSentToTimes($team, $notification, 2);
+})->with(['scheduled task' => 'scheduled-task', 'docker cleanup' => 'docker-cleanup']);
+
it('leaves recently enqueued occurrences alone', function () {
config(['constants.coolify.self_hosted' => true]);
Carbon::setTestNow(Carbon::create(2026, 9, 17, 12, 0, 0, 'UTC'));