added defaults, remove duplicated cron validation

This commit is contained in:
ayntk-ai
2024-08-05 20:31:06 +02:00
parent 38976dac12
commit 4dfec6771c
4 changed files with 17 additions and 44 deletions

View File

@@ -45,14 +45,9 @@ class Kernel extends ConsoleKernel
$schedule->command('cleanup:unreachable-servers')->daily(); $schedule->command('cleanup:unreachable-servers')->daily();
$schedule->job(new PullTemplatesFromCDN)->daily()->onOneServer(); $schedule->job(new PullTemplatesFromCDN)->daily()->onOneServer();
$schedule->job(new CleanupInstanceStuffsJob)->everyFiveMinutes()->onOneServer(); $schedule->job(new CleanupInstanceStuffsJob)->everyFiveMinutes()->onOneServer();
$schedule->job(new PullCoolifyImageJob)->cron($settings->update_check_frequency ?? '0 0 * * *')->onOneServer();
if ($settings->update_check_frequency && $this->isValidCronExpression($settings->update_check_frequency)) { $schedule->job(new CheckForUpdatesJob())->cron($settings->auto_update_frequency ?? '0 11,23 * * *')->onOneServer();
$schedule->job(new PullCoolifyImageJob)->cron($settings->update_check_frequency)->onOneServer();
} else {
// Default to every 12 hours if not set or invalid
$schedule->job(new PullCoolifyImageJob)->twiceDaily()->onOneServer();
}
// Server Jobs // Server Jobs
$this->scheduleUpdates($schedule); $this->scheduleUpdates($schedule);
$this->pull_images($schedule); $this->pull_images($schedule);
@@ -81,32 +76,12 @@ class Kernel extends ConsoleKernel
{ {
$settings = InstanceSettings::get(); $settings = InstanceSettings::get();
// Schedule update check $updateCheckFrequency = $settings->update_check_frequency ?? '0 0 * * *'; // Default to daily at 00:00
if ($settings->update_check_frequency && $this->isValidCronExpression($settings->update_check_frequency)) { $schedule->job(new CheckForUpdatesJob())->cron($updateCheckFrequency)->onOneServer();
$schedule->job(new CheckForUpdatesJob())->cron($settings->update_check_frequency)->onOneServer();
} else {
// Default to every 12 hours if not set or invalid
$schedule->job(new CheckForUpdatesJob())->twiceDaily()->onOneServer();
}
// Schedule auto-update
if ($settings->is_auto_update_enabled) { if ($settings->is_auto_update_enabled) {
if ($settings->auto_update_frequency && $this->isValidCronExpression($settings->auto_update_frequency)) { $autoUpdateFrequency = $settings->auto_update_frequency ?? '0 11,23 * * *'; // Default to twice daily at 11:00 and 23:00
$schedule->job(new UpdateCoolifyJob())->cron($settings->auto_update_frequency)->onOneServer(); $schedule->job(new UpdateCoolifyJob())->cron($autoUpdateFrequency)->onOneServer();
} else {
// Default to every 24 hours if not set or invalid
$schedule->job(new UpdateCoolifyJob())->daily()->onOneServer();
}
}
}
private function isValidCronExpression($expression)
{
try {
new \Cron\CronExpression($expression);
return true;
} catch (\Exception $e) {
return false;
} }
} }

View File

