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
feat: configurable stop grace period for applications
Hendrik Kleinwaechter60d8aba323bf
diff --git a/app/Actions/Application/StopApplication.php b/app/Actions/Application/StopApplication.phpindex e86e30f045..2badcbb674 100644--- a/app/Actions/Application/StopApplication.php+++ b/app/Actions/Application/StopApplication.php@@ -36,10 +36,13 @@ class StopApplication: getCurrentApplicationContainerStatus($server, $application->id, 0);$containersToStop = $containers->pluck('Names')->toArray();+ $timeout = ($application->settings->stop_grace_period > 0)+ ? $application->settings->stop_grace_period+ : DEFAULT_STOP_GRACE_PERIOD_SECONDS;foreach ($containersToStop as $containerName) {instant_remote_process(command: [- "docker stop -t 30 $containerName",+ "docker stop --time=$timeout $containerName","docker rm -f $containerName",], server: $server, throwError: false);}diff --git a/app/Actions/Application/StopApplicationOneServer.php b/app/Actions/Application/StopApplicationOneServer.phpindex bf9fdee725..cb51bc8656 100644--- a/app/Actions/Application/StopApplicationOneServer.php+++ b/app/Actions/Application/StopApplicationOneServer.php@@ -20,13 +20,17 @@ class StopApplicationOneServer}try {$containers = getCurrentApplicationContainerStatus($server, $application->id, 0);+ $timeout = ($application->settings->stop_grace_period > 0)+ ? $application->settings->stop_grace_period+ : DEFAULT_STOP_GRACE_PERIOD_SECONDS;+if ($containers->count() > 0) {foreach ($containers as $container) {$containerName = data_get($container, 'Names');if ($containerName) {instant_remote_process([- "docker stop -t 30 $containerName",+ "docker stop --time=$timeout $containerName","docker rm -f $containerName",],$serverdiff --git a/app/Jobs/ApplicationDeploymentJob.php b/app/Jobs/ApplicationDeploymentJob.phpindex 7e5025c8ab..229f46cd8e 100644--- a/app/Jobs/ApplicationDeploymentJob.php+++ b/app/Jobs/ApplicationDeploymentJob.php@@ -3310,14 +3310,21 @@ COPY ./nginx.conf /etc/nginx/conf.d/default.conf");private function graceful_shutdown_container(string $containerName, bool $skipRemove = false){try {- $timeout = isDev() ? 1 : 30;+ if (isDev()) {+ $timeout = 1;+ } else {+ $timeout = ($this->application->settings->stop_grace_period > 0)+ ? $this->application->settings->stop_grace_period+ : DEFAULT_STOP_GRACE_PERIOD_SECONDS;+ }+if ($skipRemove) {$this->execute_remote_command(- ["docker stop -t $timeout $containerName", 'hidden' => true, 'ignore_errors' => true]+ ["docker stop --time=$timeout $containerName", 'hidden' => true, 'ignore_errors' => true]);} else {$this->execute_remote_command(- ["docker stop -t $timeout $containerName", 'hidden' => true, 'ignore_errors' => true],+ ["docker stop --time=$timeout $containerName", 'hidden' => true, 'ignore_errors' => true],["docker rm -f $containerName", 'hidden' => true, 'ignore_errors' => true]);}diff --git a/app/Livewire/Project/Application/Advanced.php b/app/Livewire/Project/Application/Advanced.phpindex cf7ef3e0b2..862181ecf1 100644--- a/app/Livewire/Project/Application/Advanced.php+++ b/app/Livewire/Project/Application/Advanced.php@@ -61,6 +61,9 @@ class Advanced extends Component#[Validate(['string', 'nullable'])]public ?string $gpuOptions = null;+ #[Validate(['string', 'nullable'])]+ public ?string $stopGracePeriod = null;+#[Validate(['boolean'])]public bool $isBuildServerEnabled = false;@@ -145,6 +148,10 @@ class Advanced extends Component$this->injectBuildArgsToDockerfile = $this->application->settings->inject_build_args_to_dockerfile ?? true;$this->includeSourceCommitInBuild = $this->application->settings->include_source_commit_in_build ?? false;}++ // Load stop_grace_period separately since it has its own save handler+ // Convert null to empty string to prevent dirty detection issues+ $this->stopGracePeriod = $this->application->settings->stop_grace_period ?? '';}private function resetDefaultLabels()@@ -252,6 +259,34 @@ class Advanced extends Component}}+ public function saveStopGracePeriod()+ {+ try {+ $this->authorize('update', $this->application);++ // Convert empty string to null, otherwise cast to integer+ $value = ($this->stopGracePeriod === '' || $this->stopGracePeriod === null)+ ? null+ : (int) $this->stopGracePeriod;++ // Validate the integer value+ if ($value !== null && ($value < 1 || $value > 3600)) {+ $this->dispatch('error', 'Stop grace period must be between 1 and 3600 seconds.');++ return;+ }++ // Save to model+ $this->application->settings->stop_grace_period = $value;+ $this->application->settings->save();++ // User feedback+ $this->dispatch('success', 'Stop grace period updated.');+ } catch (\Throwable $e) {+ return handleError($e, $this);+ }+ }+public function render(){return view('livewire.project.application.advanced');diff --git a/app/Livewire/Project/Application/Previews.php b/app/Livewire/Project/Application/Previews.phpindex c887e9b833..9dd494f5ca 100644--- a/app/Livewire/Project/Application/Previews.php+++ b/app/Livewire/Project/Application/Previews.php@@ -338,10 +338,13 @@ class Previews extends Componentprivate function stopContainers(array $containers, $server){$containersToStop = collect($containers)->pluck('Names')->toArray();+ $timeout = ($this->application->settings->stop_grace_period > 0)+ ? $this->application->settings->stop_grace_period+ : DEFAULT_STOP_GRACE_PERIOD_SECONDS;foreach ($containersToStop as $containerName) {instant_remote_process(command: [- "docker stop -t 30 $containerName",+ "docker stop --time=$timeout $containerName","docker rm -f $containerName",], server: $server, throwError: false);}diff --git a/app/Models/ApplicationSetting.php b/app/Models/ApplicationSetting.phpindex 731a9b5da0..c365bd1872 100644--- a/app/Models/ApplicationSetting.php+++ b/app/Models/ApplicationSetting.php@@ -26,6 +26,7 @@ class ApplicationSetting extends Model'is_git_lfs_enabled' => 'boolean','is_git_shallow_clone_enabled' => 'boolean','docker_images_to_keep' => 'integer',+ 'stop_grace_period' => 'integer',];protected $fillable = [diff --git a/bootstrap/helpers/constants.php b/bootstrap/helpers/constants.phpindex bae2573dee..ee6a3bc036 100644--- a/bootstrap/helpers/constants.php+++ b/bootstrap/helpers/constants.php@@ -16,6 +16,7 @@ const VALID_CRON_STRINGS = ['@yearly' => '0 0 1 1 *',];const RESTART_MODE = 'unless-stopped';+const DEFAULT_STOP_GRACE_PERIOD_SECONDS = 30;const DATABASE_DOCKER_IMAGES = ['bitnami/mariadb',diff --git a/database/migrations/2025_11_05_091558_add_stop_grace_period_to_application_settings.php b/database/migrations/2025_11_05_091558_add_stop_grace_period_to_application_settings.phpnew file mode 100644index 0000000000..cc702ce5c7--- /dev/null+++ b/database/migrations/2025_11_05_091558_add_stop_grace_period_to_application_settings.php@@ -0,0 +1,31 @@+<?php++use Illuminate\Database\Migrations\Migration;+use Illuminate\Database\Schema\Blueprint;+use Illuminate\Support\Facades\Schema;++return new class extends Migration+{+ /**+ * Run the migrations.+ */+ public function up(): void+ {+ Schema::table('application_settings', function (Blueprint $table) {+ $table->integer('stop_grace_period')+ ->nullable()+ ->after('use_build_secrets')+ ->comment('Seconds to wait for graceful shutdown before forcing container stop (1-3600). Null uses default of 30 seconds.');+ });+ }++ /**+ * Reverse the migrations.+ */+ public function down(): void+ {+ Schema::table('application_settings', function (Blueprint $table) {+ $table->dropColumn('stop_grace_period');+ });+ }+};diff --git a/resources/views/livewire/project/application/advanced.blade.php b/resources/views/livewire/project/application/advanced.blade.phpindex a9f8c72333..362539a3c5 100644--- a/resources/views/livewire/project/application/advanced.blade.php+++ b/resources/views/livewire/project/application/advanced.blade.php@@ -86,7 +86,21 @@helper="Readonly labels are disabled. You need to set the labels in the labels section." disabledinstantSave id="isStripprefixEnabled" label="Strip Prefixes" canGate="update" :canResource="$application" />@endif-+ <h3 class="pt-4">Operations</h3>+ <form class="flex items-end gap-2" wire:submit.prevent='saveStopGracePeriod'>+ <x-forms.input+ type="number"+ id="stopGracePeriod"+ label="Stop Grace Period (seconds)"+ placeholder="{{ DEFAULT_STOP_GRACE_PERIOD_SECONDS }}"+ helper="How long to wait for graceful shutdown during rolling updates, manual stops, and restarts. Applies to all containers for this application. Default: {{ DEFAULT_STOP_GRACE_PERIOD_SECONDS }} seconds. Range: 1-3600 seconds (1 hour)."+ min="1"+ max="3600"+ canGate="update"+ :canResource="$application"+ />+ <x-forms.button canGate="update" :canResource="$application" type="submit">Save</x-forms.button>+ </form><h3 class="pt-4">Logs</h3><x-forms.checkbox helper="Drain logs to your configured log drain endpoint in your Server settings."instantSave id="isLogDrainEnabled" label="Drain Logs" canGate="update" :canResource="$application" />diff --git a/tests/Unit/ApplicationSettingStaticCastTest.php b/tests/Unit/ApplicationSettingStaticCastTest.phpindex 35ab7faaf2..d06ee920d2 100644--- a/tests/Unit/ApplicationSettingStaticCastTest.php+++ b/tests/Unit/ApplicationSettingStaticCastTest.php@@ -103,3 +103,49 @@ it('casts all boolean fields correctly', function () {->and($casts[$field])->toBe('boolean');}});++it('casts stop_grace_period to integer', function () {+ $setting = new ApplicationSetting;+ $casts = $setting->getCasts();++ expect($casts)->toHaveKey('stop_grace_period')+ ->and($casts['stop_grace_period'])->toBe('integer');+});++it('handles null stop_grace_period for default behavior', function () {+ $setting = new ApplicationSetting;+ $setting->stop_grace_period = null;++ expect($setting->stop_grace_period)->toBeNull();+});++it('casts stop_grace_period from string to integer', function () {+ $setting = new ApplicationSetting;+ $setting->stop_grace_period = '60';++ expect($setting->stop_grace_period)->toBe(60)+ ->and($setting->stop_grace_period)->toBeInt();+});++it('casts stop_grace_period zero to integer (documents fallback trigger)', function () {+ // Value of 0 is not a valid grace period — consumers guard with+ // `($value > 0) ? $value : DEFAULT_STOP_GRACE_PERIOD_SECONDS`, so+ // the cast itself must still round-trip cleanly without throwing.+ $setting = new ApplicationSetting;+ $setting->stop_grace_period = 0;++ expect($setting->stop_grace_period)->toBe(0)+ ->and($setting->stop_grace_period)->toBeInt();+});++it('casts stop_grace_period negative value to integer (documents fallback trigger)', function () {+ // Negative values should never be persisted (UI validates `min=1`),+ // but if one slips through (direct DB write, older data), the cast+ // must not throw and consumers will treat it as the fallback via the+ // `> 0` guard.+ $setting = new ApplicationSetting;+ $setting->stop_grace_period = -10;++ expect($setting->stop_grace_period)->toBe(-10)+ ->and($setting->stop_grace_period)->toBeInt();+});