mirror of
https://github.com/tiennm99/coolify.git
synced 2026-08-20 04:23:34 +00:00
Persist exited status across application, database, service, and preview stop paths, and reuse the split host/port/path input for S3 endpoints with linked validation feedback.
73 lines
2.5 KiB
PHP
73 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Actions\Application;
|
|
|
|
use App\Actions\Server\CleanupDocker;
|
|
use App\Events\ServiceStatusChanged;
|
|
use App\Models\Application;
|
|
use Lorisleiva\Actions\Concerns\AsAction;
|
|
|
|
class StopApplication
|
|
{
|
|
use AsAction;
|
|
|
|
public string $jobQueue = 'high';
|
|
|
|
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) {
|
|
$servers = $servers->merge($application->additional_servers);
|
|
}
|
|
foreach ($servers as $server) {
|
|
try {
|
|
if (! $server->isFunctional()) {
|
|
return 'Server is not functional';
|
|
}
|
|
|
|
if ($server->isSwarm()) {
|
|
instant_remote_process(["docker stack rm {$application->uuid}"], $server);
|
|
|
|
continue;
|
|
}
|
|
|
|
$containers = $previewDeployments
|
|
? getCurrentApplicationContainerStatus($server, $application->id, includePullrequests: true)
|
|
: getCurrentApplicationContainerStatus($server, $application->id, 0);
|
|
|
|
$containersToStop = $containers->pluck('Names')->toArray();
|
|
$timeout = $application->settings->stopGracePeriodSeconds();
|
|
|
|
foreach ($containersToStop as $containerName) {
|
|
instant_remote_process(command: [
|
|
dockerStopCommand($timeout, $containerName, $server),
|
|
"docker rm -f $containerName",
|
|
], server: $server, throwError: false);
|
|
}
|
|
|
|
if ($application->build_pack === 'dockercompose') {
|
|
$application->deleteConnectedNetworks();
|
|
}
|
|
|
|
if ($dockerCleanup) {
|
|
CleanupDocker::dispatch($server, false, false);
|
|
}
|
|
} catch (\Exception $e) {
|
|
return $e->getMessage();
|
|
}
|
|
}
|
|
|
|
$status = ['status' => 'exited'];
|
|
if ($resetRestartCount) {
|
|
$status = array_merge($status, [
|
|
'restart_count' => 0,
|
|
'last_restart_at' => null,
|
|
'last_restart_type' => null,
|
|
]);
|
|
}
|
|
$application->update($status);
|
|
|
|
ServiceStatusChanged::dispatch($application->environment->project->team->id);
|
|
}
|
|
}
|