Loheide-eu-mitarbeiterplatt.../app/Http/Middleware/RunPseudoCron.php
Friederich Loheide 987bf7c8f8
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
Hintergrundaufgaben: Pseudo-Cron nur nach Seitenaufrufen, Queue im selben Prozess (0.28.2)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 10:43:47 +00:00

52 lines
1.6 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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();
}
}