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(application): preserve crash restart limit status

Andras Bacsaiaaa540421fbd
diff --git a/app/Actions/Application/StopApplication.php b/app/Actions/Application/StopApplication.php
index b79709c5aa..bfad20ccfb 100644
--- a/app/Actions/Application/StopApplication.php
+++ b/app/Actions/Application/StopApplication.php
@@ -13,7 +13,7 @@ class StopApplication
public string $jobQueue = 'high';
- public function handle(Application $application, bool $previewDeployments = false, bool $dockerCleanup = true)
+ public function handle(Application $application, bool $previewDeployments = false, bool $dockerCleanup = true, bool $resetRestartCount = true)
{
$servers = collect([$application->destination->server]);
if ($application?->additional_servers?->count() > 0) {
@@ -57,12 +57,17 @@ class StopApplication
}
}
- // Reset restart tracking when application is manually stopped
- $application->update([
- 'restart_count' => 0,
- 'last_restart_at' => null,
- 'last_restart_type' => null,
- ]);
+ if ($resetRestartCount) {
+ $application->update([
+ 'restart_count' => 0,
+ 'last_restart_at' => null,
+ 'last_restart_type' => null,
+ ]);
+ } else {
+ $application->update([
+ 'status' => 'exited',
+ ]);
+ }
ServiceStatusChanged::dispatch($application->environment->project->team->id);
}
diff --git a/app/Actions/Docker/GetContainersStatus.php b/app/Actions/Docker/GetContainersStatus.php
index cfac835837..904885dfc5 100644
--- a/app/Actions/Docker/GetContainersStatus.php
+++ b/app/Actions/Docker/GetContainersStatus.php
@@ -466,7 +466,9 @@ class GetContainersStatus
}
// Wrap all database updates in a transaction to ensure consistency
- DB::transaction(function () use ($application, $maxRestartCount, $containerStatuses) {
+ $restartLimitReached = false;
+
+ DB::transaction(function () use ($application, $maxRestartCount, $containerStatuses, &$restartLimitReached) {
$previousRestartCount = $application->restart_count ?? 0;
if ($maxRestartCount > $previousRestartCount) {
@@ -480,8 +482,7 @@ class GetContainersStatus
// Check if restart limit has been reached
$maxAllowedRestarts = $application->max_restart_count ?? 0;
if ($maxAllowedRestarts > 0 && $maxRestartCount >= $maxAllowedRestarts && $previousRestartCount < $maxAllowedRestarts) {
- StopApplication::dispatch($application);
- $application->environment->project->team?->notify(new ApplicationRestartLimitReached($application));
+ $restartLimitReached = true;
}
}
@@ -496,6 +497,12 @@ class GetContainersStatus
}
}
});
+
+ if ($restartLimitReached) {
+ $application->refresh();
+ StopApplication::dispatch($application, false, true, false);
+ $application->environment->project->team?->notify(new ApplicationRestartLimitReached($application));
+ }
}
}
diff --git a/app/Models/Application.php b/app/Models/Application.php
index eee67022f0..1ffa625840 100644
--- a/app/Models/Application.php
+++ b/app/Models/Application.php
@@ -572,6 +572,15 @@ class Application extends BaseModel
return null;
}
+ public function stoppedAfterRestartLimit(): bool
+ {
+ return str($this->status)->startsWith('exited')
+ && ($this->restart_count ?? 0) > 0
+ && ($this->max_restart_count ?? 0) > 0
+ && $this->restart_count >= $this->max_restart_count
+ && $this->last_restart_type === 'crash';
+ }
+
public function taskLink($task_uuid)
{
if (data_get($this, 'environment.project.uuid')) {
diff --git a/app/Notifications/Application/RestartLimitReached.php b/app/Notifications/Application/RestartLimitReached.php
index 8709e7cd39..635dfdbdce 100644
--- a/app/Notifications/Application/RestartLimitReached.php
+++ b/app/Notifications/Application/RestartLimitReached.php
@@ -30,6 +30,7 @@ class RestartLimitReached extends CustomEmailNotification
public function __construct(public Application $resource)
{
$this->onQueue('high');
+ $this->afterCommit();
$this->resource_name = data_get($resource, 'name');
$this->project_uuid = data_get($resource, 'environment.project.uuid');
$this->environment_uuid = data_get($resource, 'environment.uuid');
@@ -40,7 +41,7 @@ class RestartLimitReached extends CustomEmailNotification
if (str($this->fqdn)->explode(',')->count() > 1) {
$this->fqdn = str($this->fqdn)->explode(',')->first();
}
- $this->resource_url = base_url()."/project/{$this->project_uuid}/environment/{$this->environment_uuid}/application/{$this->resource->uuid}";
+ $this->resource_url = $this->resource->link() ?? base_url()."/project/{$this->project_uuid}/environment/{$this->environment_uuid}/application/{$this->resource->uuid}";
}
public function via(object $notifiable): array
diff --git a/resources/views/components/status/index.blade.php b/resources/views/components/status/index.blade.php
index 8d959ce6ec..b54e35bd56 100644
--- a/resources/views/components/status/index.blade.php
+++ b/resources/views/components/status/index.blade.php
@@ -2,7 +2,11 @@
'title' => null,
'lastDeploymentLink' => null,
'resource' => null,
+ 'showRefreshButton' => true,
])
+@php
+ $stoppedAfterRestartLimit = $resource && method_exists($resource, 'stoppedAfterRestartLimit') && $resource->stoppedAfterRestartLimit();
+@endphp
<div class="flex flex-wrap items-center gap-1">
@if (str($resource->status)->startsWith('running'))
<x-status.running :status="$resource->status" :title="$title" :lastDeploymentLink="$lastDeploymentLink" />
@@ -13,13 +17,20 @@
@else
<x-status.stopped :status="$resource->status" />
@endif
- @if (isset($resource->restart_count) && $resource->restart_count > 0 && !str($resource->status)->startsWith('exited'))
+ @if (isset($resource->restart_count) && $resource->restart_count > 0 && (!str($resource->status)->startsWith('exited') || $stoppedAfterRestartLimit))
<div class="flex items-center">
<span class="text-xs dark:text-warning" title="Container has restarted {{ $resource->restart_count }} time{{ $resource->restart_count > 1 ? 's' : '' }}. Last restart: {{ $resource->last_restart_at?->diffForHumans() }}">
({{ $resource->restart_count }}x restarts)
</span>
</div>
@endif
+ @if ($stoppedAfterRestartLimit)
+ <div class="flex items-center">
+ <span class="text-xs dark:text-warning" title="Container has crashed and Coolify stopped it after {{ $resource->restart_count }} restart attempts.">
+ Stopped after reaching restart limit ({{ $resource->restart_count }}/{{ $resource->max_restart_count }}).
+ </span>
+ </div>
+ @endif
@if (!str($resource->status)->contains('exited') && $showRefreshButton)
<button wire:loading.remove.delay.shortest wire:target="manualCheckStatus" title="Refresh Status" wire:click='manualCheckStatus'
class="dark:hover:fill-white fill-black dark:fill-warning">
diff --git a/resources/views/livewire/project/application/heading.blade.php b/resources/views/livewire/project/application/heading.blade.php
index d69709f069..544bc2dc20 100644
--- a/resources/views/livewire/project/application/heading.blade.php
+++ b/resources/views/livewire/project/application/heading.blade.php
@@ -15,7 +15,7 @@
href="{{ route('project.application.logs', $parameters) }}">
<div class="flex items-center gap-1">
Logs
- @if ($application->restart_count > 0 && !str($application->status)->startsWith('exited'))
+ @if ($application->restart_count > 0 && (!str($application->status)->startsWith('exited') || $application->stoppedAfterRestartLimit()))
<svg class="w-4 h-4 dark:text-warning" viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg" title="Container has restarted {{ $application->restart_count }} time{{ $application->restart_count > 1 ? 's' : '' }}">
<path d="M12 2L1 21h22L12 2zm0 4l7.53 13H4.47L12 6zm-1 5v4h2v-4h-2zm0 5v2h2v-2h-2z"/>
</svg>
diff --git a/tests/Feature/ApplicationStoppedAfterRestartLimitTest.php b/tests/Feature/ApplicationStoppedAfterRestartLimitTest.php
new file mode 100644
index 0000000000..6b82d55689
--- /dev/null
+++ b/tests/Feature/ApplicationStoppedAfterRestartLimitTest.php
@@ -0,0 +1,82 @@
+<?php
+
+use App\Actions\Application\StopApplication;
+use App\Models\Application;
+use App\Notifications\Application\RestartLimitReached;
+
+function applicationWithRestartState(array $attributes = []): Application
+{
+ $application = new Application;
+ $application->forceFill(array_merge([
+ 'status' => 'exited:unhealthy',
+ 'restart_count' => 2,
+ 'max_restart_count' => 2,
+ 'last_restart_type' => 'crash',
+ 'last_restart_at' => now(),
+ ], $attributes));
+
+ return $application;
+}
+
+it('detects applications stopped after reaching the crash restart limit', function () {
+ expect(applicationWithRestartState()->stoppedAfterRestartLimit())->toBeTrue()
+ ->and(applicationWithRestartState(['status' => 'running:unhealthy'])->stoppedAfterRestartLimit())->toBeFalse()
+ ->and(applicationWithRestartState(['restart_count' => 1])->stoppedAfterRestartLimit())->toBeFalse()
+ ->and(applicationWithRestartState(['max_restart_count' => 0])->stoppedAfterRestartLimit())->toBeFalse()
+ ->and(applicationWithRestartState(['last_restart_type' => null])->stoppedAfterRestartLimit())->toBeFalse();
+});
+
+it('shows a stopped after restart limit warning in the status badge', function () {
+ $html = view('components.status.index', [
+ 'resource' => applicationWithRestartState(),
+ 'showRefreshButton' => false,
+ ])->render();
+
+ expect($html)->toContain('Stopped after reaching restart limit (2/2).')
+ ->and($html)->toContain('Container has crashed and Coolify stopped it after 2 restart attempts.');
+});
+
+it('does not show the restart limit warning for a normal manual stop', function () {
+ $html = view('components.status.index', [
+ 'resource' => applicationWithRestartState([
+ 'restart_count' => 0,
+ 'last_restart_type' => null,
+ ]),
+ 'showRefreshButton' => false,
+ ])->render();
+
+ expect($html)->not->toContain('Stopped after reaching restart limit');
+});
+
+it('keeps restart tracking configurable when stopping an application', function () {
+ $method = new ReflectionMethod(StopApplication::class, 'handle');
+ $resetRestartCount = collect($method->getParameters())->firstWhere('name', 'resetRestartCount');
+
+ expect($resetRestartCount)->not->toBeNull()
+ ->and($resetRestartCount->getDefaultValue())->toBeTrue();
+});
+
+it('uses the application link for restart limit notifications', function () {
+ $application = new class extends Application
+ {
+ public function link()
+ {
+ return 'https://coolify.test/project/link-from-model';
+ }
+ };
+ $application->forceFill([
+ 'name' => 'crashy-app',
+ 'uuid' => 'application-uuid',
+ 'restart_count' => 2,
+ 'max_restart_count' => 2,
+ ]);
+ $application->setRelation('environment', (object) [
+ 'uuid' => 'environment-uuid',
+ 'name' => 'production',
+ 'project' => (object) ['uuid' => 'project-uuid'],
+ ]);
+
+ $notification = new RestartLimitReached($application);
+
+ expect($notification->resource_url)->toBe('https://coolify.test/project/link-from-model');
+});