coolify/app/Actions/Service/StopService.php

46 lines
1.9 KiB
PHP
Raw Normal View History

2023-09-21 19:30:13 +00:00
<?php
namespace App\Actions\Service;
use App\Models\Service;
2024-06-10 20:43:34 +00:00
use Lorisleiva\Actions\Concerns\AsAction;
2023-09-21 19:30:13 +00:00
class StopService
{
use AsAction;
2024-06-10 20:43:34 +00:00
public function handle(Service $service, bool $isDeleteOperation = false)
2023-09-21 19:30:13 +00:00
{
try {
$server = $service->destination->server;
if (!$server->isFunctional()) {
return 'Server is not functional';
}
ray('Stopping service: ' . $service->name);
$applications = $service->applications()->get();
foreach ($applications as $application) {
2024-08-06 11:27:06 +00:00
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);
$application->update(['status' => 'exited']);
}
$dbs = $service->databases()->get();
foreach ($dbs as $db) {
2024-08-06 11:27:06 +00:00
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);
$db->update(['status' => 'exited']);
}
if (!$isDeleteOperation) {
// Only run this if not a delete operation
$service->delete_connected_networks($service->uuid);
}
} catch (\Exception $e) {
ray($e->getMessage());
2024-06-10 20:43:34 +00:00
return $e->getMessage();
2024-01-31 08:58:41 +00:00
}
2023-09-21 19:30:13 +00:00
}
}