@@ -21,9 +21,9 @@ class Configuration extends Component
public bool $is_api_enabled; public bool $is_api_enabled;
public ?string $auto_update_frequency; public string $auto_update_frequency;
public ?string $update_check_frequency; public string $update_check_frequency;
protected string $dynamic_config_path = '/data/coolify/proxy/dynamic'; protected string $dynamic_config_path = '/data/coolify/proxy/dynamic';
@@ -39,7 +39,7 @@ class Configuration extends Component
'settings.allowed_ips' => 'nullable', 'settings.allowed_ips' => 'nullable',
'settings.is_auto_update_enabled' => 'boolean', 'settings.is_auto_update_enabled' => 'boolean',
'auto_update_frequency' => 'nullable|string', 'auto_update_frequency' => 'nullable|string',
'update_check_frequency' => 'required|string', 'update_check_frequency' => 'nullable|string',
]; ];
protected $validationAttributes = [ protected $validationAttributes = [
@@ -91,6 +91,7 @@ class Configuration extends Component
} }
$this->validate(); $this->validate();
// Allow empty values and set defaults
if ($this->is_auto_update_enabled && !$this->validateCronExpression($this->auto_update_frequency)) { if ($this->is_auto_update_enabled && !$this->validateCronExpression($this->auto_update_frequency)) {
$this->dispatch('error', 'Invalid Cron / Human expression for Auto Update Frequency.'); $this->dispatch('error', 'Invalid Cron / Human expression for Auto Update Frequency.');
return; return;
@@ -131,7 +132,6 @@ class Configuration extends Component
$this->settings->is_api_enabled = $this->is_api_enabled; $this->settings->is_api_enabled = $this->is_api_enabled;
$this->settings->auto_update_frequency = $this->auto_update_frequency; $this->settings->auto_update_frequency = $this->auto_update_frequency;
$this->settings->update_check_frequency = $this->update_check_frequency; $this->settings->update_check_frequency = $this->update_check_frequency;
$this->settings->save(); $this->settings->save();
$this->server->setupDynamicProxyConfiguration(); $this->server->setupDynamicProxyConfiguration();
if (! $error_show) { if (! $error_show) {
@@ -145,7 +145,7 @@ class Configuration extends Component
private function validateCronExpression($expression): bool private function validateCronExpression($expression): bool
{ {
if (empty($expression)) { if (empty($expression)) {
return false; return true;
} }
$isValid = false; $isValid = false;
try { try {
@@ -165,14 +165,14 @@ class Configuration extends Component
public function updatedAutoUpdateFrequency() public function updatedAutoUpdateFrequency()
{ {
if (!$this->validateCronExpression($this->auto_update_frequency)) { if (!$this->validateCronExpression($this->auto_update_frequency)) {
$this->dispatch('error', 'Invalid Cron / Human expression.'); $this->dispatch('error', 'Invalid Cron / Human expression for Auto Update Frequency.');
} }
} }
public function updatedUpdateCheckFrequency() public function updatedUpdateCheckFrequency()
{ {
if (!$this->validateCronExpression($this->update_check_frequency)) { if (!$this->validateCronExpression($this->update_check_frequency)) {
$this->dispatch('error', 'Invalid Cron / Human expression.'); $this->dispatch('error', 'Invalid Cron / Human expression for Update Check Frequency.');
} }
} }
} }

View File

@@ -12,8 +12,8 @@ return new class extends Migration
public function up(): void public function up(): void
{ {
Schema::table('instance_settings', function (Blueprint $table) { Schema::table('instance_settings', function (Blueprint $table) {
$table->string('update_check_frequency')->default('0 */12 * * *')->nullable();
$table->string('auto_update_frequency')->default('0 0 * * *')->nullable(); $table->string('auto_update_frequency')->default('0 0 * * *')->nullable();
$table->string('update_check_frequency')->default('0 */11 * * *')->nullable();
}); });
} }

View File

@@ -45,11 +45,9 @@
@else @else
<x-forms.checkbox instantSave id="is_auto_update_enabled" label="Auto Update Coolify" /> <x-forms.checkbox instantSave id="is_auto_update_enabled" label="Auto Update Coolify" />
@if($is_auto_update_enabled) @if($is_auto_update_enabled)
<x-forms.input id="auto_update_frequency" label="Auto Update Frequency" placeholder="0 0 * * *" helper="Cron expression for auto update frequency" /> <x-forms.input id="auto_update_frequency" label="Auto Update Frequency" placeholder="0 0 * * *" helper="Cron expression for auto update frequency. Default is every day at 00:00" />
@error('settings.auto_update_frequency') <span class="text-error">{{ $message }}</span> @enderror
@endif @endif
<x-forms.input id="update_check_frequency" label="Update Check Frequency" placeholder="0 */12 * * *" helper="Cron expression for update check frequency" /> <x-forms.input id="update_check_frequency" label="Update Check Frequency" placeholder="0 */11 * * *" helper="Cron expression for update check frequency. Default is every 12 hours at 11:00" />
@error('settings.update_check_frequency') <span class="text-error">{{ $message }}</span> @enderror
@endif @endif
<x-forms.checkbox instantSave id="is_registration_enabled" label="Registration Allowed" /> <x-forms.checkbox instantSave id="is_registration_enabled" label="Registration Allowed" />
<x-forms.checkbox instantSave id="do_not_track" label="Do Not Track" /> <x-forms.checkbox instantSave id="do_not_track" label="Do Not Track" />