Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 6m55s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 7m6s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 6m26s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 6m13s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 9m12s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 8m41s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 6m19s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 6m29s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m33s
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
52 lines
1.6 KiB
PHP
52 lines
1.6 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Middleware;
|
||
|
||
use App\Support\BackgroundTasks;
|
||
use App\Support\Installation;
|
||
use Closure;
|
||
use Illuminate\Http\Request;
|
||
use Symfony\Component\HttpFoundation\Response;
|
||
|
||
/**
|
||
* Pseudo-Cron: Läuft weder echter Cron noch Web-Cron, werden fällige
|
||
* Hintergrundaufgaben nach dem Ausliefern einer Seite erledigt – höchstens
|
||
* einmal pro Minute.
|
||
*/
|
||
class RunPseudoCron
|
||
{
|
||
public function __construct(private readonly BackgroundTasks $tasks) {}
|
||
|
||
public function handle(Request $request, Closure $next): Response
|
||
{
|
||
return $next($request);
|
||
}
|
||
|
||
public function terminate(Request $request, Response $response): void
|
||
{
|
||
if (app()->runningUnitTests() || ! Installation::isInstalled() || ! self::suitable($request) || ! $this->tasks->pseudoCronDue()) {
|
||
return;
|
||
}
|
||
|
||
// Antwort vorher vollständig ausliefern, wo der Server das kann
|
||
if (function_exists('fastcgi_finish_request')) {
|
||
fastcgi_finish_request();
|
||
} elseif (function_exists('litespeed_finish_request')) {
|
||
litespeed_finish_request();
|
||
}
|
||
|
||
$this->tasks->run('pseudo');
|
||
}
|
||
|
||
/**
|
||
* Nur nach normalen Seitenaufrufen – nie bei Speichern-Aktionen oder Livewire-/Inertia-Anfragen
|
||
* im Hintergrund: Kann der Server die Antwort nicht vorab ausliefern, würden diese sonst warten.
|
||
*/
|
||
public static function suitable(Request $request): bool
|
||
{
|
||
return $request->isMethod('GET')
|
||
&& ! $request->hasHeader('X-Livewire')
|
||
&& ! $request->hasHeader('X-Inertia')
|
||
&& ! $request->expectsJson();
|
||
}
|
||
}
|