Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 12m25s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 12m51s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 10m43s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 10m37s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 12m42s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 13m38s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 9m39s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 9m33s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 44s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m10s
- Ladefehler während eines Updates schalten keine Plugins mehr ab: Wartungsmodus merkt sich den Update-Zeitraum (+2 min Nachlauf), Plugin-Uploads markieren den Dateiaustausch ohne Wartungsmodus; guard/recoverFromCrash prüfen das, betroffene Plugins (mit disabled_at im Zeitraum) werden wieder eingeschaltet - Plugin-Oberflächen werden nach public/plugin-assets kopiert (Webserver statt PHP), bei Deaktivieren/Löschen/Neu-Hochladen entfernt; Route bleibt als Rückfall - RichTextEditor in SDK, Leichte-Sprache-Leiste und Glossar asynchron; fehlende Menü-Symbole kuratiert → kein Laden des kompletten Symbolsatzes - Inertia-Prefetch (hover, 10 s) für Menülinks und Neuigkeiten-Vorschauen; Prefetch zählt nicht als Lesen (Reach) Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
135 lines
4 KiB
PHP
135 lines
4 KiB
PHP
<?php
|
||
|
||
namespace App\Support\Update;
|
||
|
||
/**
|
||
* Wartungsmodus während eines Updates als Flag-Datei storage/.maintenance.
|
||
*
|
||
* Selbstheilend: Ein abgelaufenes „until“ wird beim Prüfen entfernt. Ohne das
|
||
* würde ein Absturz mitten im Update die Installation dauerhaft sperren – und
|
||
* niemand käme mehr an die Oberfläche, um den Modus abzuschalten.
|
||
*/
|
||
class MaintenanceFlag
|
||
{
|
||
/**
|
||
* So lange nach einem Update gelten Abstürze beim Laden noch als Folge des Dateiaustauschs
|
||
* (Zwischenspeicher wie OPcache und parallele Anfragen brauchen einen Moment).
|
||
*/
|
||
public const GRACE_SECONDS = 120;
|
||
|
||
public function __construct(private readonly ?string $file = null) {}
|
||
|
||
public function activate(int $maxSeconds = 600): void
|
||
{
|
||
AtomicFile::write($this->path(), json_encode([
|
||
'since' => time(),
|
||
'until' => time() + $maxSeconds,
|
||
]));
|
||
}
|
||
|
||
public function deactivate(): void
|
||
{
|
||
$data = json_decode((string) @file_get_contents($this->path()), true);
|
||
@unlink($this->path());
|
||
|
||
// Zeitraum des letzten Updates merken (siehe covers())
|
||
if (is_array($data) && isset($data['since'])) {
|
||
AtomicFile::write($this->lastPath(), json_encode(['since' => (int) $data['since'], 'until' => time()]));
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Dateiaustausch ohne Wartungsmodus (z. B. ein Plugin wird aktualisiert): Die Seite bleibt
|
||
* erreichbar, Ladefehler in dieser Zeit schalten aber kein Plugin ab.
|
||
*/
|
||
public function beginQuietUpdate(): void
|
||
{
|
||
AtomicFile::write($this->windowPath(), json_encode(['since' => time()]));
|
||
}
|
||
|
||
public function endQuietUpdate(): void
|
||
{
|
||
$data = json_decode((string) @file_get_contents($this->windowPath()), true);
|
||
@unlink($this->windowPath());
|
||
|
||
if (is_array($data) && isset($data['since'])) {
|
||
AtomicFile::write($this->lastPath(), json_encode(['since' => (int) $data['since'], 'until' => time()]));
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Läuft gerade ein Update – oder ist eines erst eben zu Ende gegangen?
|
||
*/
|
||
public function isUpdating(): bool
|
||
{
|
||
return $this->isActive() || $this->covers(time());
|
||
}
|
||
|
||
/**
|
||
* Fiel dieser Zeitpunkt in ein Update (einschließlich der Nachlaufzeit)?
|
||
*/
|
||
public function covers(int $timestamp): bool
|
||
{
|
||
foreach ([$this->path(), $this->windowPath(), $this->lastPath()] as $file) {
|
||
$data = json_decode((string) @file_get_contents($file), true);
|
||
|
||
if (! is_array($data) || ! isset($data['since'])) {
|
||
continue;
|
||
}
|
||
|
||
$until = match ($file) {
|
||
// Läuft noch (ein abgebrochener Plugin-Austausch zählt höchstens zehn Minuten)
|
||
$this->path() => max(time(), (int) ($data['until'] ?? 0)),
|
||
$this->windowPath() => min(time(), (int) $data['since'] + 600),
|
||
default => (int) ($data['until'] ?? 0),
|
||
};
|
||
|
||
if ($timestamp >= (int) $data['since'] && $timestamp <= $until + self::GRACE_SECONDS) {
|
||
return true;
|
||
}
|
||
}
|
||
|
||
return false;
|
||
}
|
||
|
||
public function isActive(): bool
|
||
{
|
||
if (! is_file($this->path())) {
|
||
return false;
|
||
}
|
||
|
||
$data = json_decode((string) @file_get_contents($this->path()), true);
|
||
$until = is_array($data) ? (int) ($data['until'] ?? 0) : 0;
|
||
|
||
if ($until <= time()) {
|
||
$this->deactivate();
|
||
|
||
return false;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
public function retryAfter(): int
|
||
{
|
||
$data = json_decode((string) @file_get_contents($this->path()), true);
|
||
$remaining = is_array($data) ? (int) ($data['until'] ?? 0) - time() : 0;
|
||
|
||
return max(5, min(60, $remaining));
|
||
}
|
||
|
||
private function path(): string
|
||
{
|
||
return $this->file ?? storage_path('.maintenance');
|
||
}
|
||
|
||
private function windowPath(): string
|
||
{
|
||
return $this->path().'-quiet';
|
||
}
|
||
|
||
private function lastPath(): string
|
||
{
|
||
return $this->path().'-last';
|
||
}
|
||
}
|