coolify/app/Jobs/PushServerUpdateJob.php

373 lines
15 KiB
PHP
Raw Normal View History

2024-10-14 10:07:37 +00:00
<?php
namespace App\Jobs;
2024-10-14 15:54:29 +00:00
use App\Actions\Database\StartDatabaseProxy;
use App\Actions\Database\StopDatabaseProxy;
2024-10-15 11:39:19 +00:00
use App\Actions\Proxy\CheckProxy;
2024-10-14 11:32:36 +00:00
use App\Actions\Proxy\StartProxy;
2024-10-30 13:54:27 +00:00
use App\Actions\Server\StartLogDrain;
2024-10-14 15:54:29 +00:00
use App\Actions\Shared\ComplexStatusCheck;
2024-10-14 11:32:36 +00:00
use App\Models\Application;
use App\Models\ApplicationPreview;
2024-10-14 10:07:37 +00:00
use App\Models\Server;
2024-10-14 15:54:29 +00:00
use App\Models\ServiceApplication;
use App\Models\ServiceDatabase;
use App\Notifications\Container\ContainerRestarted;
2024-10-14 10:07:37 +00:00
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeEncrypted;
2024-10-14 10:07:37 +00:00
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\Middleware\WithoutOverlapping;
2024-10-14 10:07:37 +00:00
use Illuminate\Queue\SerializesModels;
2024-10-14 11:32:36 +00:00
use Illuminate\Support\Collection;
2024-10-14 10:07:37 +00:00
class PushServerUpdateJob implements ShouldBeEncrypted, ShouldQueue
2024-10-14 10:07:37 +00:00
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $tries = 1;
2024-10-14 15:54:29 +00:00
public $timeout = 30;
public Collection $containers;
public Collection $applications;
public Collection $previews;
public Collection $databases;
public Collection $services;
2024-10-14 15:54:29 +00:00
public Collection $allApplicationIds;
public Collection $allDatabaseUuids;
2024-10-15 11:39:19 +00:00
public Collection $allTcpProxyUuids;
2024-10-14 15:54:29 +00:00
public Collection $allServiceApplicationIds;
public Collection $allApplicationPreviewsIds;
public Collection $allServiceDatabaseIds;
public Collection $allApplicationsWithAdditionalServers;
public Collection $foundApplicationIds;
public Collection $foundDatabaseUuids;
public Collection $foundServiceApplicationIds;
public Collection $foundServiceDatabaseIds;
public Collection $foundApplicationPreviewsIds;
public bool $foundProxy = false;
2024-10-15 11:39:19 +00:00
public bool $foundLogDrainContainer = false;
2024-10-14 10:07:37 +00:00
public function middleware(): array
{
v4.0.0-beta.417 (#5784) * feat(README): add InterviewPal sponsorship link and corresponding SVG icon * chore(versions): update coolify version to 4.0.0-beta.413 and nightly version to 4.0.0-beta.414 in configuration files * fix(terminal): enhance WebSocket client verification with authorized IPs in terminal server * chore(versions): update realtime version to 1.0.8 in versions.json * chore(versions): update realtime version to 1.0.8 in versions.json * chore(docker): update soketi image version to 1.0.8 in production configuration files * chore(versions): update coolify version to 4.0.0-beta.414 and nightly version to 4.0.0-beta.415 in configuration files * fix(ApplicationDeploymentJob): ensure source is an object before checking GitHub app properties * fix(ui): Disable livewire navigate feature (causing spam of setInterval()) * fix(ui): Remove required attribute from image input in service application view * fix(ui): Change application image validation to be nullable in service application view * fix(Server): Correct proxy path formatting for Traefik proxy type * chore(versions): update coolify version to 4.0.0-beta.416 and nightly version to 4.0.0-beta.417 in configuration files; fix links in deployment view * feat(Service): Add functionality to convert between applications and databases in docker-compose based applications fix(ui): Fix service layout refresh on compose change * fix(service): graceful shutdown of old container (#5731) * refactor(Database): streamline container shutdown process and reduce timeout duration * fix(ServerCheck): enhance proxy container check to ensure it is running before proceeding * chore(seeder): update git branch from 'main' to 'v4.x' for multiple examples in ApplicationSeeder * fix(applications): include pull_request_id in deployment queue check to prevent duplicate deployments * refactor(core): streamline container stopping process and reduce timeout duration; update related methods for consistency * fix(database): update label for image input field to improve clarity * feat(migration): add 'is_migrated' and 'custom_type' columns to service_applications and service_databases tables * feat(backup): implement custom database type selection and enhance scheduled backups management * fix(ServerCheck): set default proxy status to 'exited' to handle missing container state * fix(database): reduce container stop timeout from 300 to 30 seconds for improved responsiveness * refactor(database): update DB facade usage for consistency across service files * Update app/Livewire/Project/Service/Database.php Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * refactor(database): enhance application conversion logic and add existence checks for databases and applications * refactor(actions): standardize method naming for network and configuration deletion across application and service classes * refactor(logdrain): consolidate log drain stopping logic to reduce redundancy * refactor(StandaloneMariadb): add type hint for destination method to improve code clarity * refactor(DeleteResourceJob): streamline resource deletion logic and improve conditional checks for database types * refactor(jobs): update middleware to prevent job release after expiration for CleanupInstanceStuffsJob, RestartProxyJob, and ServerCheckJob * fix(ui): system theming for charts (#5740) * chore(deps-dev): bump vite from 6.2.6 to 6.3.4 (#5743) Bumps [vite](https://github.com/vitejs/vite/tree/HEAD/packages/vite) from 6.2.6 to 6.3.4. - [Release notes](https://github.com/vitejs/vite/releases) - [Changelog](https://github.com/vitejs/vite/blob/main/packages/vite/CHANGELOG.md) - [Commits](https://github.com/vitejs/vite/commits/v6.3.4/packages/vite) --- updated-dependencies: - dependency-name: vite dependency-version: 6.3.4 dependency-type: direct:development ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * fix(dev): mount points?! * fix(dev): proxy mount point * fix(ui): allow adding scheduled backups for non-migrated databases * fix(DatabaseBackupJob): escape PostgreSQL password in backup command (#5759) * fix(ui): correct closing div tag in service index view * Revert "fix(dev): mount points?!" This reverts commit 365bf3cbf0e5aa21a2ccb12fe6ec333b93e404da. * feat(README): add Gozunga and Macarne to sponsors list * refactor(jobs): unify middleware configuration to prevent job release after expiration for DockerCleanupJob and PushServerUpdateJob * chore(versions): update coolify version to 4.0.0-beta.417 and nightly version to 4.0.0-beta.418 * feat(redis): add scheduled cleanup command for Redis keys and enhance cleanup logic --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: Jérémy <jeremy.derdaele@gmail.com> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: Best Codes <106822363+The-Best-Codes@users.noreply.github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: busybox <29630035+busybox11@users.noreply.github.com>
2025-05-07 21:11:45 +00:00
return [(new WithoutOverlapping($this->server->uuid))->dontRelease()];
}
2024-10-14 10:07:37 +00:00
public function backoff(): int
{
return isDev() ? 1 : 3;
}
2024-10-14 11:32:36 +00:00
public function __construct(public Server $server, public $data)
{
2024-10-14 15:54:29 +00:00
$this->containers = collect();
$this->foundApplicationIds = collect();
$this->foundDatabaseUuids = collect();
$this->foundServiceApplicationIds = collect();
$this->foundApplicationPreviewsIds = collect();
$this->foundServiceDatabaseIds = collect();
$this->allApplicationIds = collect();
$this->allDatabaseUuids = collect();
$this->allTcpProxyUuids = collect();
$this->allServiceApplicationIds = collect();
$this->allServiceDatabaseIds = collect();
2024-10-14 11:32:36 +00:00
}
2024-10-14 10:07:37 +00:00
public function handle()
{
2024-10-22 09:29:43 +00:00
// TODO: Swarm is not supported yet
2024-10-28 13:37:00 +00:00
if (! $this->data) {
throw new \Exception('No data provided');
}
$data = collect($this->data);
2024-10-15 11:39:19 +00:00
2024-10-28 13:37:00 +00:00
$this->server->sentinelHeartbeat();
2024-10-15 11:39:19 +00:00
2024-10-28 13:37:00 +00:00
$this->containers = collect(data_get($data, 'containers'));
2024-10-28 13:37:00 +00:00
$filesystemUsageRoot = data_get($data, 'filesystem_usage_root.used_percentage');
ServerStorageCheckJob::dispatch($this->server, $filesystemUsageRoot);
2024-10-28 13:37:00 +00:00
if ($this->containers->isEmpty()) {
return;
}
$this->applications = $this->server->applications();
$this->databases = $this->server->databases();
$this->previews = $this->server->previews();
$this->services = $this->server->services()->get();
$this->allApplicationIds = $this->applications->filter(function ($application) {
return $application->additional_servers->count() === 0;
})->pluck('id');
$this->allApplicationsWithAdditionalServers = $this->applications->filter(function ($application) {
return $application->additional_servers->count() > 0;
});
$this->allApplicationPreviewsIds = $this->previews->pluck('id');
$this->allDatabaseUuids = $this->databases->pluck('uuid');
$this->allTcpProxyUuids = $this->databases->where('is_public', true)->pluck('uuid');
$this->services->each(function ($service) {
$service->applications()->pluck('id')->each(function ($applicationId) {
$this->allServiceApplicationIds->push($applicationId);
});
2024-10-28 13:37:00 +00:00
$service->databases()->pluck('id')->each(function ($databaseId) {
$this->allServiceDatabaseIds->push($databaseId);
2024-10-14 15:54:29 +00:00
});
2024-10-28 13:37:00 +00:00
});
foreach ($this->containers as $container) {
$containerStatus = data_get($container, 'state', 'exited');
$containerHealth = data_get($container, 'health_status', 'unhealthy');
$containerStatus = "$containerStatus ($containerHealth)";
$labels = collect(data_get($container, 'labels'));
$coolify_managed = $labels->has('coolify.managed');
if ($coolify_managed) {
$name = data_get($container, 'name');
if ($name === 'coolify-log-drain' && $this->isRunning($containerStatus)) {
$this->foundLogDrainContainer = true;
}
if ($labels->has('coolify.applicationId')) {
$applicationId = $labels->get('coolify.applicationId');
$pullRequestId = data_get($labels, 'coolify.pullRequestId', '0');
try {
if ($pullRequestId === '0') {
if ($this->allApplicationIds->contains($applicationId) && $this->isRunning($containerStatus)) {
$this->foundApplicationIds->push($applicationId);
2024-10-14 15:54:29 +00:00
}
2024-10-28 13:37:00 +00:00
$this->updateApplicationStatus($applicationId, $containerStatus);
} else {
if ($this->allApplicationPreviewsIds->contains($applicationId) && $this->isRunning($containerStatus)) {
$this->foundApplicationPreviewsIds->push($applicationId);
}
$this->updateApplicationPreviewStatus($applicationId, $containerStatus);
2024-10-14 16:04:36 +00:00
}
2024-10-28 13:37:00 +00:00
} catch (\Exception $e) {
}
} elseif ($labels->has('coolify.serviceId')) {
$serviceId = $labels->get('coolify.serviceId');
$subType = $labels->get('coolify.service.subType');
$subId = $labels->get('coolify.service.subId');
if ($subType === 'application' && $this->isRunning($containerStatus)) {
$this->foundServiceApplicationIds->push($subId);
$this->updateServiceSubStatus($serviceId, $subType, $subId, $containerStatus);
} elseif ($subType === 'database' && $this->isRunning($containerStatus)) {
$this->foundServiceDatabaseIds->push($subId);
$this->updateServiceSubStatus($serviceId, $subType, $subId, $containerStatus);
}
} else {
$uuid = $labels->get('com.docker.compose.service');
$type = $labels->get('coolify.type');
if ($name === 'coolify-proxy' && $this->isRunning($containerStatus)) {
$this->foundProxy = true;
} elseif ($type === 'service' && $this->isRunning($containerStatus)) {
2024-10-14 11:32:36 +00:00
} else {
2024-10-28 13:37:00 +00:00
if ($this->allDatabaseUuids->contains($uuid) && $this->isRunning($containerStatus)) {
$this->foundDatabaseUuids->push($uuid);
if ($this->allTcpProxyUuids->contains($uuid) && $this->isRunning($containerStatus)) {
$this->updateDatabaseStatus($uuid, $containerStatus, tcpProxy: true);
} else {
$this->updateDatabaseStatus($uuid, $containerStatus, tcpProxy: false);
2024-10-14 15:54:29 +00:00
}
}
2024-10-14 11:32:36 +00:00
}
}
2024-10-14 10:07:37 +00:00
}
2024-10-28 13:37:00 +00:00
}
2024-10-14 11:32:36 +00:00
2024-10-28 13:37:00 +00:00
$this->updateProxyStatus();
2024-10-15 11:39:19 +00:00
2024-10-28 13:37:00 +00:00
$this->updateNotFoundApplicationStatus();
$this->updateNotFoundApplicationPreviewStatus();
$this->updateNotFoundDatabaseStatus();
$this->updateNotFoundServiceStatus();
2024-10-15 11:39:19 +00:00
2024-10-28 13:37:00 +00:00
$this->updateAdditionalServersStatus();
2024-10-14 15:54:29 +00:00
2024-10-28 13:37:00 +00:00
$this->checkLogDrainContainer();
2024-10-14 15:54:29 +00:00
}
private function updateApplicationStatus(string $applicationId, string $containerStatus)
{
$application = $this->applications->where('id', $applicationId)->first();
2024-10-14 15:54:29 +00:00
if (! $application) {
return;
}
$application->status = $containerStatus;
$application->save();
}
private function updateApplicationPreviewStatus(string $applicationId, string $containerStatus)
{
$application = $this->previews->where('id', $applicationId)->first();
2024-10-14 15:54:29 +00:00
if (! $application) {
return;
2024-10-14 11:32:36 +00:00
}
2024-10-14 15:54:29 +00:00
$application->status = $containerStatus;
$application->save();
}
2024-10-14 11:32:36 +00:00
2024-10-14 15:54:29 +00:00
private function updateNotFoundApplicationStatus()
{
$notFoundApplicationIds = $this->allApplicationIds->diff($this->foundApplicationIds);
2024-10-14 11:32:36 +00:00
if ($notFoundApplicationIds->isNotEmpty()) {
2024-10-14 15:54:29 +00:00
$notFoundApplicationIds->each(function ($applicationId) {
$application = Application::find($applicationId);
if ($application) {
$application->status = 'exited';
$application->save();
}
});
}
}
private function updateNotFoundApplicationPreviewStatus()
{
$notFoundApplicationPreviewsIds = $this->allApplicationPreviewsIds->diff($this->foundApplicationPreviewsIds);
if ($notFoundApplicationPreviewsIds->isNotEmpty()) {
$notFoundApplicationPreviewsIds->each(function ($applicationPreviewId) {
$applicationPreview = ApplicationPreview::find($applicationPreviewId);
if ($applicationPreview) {
$applicationPreview->status = 'exited';
$applicationPreview->save();
}
});
}
}
private function updateProxyStatus()
{
// If proxy is not found, start it
2024-10-15 11:39:19 +00:00
if ($this->server->isProxyShouldRun()) {
if ($this->foundProxy === false) {
try {
if (CheckProxy::run($this->server)) {
StartProxy::run($this->server, false);
$this->server->team?->notify(new ContainerRestarted('coolify-proxy', $this->server));
2024-10-15 11:39:19 +00:00
}
} catch (\Throwable $e) {
}
} else {
$connectProxyToDockerNetworks = connectProxyToNetworks($this->server);
instant_remote_process($connectProxyToDockerNetworks, $this->server, false);
}
2024-10-14 10:07:37 +00:00
}
}
2024-10-14 15:54:29 +00:00
private function updateDatabaseStatus(string $databaseUuid, string $containerStatus, bool $tcpProxy = false)
2024-10-14 11:32:36 +00:00
{
$database = $this->databases->where('uuid', $databaseUuid)->first();
2024-10-14 15:54:29 +00:00
if (! $database) {
return;
}
$database->status = $containerStatus;
$database->save();
if ($this->isRunning($containerStatus) && $tcpProxy) {
2024-10-14 15:54:29 +00:00
$tcpProxyContainerFound = $this->containers->filter(function ($value, $key) use ($databaseUuid) {
2024-10-14 16:04:36 +00:00
return data_get($value, 'name') === "$databaseUuid-proxy" && data_get($value, 'state') === 'running';
2024-10-14 15:54:29 +00:00
})->first();
if (! $tcpProxyContainerFound) {
StartDatabaseProxy::dispatch($database);
$this->server->team?->notify(new ContainerRestarted("TCP Proxy for {$database->name}", $this->server));
2024-10-14 15:54:29 +00:00
} else {
2024-10-14 11:32:36 +00:00
}
2024-10-14 15:54:29 +00:00
}
}
private function updateNotFoundDatabaseStatus()
{
$notFoundDatabaseUuids = $this->allDatabaseUuids->diff($this->foundDatabaseUuids);
if ($notFoundDatabaseUuids->isNotEmpty()) {
$notFoundDatabaseUuids->each(function ($databaseUuid) {
$database = $this->databases->where('uuid', $databaseUuid)->first();
2024-10-14 15:54:29 +00:00
if ($database) {
$database->status = 'exited';
$database->save();
if ($database->is_public) {
StopDatabaseProxy::dispatch($database);
}
2024-10-14 15:54:29 +00:00
}
});
}
}
private function updateServiceSubStatus(string $serviceId, string $subType, string $subId, string $containerStatus)
{
$service = $this->services->where('id', $serviceId)->first();
2024-10-14 15:54:29 +00:00
if (! $service) {
return;
}
if ($subType === 'application') {
$application = $service->applications()->where('id', $subId)->first();
2024-10-14 11:32:36 +00:00
$application->status = $containerStatus;
$application->save();
2024-10-14 15:54:29 +00:00
} elseif ($subType === 'database') {
$database = $service->databases()->where('id', $subId)->first();
$database->status = $containerStatus;
$database->save();
2024-10-14 11:32:36 +00:00
} else {
}
}
2024-10-14 10:07:37 +00:00
2024-10-14 15:54:29 +00:00
private function updateNotFoundServiceStatus()
2024-10-14 11:32:36 +00:00
{
2024-10-14 15:54:29 +00:00
$notFoundServiceApplicationIds = $this->allServiceApplicationIds->diff($this->foundServiceApplicationIds);
$notFoundServiceDatabaseIds = $this->allServiceDatabaseIds->diff($this->foundServiceDatabaseIds);
if ($notFoundServiceApplicationIds->isNotEmpty()) {
$notFoundServiceApplicationIds->each(function ($serviceApplicationId) {
$application = ServiceApplication::find($serviceApplicationId);
if ($application) {
$application->status = 'exited';
$application->save();
}
});
}
if ($notFoundServiceDatabaseIds->isNotEmpty()) {
$notFoundServiceDatabaseIds->each(function ($serviceDatabaseId) {
$database = ServiceDatabase::find($serviceDatabaseId);
if ($database) {
$database->status = 'exited';
$database->save();
}
});
}
}
private function updateAdditionalServersStatus()
{
$this->allApplicationsWithAdditionalServers->each(function ($application) {
ComplexStatusCheck::run($application);
2024-10-14 11:32:36 +00:00
});
}
private function isRunning(string $containerStatus)
{
return str($containerStatus)->contains('running');
}
2024-10-15 11:39:19 +00:00
private function checkLogDrainContainer()
{
2024-10-15 11:39:19 +00:00
if ($this->server->isLogDrainEnabled() && $this->foundLogDrainContainer === false) {
StartLogDrain::dispatch($this->server);
2024-10-15 11:39:19 +00:00
}
}
2024-10-14 10:07:37 +00:00
}