Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

Refactor StartSentinel handle method to include latest version parameter

Andras Bacsai2bea26584a3e
diff --git a/app/Actions/Server/StartSentinel.php b/app/Actions/Server/StartSentinel.php
index a8ef8b1fe4..be57590a06 100644
--- a/app/Actions/Server/StartSentinel.php
+++ b/app/Actions/Server/StartSentinel.php
@@ -9,19 +9,15 @@ class StartSentinel
{
use AsAction;
- public function handle(Server $server, bool $restart = false)
+ public function handle(Server $server, bool $restart = false, ?string $latestVersion = null)
{
- // TODO: Sentinel is not available in this version (soon).
- if (! isExperimentalFeaturesEnabled()) {
- return;
- }
- $version = get_latest_sentinel_version();
if ($server->isSwarm() || $server->isBuildServer()) {
return;
}
if ($restart) {
StopSentinel::run($server);
}
+ $version = $latestVersion ?? get_latest_sentinel_version();
$metrics_history = data_get($server, 'settings.sentinel_metrics_history_days');
$refresh_rate = data_get($server, 'settings.sentinel_metrics_refresh_rate_seconds');
$push_interval = data_get($server, 'settings.sentinel_push_interval_seconds');
@@ -42,8 +38,8 @@ class StartSentinel
];
if (isDev()) {
// data_set($environments, 'DEBUG', 'true');
- $mount_dir = '/var/lib/docker/volumes/coolify_dev_coolify_data/_data/sentinel';
// $image = 'sentinel';
+ $mount_dir = '/var/lib/docker/volumes/coolify_dev_coolify_data/_data/sentinel';
}
$docker_environments = '-e "'.implode('" -e "', array_map(fn ($key, $value) => "$key=$value", array_keys($environments), $environments)).'"';
diff --git a/app/Jobs/CheckAndStartSentinelJob.php b/app/Jobs/CheckAndStartSentinelJob.php
new file mode 100644
index 0000000000..8fbeee6637
--- /dev/null
+++ b/app/Jobs/CheckAndStartSentinelJob.php
@@ -0,0 +1,56 @@
+<?php
+
+namespace App\Jobs;
+
+use App\Actions\Server\StartSentinel;
+use App\Models\Server;
+use Illuminate\Bus\Queueable;
+use Illuminate\Contracts\Queue\ShouldBeEncrypted;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Foundation\Bus\Dispatchable;
+use Illuminate\Queue\InteractsWithQueue;
+use Illuminate\Queue\SerializesModels;
+
+class CheckAndStartSentinelJob implements ShouldBeEncrypted, ShouldQueue
+{
+ use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
+
+ public $timeout = 120;
+
+ public function __construct(public Server $server) {}
+
+ public function handle(): void
+ {
+ try {
+ $latestVersion = get_latest_sentinel_version();
+
+ // Check if sentinel is running
+ $sentinelFound = instant_remote_process(['docker inspect coolify-sentinel'], $this->server, false);
+ $sentinelFoundJson = json_decode($sentinelFound, true);
+ $sentinelStatus = data_get($sentinelFoundJson, '0.State.Status', 'exited');
+ if ($sentinelStatus !== 'running') {
+ StartSentinel::run(server: $this->server, restart: true, latestVersion: $latestVersion);
+
+ return;
+ }
+ // If sentinel is running, check if it needs an update
+ $runningVersion = instant_remote_process(['docker exec coolify-sentinel sh -c "curl http://127.0.0.1:8888/api/version"'], $this->server, false);
+ if (empty($runningVersion)) {
+ $runningVersion = '0.0.0';
+ }
+ if ($latestVersion === '0.0.0' && $runningVersion === '0.0.0') {
+ StartSentinel::run(server: $this->server, restart: true, latestVersion: 'latest');
+
+ return;
+ } else {
+ if (version_compare($runningVersion, $latestVersion, '<')) {
+ StartSentinel::run(server: $this->server, restart: true, latestVersion: $latestVersion);
+
+ return;
+ }
+ }
+ } catch (\Throwable $e) {
+ throw $e;
+ }
+ }
+}
diff --git a/app/Jobs/PullSentinelImageJob.php b/app/Jobs/PullSentinelImageJob.php
deleted file mode 100644
index 054f81d998..0000000000
--- a/app/Jobs/PullSentinelImageJob.php
+++ /dev/null
@@ -1,47 +0,0 @@
-<?php
-
-namespace App\Jobs;
-
-use App\Actions\Server\StartSentinel;
-use App\Models\Server;
-use Illuminate\Bus\Queueable;
-use Illuminate\Contracts\Queue\ShouldBeEncrypted;
-use Illuminate\Contracts\Queue\ShouldQueue;
-use Illuminate\Foundation\Bus\Dispatchable;
-use Illuminate\Queue\InteractsWithQueue;
-use Illuminate\Queue\SerializesModels;
-
-class PullSentinelImageJob implements ShouldBeEncrypted, ShouldQueue
-{
- use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
-
- public $timeout = 1000;
-
- public function __construct(public Server $server) {}
-
- public function handle(): void
- {
- try {
- $version = get_latest_sentinel_version();
- if (! $version) {
- ray('Failed to get latest Sentinel version');
-
- return;
- }
- $local_version = instant_remote_process(['docker exec coolify-sentinel sh -c "curl http://127.0.0.1:8888/api/version"'], $this->server, false);
- if (empty($local_version)) {
- $local_version = '0.0.0';
- }
- if (version_compare($local_version, $version, '<')) {
- StartSentinel::run($this->server, true);
-
- return;
- }
- ray('Sentinel image is up to date');
- } catch (\Throwable $e) {
- // send_internal_notification('PullSentinelImageJob failed with: '.$e->getMessage());
- ray($e->getMessage());
- throw $e;
- }
- }
-}
diff --git a/app/Models/Server.php b/app/Models/Server.php
index 1c66920418..bd8177dd0e 100644
--- a/app/Models/Server.php
+++ b/app/Models/Server.php
@@ -5,7 +5,7 @@ namespace App\Models;
use App\Actions\Server\InstallDocker;
use App\Actions\Server\StartSentinel;
use App\Enums\ProxyTypes;
-use App\Jobs\PullSentinelImageJob;
+use App\Jobs\CheckAndStartSentinelJob;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\SoftDeletes;
@@ -577,18 +577,7 @@ $schema://$host {
public function checkSentinel()
{
- // ray("Checking sentinel on server: {$this->name}");
- if ($this->isSentinelEnabled()) {
- $sentinel_found = instant_remote_process(['docker inspect coolify-sentinel'], $this, false);
- $sentinel_found = json_decode($sentinel_found, true);
- $status = data_get($sentinel_found, '0.State.Status', 'exited');
- if ($status !== 'running') {
- // ray('Sentinel is not running, starting it...');
- PullSentinelImageJob::dispatch($this);
- } else {
- // ray('Sentinel is running');
- }
- }
+ CheckAndStartSentinelJob::dispatch($this);
}
public function getCpuMetrics(int $mins = 5)
@@ -1269,10 +1258,14 @@ $schema://$host {
return str($this->ip)->contains(':');
}
- public function restartSentinel()
+ public function restartSentinel(bool $async = true): void
{
try {
- StartSentinel::dispatch($this, true);
+ if ($async) {
+ StartSentinel::dispatch($this, true);
+ } else {
+ StartSentinel::run($this, true);
+ }
} catch (\Throwable $e) {
loggy('Error restarting Sentinel: '.$e->getMessage());
}