2023-09-14 10:45:50 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
|
|
use App\Actions\Proxy\StartProxy;
|
2023-09-14 13:52:04 +00:00
|
|
|
use App\Models\ApplicationPreview;
|
2023-09-14 10:45:50 +00:00
|
|
|
use App\Models\Server;
|
|
|
|
use App\Notifications\Container\ContainerRestarted;
|
|
|
|
use App\Notifications\Container\ContainerStopped;
|
2023-10-06 22:51:01 +00:00
|
|
|
use App\Notifications\Server\Revived;
|
2023-09-14 10:45:50 +00:00
|
|
|
use App\Notifications\Server\Unreachable;
|
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
|
use Illuminate\Contracts\Queue\ShouldBeEncrypted;
|
|
|
|
use Illuminate\Contracts\Queue\ShouldBeUnique;
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
use Illuminate\Queue\Middleware\WithoutOverlapping;
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
2023-09-19 13:51:13 +00:00
|
|
|
use Illuminate\Support\Arr;
|
2023-09-15 09:19:36 +00:00
|
|
|
use Illuminate\Support\Str;
|
2023-09-14 10:45:50 +00:00
|
|
|
|
2023-09-14 15:22:21 +00:00
|
|
|
class ContainerStatusJob implements ShouldQueue, ShouldBeEncrypted
|
2023-09-14 10:45:50 +00:00
|
|
|
{
|
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
|
|
public $tries = 1;
|
|
|
|
public $timeout = 120;
|
|
|
|
|
|
|
|
public function __construct(public Server $server)
|
|
|
|
{
|
2023-09-28 09:33:16 +00:00
|
|
|
$this->handle();
|
2023-09-14 10:45:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function middleware(): array
|
|
|
|
{
|
2023-09-28 09:33:16 +00:00
|
|
|
return [(new WithoutOverlapping($this->server->uuid))->dontRelease()];
|
2023-09-14 10:45:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function uniqueId(): string
|
|
|
|
{
|
|
|
|
return $this->server->uuid;
|
|
|
|
}
|
2023-10-09 09:00:18 +00:00
|
|
|
public function handle()
|
2023-09-14 10:45:50 +00:00
|
|
|
{
|
|
|
|
try {
|
2023-10-09 18:37:42 +00:00
|
|
|
// ray("checking server status for {$this->server->name}");
|
2023-09-18 07:58:13 +00:00
|
|
|
// ray()->clearAll();
|
2023-10-09 10:07:42 +00:00
|
|
|
$serverUptimeCheckNumber = $this->server->unreachable_count;
|
2023-09-18 09:49:26 +00:00
|
|
|
$serverUptimeCheckNumberMax = 3;
|
2023-10-09 10:07:42 +00:00
|
|
|
|
2023-10-09 18:37:42 +00:00
|
|
|
// ray('checking # ' . $serverUptimeCheckNumber);
|
2023-10-09 10:07:42 +00:00
|
|
|
if ($serverUptimeCheckNumber >= $serverUptimeCheckNumberMax) {
|
|
|
|
if ($this->server->unreachable_email_sent === false) {
|
|
|
|
ray('Server unreachable, sending notification...');
|
|
|
|
// $this->server->team->notify(new Unreachable($this->server));
|
2023-10-06 22:51:01 +00:00
|
|
|
$this->server->update(['unreachable_email_sent' => true]);
|
2023-09-14 10:45:50 +00:00
|
|
|
}
|
2023-10-09 10:07:42 +00:00
|
|
|
$this->server->settings()->update([
|
|
|
|
'is_reachable' => false,
|
2023-10-09 10:13:30 +00:00
|
|
|
]);
|
2023-10-09 10:07:42 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
$result = $this->server->validateConnection();
|
|
|
|
if ($result) {
|
|
|
|
$this->server->settings()->update([
|
|
|
|
'is_reachable' => true,
|
2023-10-09 10:13:30 +00:00
|
|
|
]);
|
|
|
|
$this->server->update([
|
2023-10-09 10:07:42 +00:00
|
|
|
'unreachable_count' => 0,
|
|
|
|
]);
|
|
|
|
} else {
|
2023-09-14 10:45:50 +00:00
|
|
|
$serverUptimeCheckNumber++;
|
2023-10-09 10:07:42 +00:00
|
|
|
$this->server->settings()->update([
|
|
|
|
'is_reachable' => false,
|
2023-10-09 10:13:30 +00:00
|
|
|
]);
|
|
|
|
$this->server->update([
|
2023-10-09 10:07:42 +00:00
|
|
|
'unreachable_count' => $serverUptimeCheckNumber,
|
|
|
|
]);
|
|
|
|
return;
|
2023-09-14 10:45:50 +00:00
|
|
|
}
|
2023-10-09 10:07:42 +00:00
|
|
|
|
2023-10-06 22:51:01 +00:00
|
|
|
if (data_get($this->server, 'unreachable_email_sent') === true) {
|
2023-10-09 09:00:18 +00:00
|
|
|
ray('Server is reachable again, sending notification...');
|
2023-10-09 09:49:38 +00:00
|
|
|
// $this->server->team->notify(new Revived($this->server));
|
2023-10-06 22:51:01 +00:00
|
|
|
$this->server->update(['unreachable_email_sent' => false]);
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
data_get($this->server, 'settings.is_reachable') === false ||
|
|
|
|
data_get($this->server, 'settings.is_usable') === false
|
|
|
|
) {
|
|
|
|
$this->server->settings()->update([
|
|
|
|
'is_reachable' => true,
|
|
|
|
'is_usable' => true
|
|
|
|
]);
|
|
|
|
}
|
2023-10-09 10:07:42 +00:00
|
|
|
// $this->server->validateDockerEngine(true);
|
2023-09-18 09:49:26 +00:00
|
|
|
$containers = instant_remote_process(["docker container ls -q"], $this->server);
|
|
|
|
if (!$containers) {
|
|
|
|
return;
|
|
|
|
}
|
2023-09-14 10:45:50 +00:00
|
|
|
$containers = instant_remote_process(["docker container inspect $(docker container ls -q) --format '{{json .}}'"], $this->server);
|
|
|
|
$containers = format_docker_command_output_to_json($containers);
|
|
|
|
$applications = $this->server->applications();
|
|
|
|
$databases = $this->server->databases();
|
2023-09-26 13:07:33 +00:00
|
|
|
$services = $this->server->services()->get();
|
2023-09-14 13:52:04 +00:00
|
|
|
$previews = $this->server->previews();
|
2023-09-25 07:17:42 +00:00
|
|
|
$this->server->proxyType();
|
2023-09-18 10:08:19 +00:00
|
|
|
/// Check if proxy is running
|
|
|
|
$foundProxyContainer = $containers->filter(function ($value, $key) {
|
|
|
|
return data_get($value, 'Name') === '/coolify-proxy';
|
|
|
|
})->first();
|
|
|
|
if (!$foundProxyContainer) {
|
2023-09-25 07:17:42 +00:00
|
|
|
ray('Proxy not found, starting it...');
|
2023-09-18 10:08:19 +00:00
|
|
|
if ($this->server->isProxyShouldRun()) {
|
2023-09-18 10:29:50 +00:00
|
|
|
StartProxy::run($this->server, false);
|
2023-09-14 10:45:50 +00:00
|
|
|
$this->server->team->notify(new ContainerRestarted('coolify-proxy', $this->server));
|
|
|
|
}
|
2023-09-18 10:08:19 +00:00
|
|
|
} else {
|
|
|
|
$this->server->proxy->status = data_get($foundProxyContainer, 'State.Status');
|
|
|
|
$this->server->save();
|
2023-09-22 06:42:27 +00:00
|
|
|
$connectProxyToDockerNetworks = connectProxyToNetworks($this->server);
|
2023-09-22 06:52:07 +00:00
|
|
|
instant_remote_process($connectProxyToDockerNetworks, $this->server, false);
|
2023-09-14 10:45:50 +00:00
|
|
|
}
|
2023-09-14 13:52:04 +00:00
|
|
|
$foundApplications = [];
|
|
|
|
$foundApplicationPreviews = [];
|
|
|
|
$foundDatabases = [];
|
2023-09-21 15:48:31 +00:00
|
|
|
$foundServices = [];
|
2023-09-22 06:42:27 +00:00
|
|
|
|
2023-09-14 13:52:04 +00:00
|
|
|
foreach ($containers as $container) {
|
|
|
|
$containerStatus = data_get($container, 'State.Status');
|
2023-09-25 07:17:42 +00:00
|
|
|
$containerHealth = data_get($container, 'State.Health.Status', 'unhealthy');
|
2023-09-22 13:29:19 +00:00
|
|
|
$containerStatus = "$containerStatus ($containerHealth)";
|
2023-09-14 13:52:04 +00:00
|
|
|
$labels = data_get($container, 'Config.Labels');
|
|
|
|
$labels = Arr::undot(format_docker_labels_to_json($labels));
|
|
|
|
$labelId = data_get($labels, 'coolify.applicationId');
|
|
|
|
if ($labelId) {
|
2023-09-18 10:08:19 +00:00
|
|
|
if (str_contains($labelId, '-pr-')) {
|
2023-10-01 10:02:44 +00:00
|
|
|
$pullRequestId = data_get($labels, 'coolify.pullRequestId');
|
2023-09-14 13:52:04 +00:00
|
|
|
$applicationId = (int) Str::before($labelId, '-pr-');
|
2023-10-01 10:02:44 +00:00
|
|
|
$preview = ApplicationPreview::where('application_id', $applicationId)->where('pull_request_id', $pullRequestId)->first();
|
2023-09-14 13:52:04 +00:00
|
|
|
if ($preview) {
|
|
|
|
$foundApplicationPreviews[] = $preview->id;
|
|
|
|
$statusFromDb = $preview->status;
|
|
|
|
if ($statusFromDb !== $containerStatus) {
|
|
|
|
$preview->update(['status' => $containerStatus]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//Notify user that this container should not be there.
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$application = $applications->where('id', $labelId)->first();
|
|
|
|
if ($application) {
|
|
|
|
$foundApplications[] = $application->id;
|
|
|
|
$statusFromDb = $application->status;
|
|
|
|
if ($statusFromDb !== $containerStatus) {
|
|
|
|
$application->update(['status' => $containerStatus]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//Notify user that this container should not be there.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$uuid = data_get($labels, 'com.docker.compose.service');
|
|
|
|
if ($uuid) {
|
|
|
|
$database = $databases->where('uuid', $uuid)->first();
|
|
|
|
if ($database) {
|
|
|
|
$foundDatabases[] = $database->id;
|
|
|
|
$statusFromDb = $database->status;
|
|
|
|
if ($statusFromDb !== $containerStatus) {
|
|
|
|
$database->update(['status' => $containerStatus]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Notify user that this container should not be there.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-21 15:48:31 +00:00
|
|
|
$serviceLabelId = data_get($labels, 'coolify.serviceId');
|
|
|
|
if ($serviceLabelId) {
|
2023-09-25 13:48:43 +00:00
|
|
|
$subType = data_get($labels, 'coolify.service.subType');
|
|
|
|
$subId = data_get($labels, 'coolify.service.subId');
|
|
|
|
$service = $services->where('id', $serviceLabelId)->first();
|
2023-09-26 12:45:52 +00:00
|
|
|
if (!$service) {
|
|
|
|
continue;
|
|
|
|
}
|
2023-09-25 13:48:43 +00:00
|
|
|
if ($subType === 'application') {
|
|
|
|
$service = $service->applications()->where('id', $subId)->first();
|
|
|
|
} else {
|
|
|
|
$service = $service->databases()->where('id', $subId)->first();
|
|
|
|
}
|
2023-09-21 15:48:31 +00:00
|
|
|
if ($service) {
|
2023-09-25 13:48:43 +00:00
|
|
|
$foundServices[] = "$service->id-$service->name";
|
|
|
|
$statusFromDb = $service->status;
|
|
|
|
if ($statusFromDb !== $containerStatus) {
|
|
|
|
// ray('Updating status: ' . $containerStatus);
|
|
|
|
$service->update(['status' => $containerStatus]);
|
2023-09-21 15:48:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-14 13:52:04 +00:00
|
|
|
}
|
2023-09-21 15:48:31 +00:00
|
|
|
$exitedServices = collect([]);
|
2023-09-26 13:07:33 +00:00
|
|
|
foreach ($services as $service) {
|
2023-09-21 15:48:31 +00:00
|
|
|
$apps = $service->applications()->get();
|
|
|
|
$dbs = $service->databases()->get();
|
|
|
|
foreach ($apps as $app) {
|
2023-09-22 18:39:56 +00:00
|
|
|
if (in_array("$app->id-$app->name", $foundServices)) {
|
2023-09-21 15:48:31 +00:00
|
|
|
continue;
|
|
|
|
} else {
|
2023-09-22 09:23:49 +00:00
|
|
|
$exitedServices->push($app);
|
2023-09-21 15:48:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
foreach ($dbs as $db) {
|
2023-09-22 18:39:56 +00:00
|
|
|
if (in_array("$db->id-$db->name", $foundServices)) {
|
2023-09-21 15:48:31 +00:00
|
|
|
continue;
|
|
|
|
} else {
|
2023-09-22 09:23:49 +00:00
|
|
|
$exitedServices->push($db);
|
2023-09-21 15:48:31 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-22 09:23:49 +00:00
|
|
|
}
|
2023-09-21 15:48:31 +00:00
|
|
|
$exitedServices = $exitedServices->unique('id');
|
2023-09-22 09:23:49 +00:00
|
|
|
foreach ($exitedServices as $exitedService) {
|
|
|
|
if ($exitedService->status === 'exited') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$name = data_get($exitedService, 'name');
|
|
|
|
$fqdn = data_get($exitedService, 'fqdn');
|
|
|
|
$containerName = $name ? "$name ($fqdn)" : $fqdn;
|
|
|
|
$project = data_get($service, 'environment.project');
|
|
|
|
$environment = data_get($service, 'environment');
|
|
|
|
|
|
|
|
$url = base_url() . '/project/' . $project->uuid . "/" . $environment->name . "/service/" . $service->uuid;
|
|
|
|
$this->server->team->notify(new ContainerStopped($containerName, $this->server, $url));
|
|
|
|
$exitedService->update(['status' => 'exited']);
|
|
|
|
}
|
2023-09-21 15:48:31 +00:00
|
|
|
|
2023-09-14 13:52:04 +00:00
|
|
|
$notRunningApplications = $applications->pluck('id')->diff($foundApplications);
|
2023-09-18 10:08:19 +00:00
|
|
|
foreach ($notRunningApplications as $applicationId) {
|
2023-09-14 13:52:04 +00:00
|
|
|
$application = $applications->where('id', $applicationId)->first();
|
|
|
|
if ($application->status === 'exited') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$application->update(['status' => 'exited']);
|
|
|
|
|
|
|
|
$name = data_get($application, 'name');
|
|
|
|
$fqdn = data_get($application, 'fqdn');
|
|
|
|
|
|
|
|
$containerName = $name ? "$name ($fqdn)" : $fqdn;
|
|
|
|
|
|
|
|
$project = data_get($application, 'environment.project');
|
|
|
|
$environment = data_get($application, 'environment');
|
|
|
|
|
|
|
|
$url = base_url() . '/project/' . $project->uuid . "/" . $environment->name . "/application/" . $application->uuid;
|
|
|
|
|
|
|
|
$this->server->team->notify(new ContainerStopped($containerName, $this->server, $url));
|
|
|
|
}
|
|
|
|
$notRunningApplicationPreviews = $previews->pluck('id')->diff($foundApplicationPreviews);
|
|
|
|
foreach ($notRunningApplicationPreviews as $previewId) {
|
|
|
|
$preview = $previews->where('id', $previewId)->first();
|
|
|
|
if ($preview->status === 'exited') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$preview->update(['status' => 'exited']);
|
|
|
|
|
|
|
|
$name = data_get($preview, 'name');
|
|
|
|
$fqdn = data_get($preview, 'fqdn');
|
|
|
|
|
|
|
|
$containerName = $name ? "$name ($fqdn)" : $fqdn;
|
|
|
|
|
|
|
|
$project = data_get($preview, 'application.environment.project');
|
|
|
|
$environment = data_get($preview, 'application.environment');
|
|
|
|
|
|
|
|
$url = base_url() . '/project/' . $project->uuid . "/" . $environment->name . "/application/" . $preview->application->uuid;
|
|
|
|
$this->server->team->notify(new ContainerStopped($containerName, $this->server, $url));
|
|
|
|
}
|
|
|
|
$notRunningDatabases = $databases->pluck('id')->diff($foundDatabases);
|
2023-09-18 10:08:19 +00:00
|
|
|
foreach ($notRunningDatabases as $database) {
|
2023-09-14 13:52:04 +00:00
|
|
|
$database = $databases->where('id', $database)->first();
|
|
|
|
if ($database->status === 'exited') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$database->update(['status' => 'exited']);
|
|
|
|
|
2023-09-18 10:08:19 +00:00
|
|
|
$name = data_get($database, 'name');
|
2023-09-14 13:52:04 +00:00
|
|
|
$fqdn = data_get($database, 'fqdn');
|
|
|
|
|
|
|
|
$containerName = $name;
|
|
|
|
|
|
|
|
$project = data_get($database, 'environment.project');
|
|
|
|
$environment = data_get($database, 'environment');
|
|
|
|
|
|
|
|
$url = base_url() . '/project/' . $project->uuid . "/" . $environment->name . "/database/" . $database->uuid;
|
|
|
|
$this->server->team->notify(new ContainerStopped($containerName, $this->server, $url));
|
|
|
|
}
|
2023-09-14 10:45:50 +00:00
|
|
|
} catch (\Throwable $e) {
|
2023-09-24 15:51:01 +00:00
|
|
|
send_internal_notification('ContainerStatusJob failed with: ' . $e->getMessage());
|
2023-09-14 10:45:50 +00:00
|
|
|
ray($e->getMessage());
|
2023-10-09 09:00:18 +00:00
|
|
|
return handleError($e);
|
2023-09-14 10:45:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|