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
refactor(core): streamline container stopping process and reduce timeout duration; update related methods for consistency
Andras Bacsaie062edb68938
diff --git a/app/Actions/CoolifyTask/RunRemoteProcess.php b/app/Actions/CoolifyTask/RunRemoteProcess.phpindex 926d30fe6c..8ce9aa1f23 100644--- a/app/Actions/CoolifyTask/RunRemoteProcess.php+++ b/app/Actions/CoolifyTask/RunRemoteProcess.php@@ -85,7 +85,6 @@ class RunRemoteProcess]);$processResult = $process->wait();- // $processResult = Process::timeout($timeout)->run($this->getCommand(), $this->handleOutput(...));if ($this->activity->properties->get('status') === ProcessStatus::ERROR->value) {$status = ProcessStatus::ERROR;} else {diff --git a/app/Actions/Proxy/StopProxy.php b/app/Actions/Proxy/StopProxy.phpindex a5dcc6cf43..75b22d2368 100644--- a/app/Actions/Proxy/StopProxy.php+++ b/app/Actions/Proxy/StopProxy.php@@ -3,54 +3,27 @@namespace App\Actions\Proxy;use App\Models\Server;-use Carbon\Carbon;-use Illuminate\Process\InvokedProcess;-use Illuminate\Support\Facades\Process;use Lorisleiva\Actions\Concerns\AsAction;class StopProxy{use AsAction;- public function handle(Server $server, bool $forceStop = true)+ public function handle(Server $server, bool $forceStop = true, int $timeout = 30){try {$containerName = $server->isSwarm() ? 'coolify-proxy_traefik' : 'coolify-proxy';- $timeout = 30;- $process = $this->stopContainer($containerName, $timeout);+ instant_remote_process(command: [+ "docker stop --time=$timeout $containerName",+ "docker rm -f $containerName",+ ], server: $server, throwError: false);- $startTime = Carbon::now()->getTimestamp();- while ($process->running()) {- if (Carbon::now()->getTimestamp() - $startTime >= $timeout) {- $this->forceStopContainer($containerName, $server);- break;- }- usleep(100000);- }-- $this->removeContainer($containerName, $server);- } catch (\Throwable $e) {- return handleError($e);- } finally {$server->proxy->force_stop = $forceStop;$server->proxy->status = 'exited';$server->save();+ } catch (\Throwable $e) {+ return handleError($e);}}-- private function stopContainer(string $containerName, int $timeout): InvokedProcess- {- return Process::timeout($timeout)->start("docker stop --time=$timeout $containerName");- }-- private function forceStopContainer(string $containerName, Server $server)- {- instant_remote_process(["docker kill $containerName"], $server, throwError: false);- }-- private function removeContainer(string $containerName, Server $server)- {- instant_remote_process(["docker rm -f $containerName"], $server, throwError: false);- }}diff --git a/app/Livewire/Project/Application/Previews.php b/app/Livewire/Project/Application/Previews.phpindex 88ce65c530..193a22280b 100644--- a/app/Livewire/Project/Application/Previews.php+++ b/app/Livewire/Project/Application/Previews.php@@ -5,10 +5,7 @@ namespace App\Livewire\Project\Application;use App\Actions\Docker\GetContainersStatus;use App\Models\Application;use App\Models\ApplicationPreview;-use Carbon\Carbon;-use Illuminate\Process\InvokedProcess;use Illuminate\Support\Collection;-use Illuminate\Support\Facades\Process;use Livewire\Component;use Spatie\Url\Url;use Visus\Cuid2\Cuid2;@@ -193,13 +190,12 @@ class Previews extends Component{try {$server = $this->application->destination->server;- $timeout = 300;if ($this->application->destination->server->isSwarm()) {instant_remote_process(["docker stack rm {$this->application->uuid}-{$pull_request_id}"], $server);} else {$containers = getCurrentApplicationContainerStatus($server, $this->application->id, $pull_request_id)->toArray();- $this->stopContainers($containers, $server, $timeout);+ $this->stopContainers($containers, $server);}GetContainersStatus::run($server);@@ -215,13 +211,12 @@ class Previews extends Component{try {$server = $this->application->destination->server;- $timeout = 300;if ($this->application->destination->server->isSwarm()) {instant_remote_process(["docker stack rm {$this->application->uuid}-{$pull_request_id}"], $server);} else {$containers = getCurrentApplicationContainerStatus($server, $this->application->id, $pull_request_id)->toArray();- $this->stopContainers($containers, $server, $timeout);+ $this->stopContainers($containers, $server);}ApplicationPreview::where('application_id', $this->application->id)@@ -237,48 +232,14 @@ class Previews extends Component}}- private function stopContainers(array $containers, $server, int $timeout)+ private function stopContainers(array $containers, $server, int $timeout = 30){- $processes = [];foreach ($containers as $container) {$containerName = str_replace('/', '', $container['Names']);- $processes[$containerName] = $this->stopContainer($containerName, $timeout);- }-- $startTime = Carbon::now()->getTimestamp();- while (count($processes) > 0) {- $finishedProcesses = array_filter($processes, function ($process) {- return ! $process->running();- });- foreach (array_keys($finishedProcesses) as $containerName) {- unset($processes[$containerName]);- $this->removeContainer($containerName, $server);- }-- if (Carbon::now()->getTimestamp() - $startTime >= $timeout) {- $this->forceStopRemainingContainers(array_keys($processes), $server);- break;- }-- usleep(100000);- }- }-- private function stopContainer(string $containerName, int $timeout): InvokedProcess- {- return Process::timeout($timeout)->start("docker stop --time=$timeout $containerName");- }-- private function removeContainer(string $containerName, $server)- {- instant_remote_process(["docker rm -f $containerName"], $server, throwError: false);- }-- private function forceStopRemainingContainers(array $containerNames, $server)- {- foreach ($containerNames as $containerName) {- instant_remote_process(["docker kill $containerName"], $server, throwError: false);- $this->removeContainer($containerName, $server);+ instant_remote_process(command: [+ "docker stop --time=$timeout $containerName",+ "docker rm -f $containerName",+ ], server: $server, throwError: false);}}}diff --git a/app/Models/Application.php b/app/Models/Application.phpindex 233c8d8063..124a62eb7a 100644--- a/app/Models/Application.php+++ b/app/Models/Application.php@@ -9,9 +9,7 @@ use Illuminate\Database\Eloquent\Casts\Attribute;use Illuminate\Database\Eloquent\Factories\HasFactory;use Illuminate\Database\Eloquent\Relations\HasMany;use Illuminate\Database\Eloquent\SoftDeletes;-use Illuminate\Process\InvokedProcess;use Illuminate\Support\Collection;-use Illuminate\Support\Facades\Process;use Illuminate\Support\Facades\Validator;use Illuminate\Support\Str;use OpenApi\Attributes as OA;@@ -270,47 +268,13 @@ class Application extends BaseModelreturn $containers->pluck('Names')->toArray();}- public function stopContainers(array $containerNames, $server, int $timeout = 600)+ public function stopContainers(array $containerNames, $server, int $timeout = 30){- $processes = [];foreach ($containerNames as $containerName) {- $processes[$containerName] = $this->stopContainer($containerName, $server, $timeout);- }-- $startTime = time();- while (count($processes) > 0) {- $finishedProcesses = array_filter($processes, function ($process) {- return ! $process->running();- });- foreach ($finishedProcesses as $containerName => $process) {- unset($processes[$containerName]);- $this->removeContainer($containerName, $server);- }-- if (time() - $startTime >= $timeout) {- $this->forceStopRemainingContainers(array_keys($processes), $server);- break;- }-- usleep(100000);- }- }-- public function stopContainer(string $containerName, $server, int $timeout): InvokedProcess- {- return Process::timeout($timeout)->start("docker stop --time=$timeout $containerName");- }-- public function removeContainer(string $containerName, $server)- {- instant_remote_process(command: ["docker rm -f $containerName"], server: $server, throwError: false);- }-- public function forceStopRemainingContainers(array $containerNames, $server)- {- foreach ($containerNames as $containerName) {- instant_remote_process(command: ["docker kill $containerName"], server: $server, throwError: false);- $this->removeContainer($containerName, $server);+ instant_remote_process(command: [+ "docker stop --time=$timeout $containerName",+ "docker rm -f $containerName",+ ], server: $server, throwError: false);}}diff --git a/app/Models/Service.php b/app/Models/Service.phpindex 23ddb5923a..5232e5897c 100644--- a/app/Models/Service.php+++ b/app/Models/Service.php@@ -6,9 +6,7 @@ use Illuminate\Database\Eloquent\Casts\Attribute;use Illuminate\Database\Eloquent\Factories\HasFactory;use Illuminate\Database\Eloquent\Relations\HasMany;use Illuminate\Database\Eloquent\SoftDeletes;-use Illuminate\Process\InvokedProcess;use Illuminate\Support\Collection;-use Illuminate\Support\Facades\Process;use Illuminate\Support\Facades\Storage;use OpenApi\Attributes as OA;use Spatie\Url\Url;@@ -158,47 +156,13 @@ class Service extends BaseModelreturn $containersToStop;}- public function stopContainers(array $containerNames, $server, int $timeout = 300)+ public function stopContainers(array $containerNames, $server, int $timeout = 30){- $processes = [];foreach ($containerNames as $containerName) {- $processes[$containerName] = $this->stopContainer($containerName, $timeout);- }-- $startTime = time();- while (count($processes) > 0) {- $finishedProcesses = array_filter($processes, function ($process) {- return ! $process->running();- });- foreach (array_keys($finishedProcesses) as $containerName) {- unset($processes[$containerName]);- $this->removeContainer($containerName, $server);- }-- if (time() - $startTime >= $timeout) {- $this->forceStopRemainingContainers(array_keys($processes), $server);- break;- }-- usleep(100000);- }- }-- public function stopContainer(string $containerName, int $timeout): InvokedProcess- {- return Process::timeout($timeout)->start("docker stop --time=$timeout $containerName");- }-- public function removeContainer(string $containerName, $server)- {- instant_remote_process(command: ["docker rm -f $containerName"], server: $server, throwError: false);- }-- public function forceStopRemainingContainers(array $containerNames, $server)- {- foreach ($containerNames as $containerName) {- instant_remote_process(command: ["docker kill $containerName"], server: $server, throwError: false);- $this->removeContainer($containerName, $server);+ instant_remote_process(command: [+ "docker stop --time=$timeout $containerName",+ "docker rm -f $containerName",+ ], server: $server, throwError: false);}}diff --git a/resources/views/livewire/project/application/heading.blade.php b/resources/views/livewire/project/application/heading.blade.phpindex aecab02650..5605975300 100644--- a/resources/views/livewire/project/application/heading.blade.php+++ b/resources/views/livewire/project/application/heading.blade.php@@ -117,7 +117,8 @@@script<script>$wire.$on('stopEvent', () => {- $wire.$dispatch('info', 'Stopping application.');+ $wire.$dispatch('info',+ 'Gracefully stopping application, it could take a while depending on the application.');$wire.$call('stop');});</script>diff --git a/resources/views/livewire/project/service/navbar.blade.php b/resources/views/livewire/project/service/navbar.blade.phpindex 4b3c935042..dd2ce41427 100644--- a/resources/views/livewire/project/service/navbar.blade.php+++ b/resources/views/livewire/project/service/navbar.blade.php@@ -138,7 +138,7 @@@script<script>$wire.$on('stopEvent', () => {- $wire.$dispatch('info', 'Stopping service.');+ $wire.$dispatch('info', 'Gracefully stopping service, it could take a while depending on the service.');$wire.$call('stop');});$wire.$on('startEvent', async () => {