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.
PHP coolifydatabasesdeploymentdockerdocker-composeinertiajslaravelmariadbmysqlnextjsnodejsphppostgresredisself-hostedself-hostingserverstatic-sitesveltesvelte5
fix(scheduled-tasks): skip a run while the previous run is still running
Andras Bacsaied8271eb9f59
diff --git a/app/Jobs/ScheduledTaskJob.php b/app/Jobs/ScheduledTaskJob.phpindex 8538760db8..c36f3cd7ed 100644--- a/app/Jobs/ScheduledTaskJob.php+++ b/app/Jobs/ScheduledTaskJob.php@@ -80,6 +80,17 @@ class ScheduledTaskJob implements ShouldBeEncrypted, ShouldQueue$this->executionUuid = new_public_id();}+ /**+ * The lock outlives the worker timeout, so a run killed at the timeout still blocks a new run+ * while its remote command can still be running.+ */+ public function middleware(): array+ {+ $expireAfter = ($this->task->timeout ?? 300) + 300;++ return [ScheduledJobDeliveryService::withoutOverlapping('scheduled-task-'.$this->task->id, $this->occurrenceUuid)->expireAfter($expireAfter)->dontRelease()];+ }+private function initializeExecutionContext(): void{$this->task->loadMissing([diff --git a/app/Services/ScheduledJobDeliveryService.php b/app/Services/ScheduledJobDeliveryService.phpindex 0d5ddd633a..b2dc7446ea 100644--- a/app/Services/ScheduledJobDeliveryService.php+++ b/app/Services/ScheduledJobDeliveryService.php@@ -627,8 +627,8 @@ class ScheduledJobDeliveryServicereturn false;}- // An open previous occurrence usually means the previous run is still running. Backup jobs- // then skip this one without running it (see withoutOverlapping()).+ // An open previous occurrence usually means the previous run is still running. Backup and+ // task jobs then skip this one without running it (see withoutOverlapping()).$previousOpen = ScheduledJobDelivery::query()->where('schedule_key', $occurrence->schedule_key)->where('scheduled_for', '<', $occurrence->scheduled_for)diff --git a/tests/Feature/ScheduledJobManagerDispatchTest.php b/tests/Feature/ScheduledJobManagerDispatchTest.phpindex d09bb865a1..d904fe016d 100644--- a/tests/Feature/ScheduledJobManagerDispatchTest.php+++ b/tests/Feature/ScheduledJobManagerDispatchTest.php@@ -1060,6 +1060,33 @@ it('does not run a scheduled task again when Redis hands it out after its workerexpect(ScheduledTaskExecution::query()->where('scheduled_task_id', $task->id)->exists())->toBeFalse();});+it('drops a scheduled task job while an earlier run of the same task holds the overlap lock', function () {+ $task = createScheduledApplicationTask(createScheduledTaskApplication(), ['timeout' => 4200]);+ $occurrence = ScheduledJobDelivery::create([+ 'schedule_key' => "scheduled-task:{$task->id}",+ 'scheduled_for' => now(),+ 'job_type' => 'scheduled-task',+ 'resource_id' => $task->id,+ 'status' => 'enqueued',+ 'enqueued_at' => now(),+ ]);+ $job = (new ScheduledTaskJob($task, $occurrence->uuid))->withFakeQueueInteractions();+ $middleware = $job->middleware()[0];+ $runningLock = Cache::lock($middleware->getLockKey($job), 60);+ $runningLock->get();++ $middleware->handle($job, fn (ScheduledTaskJob $job) => $job->handle());+ $otherTaskJob = new ScheduledTaskJob(createScheduledApplicationTask(createScheduledTaskApplication()));++ $job->assertNotReleased();+ expect($middleware->expiresAfter)->toBe(4200 + 300)+ ->and($occurrence->fresh()->status)->toBe('skipped')+ ->and(ScheduledTaskExecution::query()->where('scheduled_task_id', $task->id)->exists())->toBeFalse()+ ->and($otherTaskJob->middleware()[0]->getLockKey($otherTaskJob))->not->toBe($middleware->getLockKey($job));++ $runningLock->release();+});+it('fails only its own execution and notifies the team when the worker timeout stops a scheduled task job', function () {InstanceSettings::forceCreate(['id' => 0]);Notification::fake();