Refactor StartSentinel handle method to include latest version parameter
This commit is contained in:
		@@ -9,19 +9,15 @@ class StartSentinel
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
    use AsAction;
 | 
					    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()) {
 | 
					        if ($server->isSwarm() || $server->isBuildServer()) {
 | 
				
			||||||
            return;
 | 
					            return;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        if ($restart) {
 | 
					        if ($restart) {
 | 
				
			||||||
            StopSentinel::run($server);
 | 
					            StopSentinel::run($server);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					        $version = $latestVersion ?? get_latest_sentinel_version();
 | 
				
			||||||
        $metrics_history = data_get($server, 'settings.sentinel_metrics_history_days');
 | 
					        $metrics_history = data_get($server, 'settings.sentinel_metrics_history_days');
 | 
				
			||||||
        $refresh_rate = data_get($server, 'settings.sentinel_metrics_refresh_rate_seconds');
 | 
					        $refresh_rate = data_get($server, 'settings.sentinel_metrics_refresh_rate_seconds');
 | 
				
			||||||
        $push_interval = data_get($server, 'settings.sentinel_push_interval_seconds');
 | 
					        $push_interval = data_get($server, 'settings.sentinel_push_interval_seconds');
 | 
				
			||||||
@@ -42,8 +38,8 @@ class StartSentinel
 | 
				
			|||||||
        ];
 | 
					        ];
 | 
				
			||||||
        if (isDev()) {
 | 
					        if (isDev()) {
 | 
				
			||||||
            // data_set($environments, 'DEBUG', 'true');
 | 
					            // data_set($environments, 'DEBUG', 'true');
 | 
				
			||||||
            $mount_dir = '/var/lib/docker/volumes/coolify_dev_coolify_data/_data/sentinel';
 | 
					 | 
				
			||||||
            // $image = '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)).'"';
 | 
					        $docker_environments = '-e "'.implode('" -e "', array_map(fn ($key, $value) => "$key=$value", array_keys($environments), $environments)).'"';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										56
									
								
								app/Jobs/CheckAndStartSentinelJob.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										56
									
								
								app/Jobs/CheckAndStartSentinelJob.php
									
									
									
									
									
										Normal file
									
								
							@@ -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;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -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;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
@@ -5,7 +5,7 @@ namespace App\Models;
 | 
				
			|||||||
use App\Actions\Server\InstallDocker;
 | 
					use App\Actions\Server\InstallDocker;
 | 
				
			||||||
use App\Actions\Server\StartSentinel;
 | 
					use App\Actions\Server\StartSentinel;
 | 
				
			||||||
use App\Enums\ProxyTypes;
 | 
					use App\Enums\ProxyTypes;
 | 
				
			||||||
use App\Jobs\PullSentinelImageJob;
 | 
					use App\Jobs\CheckAndStartSentinelJob;
 | 
				
			||||||
use Illuminate\Database\Eloquent\Builder;
 | 
					use Illuminate\Database\Eloquent\Builder;
 | 
				
			||||||
use Illuminate\Database\Eloquent\Casts\Attribute;
 | 
					use Illuminate\Database\Eloquent\Casts\Attribute;
 | 
				
			||||||
use Illuminate\Database\Eloquent\SoftDeletes;
 | 
					use Illuminate\Database\Eloquent\SoftDeletes;
 | 
				
			||||||
@@ -577,18 +577,7 @@ $schema://$host {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    public function checkSentinel()
 | 
					    public function checkSentinel()
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        // ray("Checking sentinel on server: {$this->name}");
 | 
					        CheckAndStartSentinelJob::dispatch($this);
 | 
				
			||||||
        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');
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function getCpuMetrics(int $mins = 5)
 | 
					    public function getCpuMetrics(int $mins = 5)
 | 
				
			||||||
@@ -1269,10 +1258,14 @@ $schema://$host {
 | 
				
			|||||||
        return str($this->ip)->contains(':');
 | 
					        return str($this->ip)->contains(':');
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function restartSentinel()
 | 
					    public function restartSentinel(bool $async = true): void
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        try {
 | 
					        try {
 | 
				
			||||||
 | 
					            if ($async) {
 | 
				
			||||||
                StartSentinel::dispatch($this, true);
 | 
					                StartSentinel::dispatch($this, true);
 | 
				
			||||||
 | 
					            } else {
 | 
				
			||||||
 | 
					                StartSentinel::run($this, true);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
        } catch (\Throwable $e) {
 | 
					        } catch (\Throwable $e) {
 | 
				
			||||||
            loggy('Error restarting Sentinel: '.$e->getMessage());
 | 
					            loggy('Error restarting Sentinel: '.$e->getMessage());
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user