refactor: update check frequency logic for cloud and self-hosted environments; streamline server task scheduling and timezone handling

This commit is contained in:
Andras Bacsai
2025-08-26 13:51:42 +02:00
parent 63fcc0ebc3
commit 13397b62cf

View File

@@ -28,6 +28,8 @@ class ServerManagerJob implements ShouldQueue
private string $instanceTimezone; private string $instanceTimezone;
private string $checkFrequency = '* * * * *';
/** /**
* Create a new job instance. * Create a new job instance.
*/ */
@@ -40,7 +42,9 @@ class ServerManagerJob implements ShouldQueue
{ {
// Freeze the execution time at the start of the job // Freeze the execution time at the start of the job
$this->executionTime = Carbon::now(); $this->executionTime = Carbon::now();
if (isCloud()) {
$this->checkFrequency = '*/5 * * * *';
}
$this->settings = instanceSettings(); $this->settings = instanceSettings();
$this->instanceTimezone = $this->settings->instance_timezone ?: config('app.timezone'); $this->instanceTimezone = $this->settings->instance_timezone ?: config('app.timezone');
@@ -75,10 +79,7 @@ class ServerManagerJob implements ShouldQueue
private function dispatchConnectionChecks(Collection $servers): void private function dispatchConnectionChecks(Collection $servers): void
{ {
$checkFrequency = isCloud() ? '*/2 * * * *' : '* * * * *'; // Every 2 min for cloud, every minute for self-hosted if ($this->shouldRunNow($this->checkFrequency)) {
$cron = new CronExpression($checkFrequency);
if ($cron->isDue($this->executionTime)) {
$servers->each(function (Server $server) { $servers->each(function (Server $server) {
try { try {
ServerConnectionCheckJob::dispatch($server); ServerConnectionCheckJob::dispatch($server);
@@ -110,11 +111,6 @@ class ServerManagerJob implements ShouldQueue
private function processServerTasks(Server $server): void private function processServerTasks(Server $server): void
{ {
$serverTimezone = data_get($server->settings, 'server_timezone', $this->instanceTimezone);
if (validate_timezone($serverTimezone) === false) {
$serverTimezone = config('app.timezone');
}
// Check if we should run sentinel-based checks // Check if we should run sentinel-based checks
$lastSentinelUpdate = $server->sentinel_updated_at; $lastSentinelUpdate = $server->sentinel_updated_at;
$waitTime = $server->waitBeforeDoingSshCheck(); $waitTime = $server->waitBeforeDoingSshCheck();
@@ -122,10 +118,7 @@ class ServerManagerJob implements ShouldQueue
if ($sentinelOutOfSync) { if ($sentinelOutOfSync) {
// Dispatch jobs if Sentinel is out of sync // Dispatch jobs if Sentinel is out of sync
$checkFrequency = isCloud() ? '*/5 * * * *' : '* * * * *'; // Every 5 min for cloud, every minute for self-hosted if ($this->shouldRunNow($this->checkFrequency)) {
$cron = new CronExpression($checkFrequency);
if ($cron->isDue($this->executionTime)) {
ServerCheckJob::dispatch($server); ServerCheckJob::dispatch($server);
} }
@@ -134,13 +127,17 @@ class ServerManagerJob implements ShouldQueue
if (isset(VALID_CRON_STRINGS[$serverDiskUsageCheckFrequency])) { if (isset(VALID_CRON_STRINGS[$serverDiskUsageCheckFrequency])) {
$serverDiskUsageCheckFrequency = VALID_CRON_STRINGS[$serverDiskUsageCheckFrequency]; $serverDiskUsageCheckFrequency = VALID_CRON_STRINGS[$serverDiskUsageCheckFrequency];
} }
$shouldRunStorageCheck = $this->shouldRunNow($serverDiskUsageCheckFrequency, $serverTimezone); $shouldRunStorageCheck = $this->shouldRunNow($serverDiskUsageCheckFrequency);
if ($shouldRunStorageCheck) { if ($shouldRunStorageCheck) {
ServerStorageCheckJob::dispatch($server); ServerStorageCheckJob::dispatch($server);
} }
} }
$serverTimezone = data_get($server->settings, 'server_timezone', $this->instanceTimezone);
if (validate_timezone($serverTimezone) === false) {
$serverTimezone = config('app.timezone');
}
// Dispatch DockerCleanupJob if due // Dispatch DockerCleanupJob if due
$dockerCleanupFrequency = data_get($server->settings, 'docker_cleanup_frequency', '0 * * * *'); $dockerCleanupFrequency = data_get($server->settings, 'docker_cleanup_frequency', '0 * * * *');
if (isset(VALID_CRON_STRINGS[$dockerCleanupFrequency])) { if (isset(VALID_CRON_STRINGS[$dockerCleanupFrequency])) {
@@ -170,13 +167,13 @@ class ServerManagerJob implements ShouldQueue
} }
} }
private function shouldRunNow(string $frequency, string $timezone): bool private function shouldRunNow(string $frequency, ?string $timezone = null): bool
{ {
$cron = new CronExpression($frequency); $cron = new CronExpression($frequency);
// Use the frozen execution time, not the current time // Use the frozen execution time, not the current time
$baseTime = $this->executionTime ?? Carbon::now(); $baseTime = $this->executionTime ?? Carbon::now();
$executionTime = $baseTime->copy()->setTimezone($timezone); $executionTime = $baseTime->copy()->setTimezone($timezone ?? config('app.timezone'));
return $cron->isDue($executionTime); return $cron->isDue($executionTime);
} }