- Jobs speichern Ergebnis nur per bedingtem UPDATE auf den analysierten Hash; publish() prüft bei ki/scheduler unter der Zeilensperre die jüngste gültige Klassifikation und plant sonst eine neue Prüfung ein (StaleClassificationException) - Re-Check ohne Routing: Rot bei einer PM in Review lehnt wie die Erstprüfung ab - consumePublishSlot() zählt eine NULL-Periode wie den laufenden Monat; Backfill-Migration setzt die Periode für Altverbrauch - ApiPortalResolutionTest: gemeinsame Kategorie wegen der U10-Prüfung Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
165 lines
6.1 KiB
PHP
165 lines
6.1 KiB
PHP
<?php
|
||
|
||
namespace App\Console\Commands;
|
||
|
||
use App\Enums\PressReleaseClassification;
|
||
use App\Enums\PressReleaseStatus;
|
||
use App\Jobs\ClassifyPressRelease;
|
||
use App\Models\KiAudit;
|
||
use App\Models\PressRelease;
|
||
use App\Scopes\PortalScope;
|
||
use App\Services\PressRelease\BlacklistViolationException;
|
||
use App\Services\PressRelease\PressReleaseService;
|
||
use App\Services\PressRelease\StaleClassificationException;
|
||
use Illuminate\Console\Command;
|
||
use Illuminate\Support\Str;
|
||
use Throwable;
|
||
|
||
/**
|
||
* Veröffentlicht Pressemitteilungen mit Status `review`, der KI-Klassifikation
|
||
* `green` und einem `scheduled_at`-Zeitpunkt, der erreicht/überschritten wurde.
|
||
*
|
||
* Läuft regelmäßig per Scheduler (siehe routes/console.php). Idempotent:
|
||
* berührt nur grüne PRs in Review-Status — bereits publishte werden ignoriert.
|
||
* Gelb eingestufte PMs bleiben bewusst in der manuellen Admin-Queue, auch wenn
|
||
* ihr Termin fällig ist.
|
||
*
|
||
* Blacklist-Treffer landen wie beim manuellen Publish im Reject-Status mit
|
||
* Mail-Benachrichtigung des Autors.
|
||
*
|
||
* Inhaltsbindung (Audit F07): Passt die jüngste Klassifikation nicht mehr
|
||
* zum aktuellen Text (Admin- oder Korrektur-Bearbeitung nach der Prüfung),
|
||
* wird nicht veröffentlicht, sondern eine Prüfung mit Routing eingeplant –
|
||
* Grün geht dann zum fälligen Termin live, Rot lehnt ab und benachrichtigt.
|
||
* Die Vorprüfung hier ist nur der schnelle Filter; verbindlich prüft
|
||
* PressReleaseService::publish() unter der Zeilensperre noch einmal
|
||
* (Audit U02) und meldet eine Abweichung als StaleClassificationException –
|
||
* die zählt hier ebenfalls als Re-Check, nicht als Fehler; publish() hat die
|
||
* Prüfung dann schon eingeplant.
|
||
* Technische Fehler ergeben einen Fehler-Exit-Code, damit der Failure-Hook
|
||
* des Schedulers greift (Audit F20); fachliche Ablehnungen nicht.
|
||
*/
|
||
class PublishScheduledPressReleases extends Command
|
||
{
|
||
/**
|
||
* @var string
|
||
*/
|
||
protected $signature = 'press-releases:publish-scheduled
|
||
{--dry-run : Nur anzeigen, was publiziert würde, ohne DB zu ändern}
|
||
{--limit=200 : Maximale Anzahl pro Lauf}';
|
||
|
||
/**
|
||
* @var string
|
||
*/
|
||
protected $description = 'Veröffentlicht fällige geplante Pressemitteilungen (Status review + scheduled_at <= now).';
|
||
|
||
public function handle(PressReleaseService $service): int
|
||
{
|
||
$dryRun = (bool) $this->option('dry-run');
|
||
$limit = max(1, (int) $this->option('limit'));
|
||
|
||
$now = now();
|
||
|
||
// Gelb und Grün gehen zum Termin automatisch live (Decision-Update
|
||
// §5.0); nur Rot wird abgelehnt. Unklassifizierte PMs bleiben als
|
||
// Fallback in der manuellen Queue.
|
||
$candidates = PressRelease::withoutGlobalScope(PortalScope::class)
|
||
->where('status', PressReleaseStatus::Review->value)
|
||
->whereIn('classification', [
|
||
PressReleaseClassification::Green->value,
|
||
PressReleaseClassification::Yellow->value,
|
||
])
|
||
->whereNotNull('scheduled_at')
|
||
->where('scheduled_at', '<=', $now)
|
||
->orderBy('scheduled_at')
|
||
->limit($limit)
|
||
->get();
|
||
|
||
if ($candidates->isEmpty()) {
|
||
$this->info('Keine fälligen geplanten Pressemitteilungen gefunden.');
|
||
|
||
return self::SUCCESS;
|
||
}
|
||
|
||
$this->info(sprintf(
|
||
'%d fällige Pressemitteilung(en) gefunden.%s',
|
||
$candidates->count(),
|
||
$dryRun ? ' (Dry-Run)' : '',
|
||
));
|
||
|
||
$published = 0;
|
||
$rejected = 0;
|
||
$failed = 0;
|
||
$rechecked = 0;
|
||
|
||
foreach ($candidates as $pressRelease) {
|
||
$line = sprintf(
|
||
' #%d scheduled_at=%s title="%s"',
|
||
$pressRelease->id,
|
||
$pressRelease->scheduled_at?->format('Y-m-d H:i') ?? '-',
|
||
Str::limit($pressRelease->title, 60),
|
||
);
|
||
|
||
if ($dryRun) {
|
||
$this->line($line.' [DRY]');
|
||
|
||
continue;
|
||
}
|
||
|
||
if ($this->classificationIsStale($pressRelease)) {
|
||
ClassifyPressRelease::dispatch($pressRelease->id, route: true, contentHash: $pressRelease->content_hash)->onQueue('classification');
|
||
$rechecked++;
|
||
$this->warn($line.' [RECHECK: Inhalt nach der Prüfung geändert]');
|
||
|
||
continue;
|
||
}
|
||
|
||
try {
|
||
$service->publish($pressRelease, source: 'scheduler');
|
||
$published++;
|
||
$this->line($line.' [OK]');
|
||
} catch (BlacklistViolationException $e) {
|
||
$rejected++;
|
||
$this->warn($line.' [REJECT: '.$e->word.']');
|
||
} catch (StaleClassificationException) {
|
||
$rechecked++;
|
||
$this->warn($line.' [RECHECK: Inhalt unter der Sperre abweichend, Prüfung neu eingeplant]');
|
||
} catch (Throwable $e) {
|
||
$failed++;
|
||
$this->error($line.' [FAIL: '.$e->getMessage().']');
|
||
report($e);
|
||
}
|
||
}
|
||
|
||
if (! $dryRun) {
|
||
$this->newLine();
|
||
$this->info(sprintf(
|
||
'Fertig: %d veröffentlicht, %d wegen Blacklist abgelehnt, %d neu zur Prüfung, %d fehlgeschlagen.',
|
||
$published,
|
||
$rejected,
|
||
$rechecked,
|
||
$failed,
|
||
));
|
||
}
|
||
|
||
return $failed > 0 ? self::FAILURE : self::SUCCESS;
|
||
}
|
||
|
||
/**
|
||
* Jüngste gültige Klassifikation mit Prüfsumme, die nicht zum aktuellen
|
||
* Inhalt passt. Altbestand ohne Prüfsumme (vor der Bindung) gilt weiter.
|
||
*/
|
||
private function classificationIsStale(PressRelease $pressRelease): bool
|
||
{
|
||
$latestHash = KiAudit::query()
|
||
->where('press_release_id', $pressRelease->id)
|
||
->where('type', KiAudit::TYPE_CLASSIFICATION)
|
||
->whereNull('discarded_at')
|
||
->orderByDesc('id')
|
||
->value('content_hash');
|
||
|
||
return $latestHash !== null
|
||
&& $pressRelease->content_hash !== null
|
||
&& $latestHash !== $pressRelease->content_hash;
|
||
}
|
||
}
|