You've already forked Atomcms-edit
Initial commit
This commit is contained in:
Executable
+150
@@ -0,0 +1,150 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Enums\AlertChannel;
|
||||
use App\Enums\AlertType;
|
||||
use App\Services\AlertService;
|
||||
use App\Services\EmulatorUpdateService;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class AutoUpdateCommand extends Command
|
||||
{
|
||||
#[\Override]
|
||||
protected $signature = 'update:auto
|
||||
{--force : Forceer update ook al is het niet de geplande tijd}
|
||||
{--sql-only : Alleen SQL updates draaien}
|
||||
{--emu-only : Alleen emulator updates draaien}';
|
||||
|
||||
#[\Override]
|
||||
protected $description = 'Automatische emulator en SQL updates uitvoeren';
|
||||
|
||||
private const string CACHE_KEY_LAST_EMU_UPDATE = 'auto_update_last_emu';
|
||||
|
||||
private const string CACHE_KEY_LAST_SQL_UPDATE = 'auto_update_last_sql';
|
||||
|
||||
public function handle(AlertService $alertService, EmulatorUpdateService $updateService): int
|
||||
{
|
||||
$this->info('Automatische update check...');
|
||||
|
||||
$isForced = $this->option('force');
|
||||
$sqlOnly = $this->option('sql-only');
|
||||
$emuOnly = $this->option('emu-only');
|
||||
|
||||
if (! $isForced && ! $this->isScheduledTime()) {
|
||||
$this->line('Niet de geplande tijd, overslaan.');
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
if (! $sqlOnly) {
|
||||
$this->runEmulatorUpdate($alertService, $updateService);
|
||||
}
|
||||
|
||||
if (! $emuOnly) {
|
||||
$this->runSqlUpdates($alertService, $updateService);
|
||||
}
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
private function isScheduledTime(): bool
|
||||
{
|
||||
$enabled = setting('auto_update_enabled', true);
|
||||
|
||||
if (! $enabled) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$scheduleTime = setting('auto_update_schedule', '03:00');
|
||||
$scheduleDays = setting('auto_update_days', '0,1,2,3,4,5,6');
|
||||
|
||||
$now = now();
|
||||
$currentTime = $now->format('H:i');
|
||||
$currentDay = (int) $now->dayOfWeek;
|
||||
|
||||
$allowedDays = array_map(intval(...), explode(',', $scheduleDays));
|
||||
|
||||
if (! in_array($currentDay, $allowedDays)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($currentTime !== $scheduleTime) {
|
||||
$minuteDiff = abs(strtotime($currentTime) - strtotime((string) $scheduleTime)) / 60;
|
||||
if ($minuteDiff > 5) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private function runEmulatorUpdate(AlertService $alertService, EmulatorUpdateService $updateService): void
|
||||
{
|
||||
$check = $updateService->checkForUpdates();
|
||||
|
||||
if (! ($check['update_available'] ?? false)) {
|
||||
$this->line('Emulator is al up-to-date.');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->warn('Nieuwe emulator versie beschikbaar: ' . ($check['latest_version'] ?? 'onbekend'));
|
||||
$this->info('Emulator updaten...');
|
||||
|
||||
$result = $updateService->updateEmulator();
|
||||
|
||||
if ($result['success']) {
|
||||
$this->info('Emulator succesvol geüpdatet!');
|
||||
$alertService->sendEmulatorUpdate($result['version'] ?? 'onbekend', $result['message'] ?? '');
|
||||
Log::info('[AutoUpdate] Emulator updated to v' . ($result['version'] ?? 'unknown'));
|
||||
} else {
|
||||
$this->error('Emulator update mislukt: ' . ($result['error'] ?? 'onbekende fout'));
|
||||
$alertService->send(
|
||||
AlertType::EMULATOR_ERROR,
|
||||
'Emulator Auto-Update Mislukt: ' . ($result['error'] ?? 'Onbekende fout'),
|
||||
[],
|
||||
AlertChannel::DISCORD,
|
||||
);
|
||||
}
|
||||
|
||||
Cache::put(self::CACHE_KEY_LAST_EMU_UPDATE, now()->toIso8601String());
|
||||
}
|
||||
|
||||
private function runSqlUpdates(AlertService $alertService, EmulatorUpdateService $updateService): void
|
||||
{
|
||||
$check = $updateService->checkForSqlUpdates();
|
||||
|
||||
if (! ($check['has_updates'] ?? false)) {
|
||||
$this->line('Geen SQL updates beschikbaar.');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->warn($check['message']);
|
||||
$this->info('SQL updates uitvoeren...');
|
||||
|
||||
$result = $updateService->runSqlUpdates();
|
||||
|
||||
if ($result['success'] && ! empty($result['files_run'])) {
|
||||
$count = count($result['files_run']);
|
||||
$this->info("{$count} SQL updates succesvol uitgevoerd!");
|
||||
$alertService->sendSqlUpdate($count, $result['message'] ?? '');
|
||||
Log::info('[AutoUpdate] SQL updates completed', ['files' => $result['files_run']]);
|
||||
} elseif (! empty($result['errors'])) {
|
||||
$this->warn('SQL updates met fouten: ' . implode(', ', $result['errors']));
|
||||
$alertService->send(
|
||||
AlertType::CRITICAL_ERROR,
|
||||
'SQL Updates Met Fouten: ' . implode(', ', $result['errors']),
|
||||
['files' => $result['errors']],
|
||||
AlertChannel::DISCORD,
|
||||
);
|
||||
}
|
||||
|
||||
Cache::put(self::CACHE_KEY_LAST_SQL_UPDATE, now()->toIso8601String());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user