graceful service container stop

This commit is contained in:
ayntk-ai
2024-08-09 18:59:41 +02:00
parent d177e49e62
commit 72bcf03cbb

View File

@@ -3,6 +3,7 @@
namespace App\Actions\Service;
use App\Models\Service;
use App\Actions\Server\CleanupDocker;
use Lorisleiva\Actions\Concerns\AsAction;
class StopService
@@ -19,27 +20,38 @@ class StopService
ray('Stopping service: ' . $service->name);
$applications = $service->applications()->get();
foreach ($applications as $application) {
instant_remote_process(command: ["docker stop --time=30 {$application->name}-{$service->uuid}"], server: $server, throwError: false);
instant_remote_process(command: ["docker rm {$application->name}-{$service->uuid}"], server: $server, throwError: false);
instant_remote_process(command: ["docker rm -f {$application->name}-{$service->uuid}"], server: $server, throwError: false);
$this->stopContainer("{$application->name}-{$service->uuid}", $server, 600);
$application->update(['status' => 'exited']);
}
$dbs = $service->databases()->get();
foreach ($dbs as $db) {
instant_remote_process(command: ["docker stop --time=30 {$db->name}-{$service->uuid}"], server: $server, throwError: false);
instant_remote_process(command: ["docker rm {$db->name}-{$service->uuid}"], server: $server, throwError: false);
instant_remote_process(command: ["docker rm -f {$db->name}-{$service->uuid}"], server: $server, throwError: false);
$this->stopContainer("{$db->name}-{$service->uuid}", $server, 600);
$db->update(['status' => 'exited']);
}
if (!$isDeleteOperation) {
// Only run this if not a delete operation
// Only run if not a deletion operation as for deletion we can specify if we want to delete networks or not
$service->delete_connected_networks($service->uuid);
CleanupDocker::run($server, true);
}
} catch (\Exception $e) {
ray($e->getMessage());
return $e->getMessage();
}
}
private function stopContainer(string $containerName, $server, int $timeout = 600)
{
try {
instant_remote_process(command: ["docker stop --time=$timeout $containerName"], server: $server, throwError: false);
$isRunning = instant_remote_process(command: ["docker inspect -f '{{.State.Running}}' $containerName"], server: $server, throwError: false);
if (trim($isRunning) === 'true') {
instant_remote_process(command: ["docker kill $containerName"], server: $server, throwError: false);
}
} catch (\Exception $error) {
}
instant_remote_process(command: ["docker rm -f $containerName"], server: $server, throwError: false);
}
}