optimize tag and rebuilding

This commit is contained in:
team2
2026-02-23 08:51:21 +01:00
parent 8c58d6777d
commit dceeaeee52
8 changed files with 678 additions and 167 deletions

View File

@@ -40,64 +40,86 @@ final class TagRebuildRunJobCommand extends Command
/** @var TagRebuildJob|null $job */ /** @var TagRebuildJob|null $job */
$job = $this->em->getRepository(TagRebuildJob::class)->find($jobId); $job = $this->em->getRepository(TagRebuildJob::class)->find($jobId);
if (!$job instanceof TagRebuildJob) { if (!$job instanceof TagRebuildJob) {
$output->writeln('<error>Job not found.</error>'); $output->writeln('<error>Job not found.</error>');
return Command::FAILURE; return Command::FAILURE;
} }
$fh = null;
try {
// --------------------------------------------------------- // ---------------------------------------------------------
// Global lock to avoid parallel rebuilds // LOCK INITIALIZATION
// --------------------------------------------------------- // ---------------------------------------------------------
$lockDir = \dirname($this->lockFilePath); $lockDir = \dirname($this->lockFilePath);
if (!\is_dir($lockDir)) { if (!\is_dir($lockDir) && !@\mkdir($lockDir, 0775, true) && !\is_dir($lockDir)) {
@\mkdir($lockDir, 0775, true); throw new \RuntimeException('Cannot create lock directory.');
} }
$fh = @\fopen($this->lockFilePath, 'c+'); $fh = @\fopen($this->lockFilePath, 'c+');
if (!$fh) { if (!$fh) {
$job->markFailed('Cannot open lock file: ' . $this->lockFilePath); throw new \RuntimeException('Cannot open lock file: ' . $this->lockFilePath);
$this->em->flush();
$output->writeln('<error>Cannot open lock file.</error>');
return Command::FAILURE;
} }
// If another rebuild runs, we fail fast (simple & safe).
if (!@\flock($fh, LOCK_EX | LOCK_NB)) { if (!@\flock($fh, LOCK_EX | LOCK_NB)) {
\fclose($fh); throw new \RuntimeException('Another tag rebuild is currently running (lock busy).');
$job->markFailed('Another tag rebuild is currently running (lock busy).');
$this->em->flush();
$output->writeln('<error>Lock busy. Another rebuild is running.</error>');
return Command::FAILURE;
} }
// mark running // ---------------------------------------------------------
// MARK RUNNING
// ---------------------------------------------------------
$job->markRunning(); $job->markRunning();
$this->em->flush(); $this->em->flush();
try { // ---------------------------------------------------------
// EXPORT TAGS (NDJSON)
// ---------------------------------------------------------
$export = $this->exporter->export(); $export = $this->exporter->export();
if (
!isset($export['path']) ||
!\is_string($export['path']) ||
!\file_exists($export['path'])
) {
throw new \RuntimeException('Export failed: NDJSON file missing.');
}
if (isset($export['count']) && (int)$export['count'] === 0) {
throw new \RuntimeException('Export produced zero tags.');
}
// ---------------------------------------------------------
// BUILD VECTOR INDEX
// ---------------------------------------------------------
$this->builder->build(); $this->builder->build();
// ---------------------------------------------------------
// MARK COMPLETED
// ---------------------------------------------------------
$job->markCompleted(); $job->markCompleted();
$this->em->flush(); $this->em->flush();
$output->writeln('<info>OK</info>'); $output->writeln('<info>Tag rebuild successful.</info>');
$output->writeln('tags.ndjson: ' . $export['path']); $output->writeln('NDJSON: ' . $export['path']);
} catch (\Throwable $e) {
return Command::SUCCESS;
}
catch (\Throwable $e) {
$job->markFailed($e->getMessage()); $job->markFailed($e->getMessage());
$this->em->flush(); $this->em->flush();
$output->writeln('<error>FAILED: ' . $e->getMessage() . '</error>'); $output->writeln('<error>FAILED: ' . $e->getMessage() . '</error>');
@\flock($fh, LOCK_UN);
@\fclose($fh);
return Command::FAILURE; return Command::FAILURE;
} }
finally {
if ($fh) {
@\flock($fh, LOCK_UN); @\flock($fh, LOCK_UN);
@\fclose($fh); @\fclose($fh);
}
return Command::SUCCESS; }
} }
} }

View File

@@ -6,27 +6,33 @@ namespace App\Controller\Admin;
use App\Entity\Document; use App\Entity\Document;
use App\Entity\Tag; use App\Entity\Tag;
use App\Entity\TagRebuildJob;
use App\Service\TagRebuildJobService; use App\Service\TagRebuildJobService;
use Doctrine\DBAL\Types\Types; use App\Tag\TagService;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Uid\Uuid;
#[Route('/admin/documents')] #[Route('/admin/documents')]
final class DocumentTagController extends AbstractController final class DocumentTagController extends AbstractController
{ {
#[Route('/{id}/tags', name: 'admin_document_tags_edit', methods: ['GET'])] #[Route('/{id}/tags', name: 'admin_document_tags_edit', methods: ['GET'])]
public function edit(string $id, EntityManagerInterface $em): Response public function edit(
{ string $id,
EntityManagerInterface $em,
TagRebuildJobService $jobs
): Response {
$document = $em->getRepository(Document::class)->find($id); $document = $em->getRepository(Document::class)->find($id);
if (!$document instanceof Document) { if (!$document instanceof Document) {
throw $this->createNotFoundException('Document not found'); throw $this->createNotFoundException('Document not found');
} }
// 🔹 Alle verfügbaren Tags laden (fehlte!)
$allTags = $em->createQueryBuilder() $allTags = $em->createQueryBuilder()
->select('t') ->select('t')
->from(Tag::class, 't') ->from(Tag::class, 't')
@@ -34,15 +40,16 @@ final class DocumentTagController extends AbstractController
->getQuery() ->getQuery()
->getResult(); ->getResult();
$assigned = []; $latestJob = $jobs->getLatestJob();
foreach ($document->getTags() as $tag) {
$assigned[(string)$tag->getId()] = true;
}
return $this->render('admin/document_tags/edit.html.twig', [ return $this->render('admin/document_tags/edit.html.twig', [
'document' => $document, 'document' => $document,
'allTags' => $allTags, 'allTags' => $allTags, // ✅ jetzt vorhanden
'assigned' => $assigned, 'latestJob' => $latestJob,
'statusRunning' => TagRebuildJob::STATUS_RUNNING,
'statusQueued' => TagRebuildJob::STATUS_QUEUED,
'statusCompleted' => TagRebuildJob::STATUS_COMPLETED,
'statusFailed' => TagRebuildJob::STATUS_FAILED,
]); ]);
} }
@@ -51,7 +58,7 @@ final class DocumentTagController extends AbstractController
string $id, string $id,
Request $request, Request $request,
EntityManagerInterface $em, EntityManagerInterface $em,
TagRebuildJobService $jobs TagService $tagService
): RedirectResponse { ): RedirectResponse {
$document = $em->getRepository(Document::class)->find($id); $document = $em->getRepository(Document::class)->find($id);
@@ -61,33 +68,23 @@ final class DocumentTagController extends AbstractController
$selected = $request->request->all('tag_ids') ?? []; $selected = $request->request->all('tag_ids') ?? [];
$uuidObjects = [];
foreach ($selected as $value) {
try { try {
$uuidObjects[] = \Symfony\Component\Uid\Uuid::fromString($value); $tagService->syncDocumentTags($document, $selected);
} catch (\Throwable) { $this->addFlash('success', 'Tags wurden aktualisiert. Rebuild läuft im Hintergrund.');
continue; } catch (\Throwable $e) {
$this->addFlash('danger', $e->getMessage());
} }
}
// Remove
foreach ($document->getTags() as $tag) {
if (!in_array($tag->getId(), $uuidObjects, false)) {
$document->removeTag($tag);
}
}
// Add
foreach ($uuidObjects as $uuid) {
$tag = $em->find(\App\Entity\Tag::class, $uuid);
if ($tag && !$document->hasTag($tag)) {
$document->addTag($tag);
}
}
$em->flush();
$jobs->enqueueAndStartAsync();
return $this->redirectToRoute('admin_document_tags_edit', ['id' => $id]); return $this->redirectToRoute('admin_document_tags_edit', ['id' => $id]);
} }
#[Route('/admin/tags/status', name: 'admin_tags_status', methods: ['GET'])]
public function status(TagRebuildJobService $jobs): JsonResponse
{
$job = $jobs->getLatestJob();
return $this->json([
'status' => $job?->getStatus(),
]);
}
} }

View File

@@ -5,7 +5,8 @@ declare(strict_types=1);
namespace App\Controller\Admin; namespace App\Controller\Admin;
use App\Entity\Tag; use App\Entity\Tag;
use App\Service\TagRebuildJobService; use App\Entity\TagRebuildJob;
use App\Tag\TagService;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\RedirectResponse;
@@ -17,85 +18,76 @@ use Symfony\Component\Routing\Attribute\Route;
final class TagController extends AbstractController final class TagController extends AbstractController
{ {
#[Route('', name: 'admin_tags_index', methods: ['GET'])] #[Route('', name: 'admin_tags_index', methods: ['GET'])]
public function index(EntityManagerInterface $em): Response public function index(
{ EntityManagerInterface $em,
$tags = $em->createQueryBuilder() \App\Service\TagRebuildJobService $jobs
->select('t') ): Response {
->from(Tag::class, 't')
->orderBy('t.label', 'ASC') $tags = $em->getRepository(\App\Entity\Tag::class)
->getQuery() ->findBy([], ['label' => 'ASC']);
->getResult();
return $this->render('admin/tag/index.html.twig', [ return $this->render('admin/tag/index.html.twig', [
'tags' => $tags, 'tags' => $tags,
'latestJob' => $jobs->getLatestJob(),
'hasActiveJob' => $jobs->hasActiveJob(),
'statusRunning' => TagRebuildJob::STATUS_RUNNING,
'statusQueued' => TagRebuildJob::STATUS_QUEUED,
'statusCompleted' => TagRebuildJob::STATUS_COMPLETED,
'statusFailed' => TagRebuildJob::STATUS_FAILED,
]); ]);
} }
#[Route('/create', name: 'admin_tags_create', methods: ['POST'])] #[Route('/create', name: 'admin_tags_create', methods: ['POST'])]
public function create(Request $request, EntityManagerInterface $em, TagRebuildJobService $jobs): RedirectResponse public function create(
{ Request $request,
TagService $tagService
): RedirectResponse {
$token = (string) $request->request->get('_token', ''); $token = (string) $request->request->get('_token', '');
if (!$this->isCsrfTokenValid('admin_tag_create', $token)) { if (!$this->isCsrfTokenValid('admin_tag_create', $token)) {
$this->addFlash('danger', 'Ungültiges CSRF Token.'); $this->addFlash('danger', 'Ungültiges CSRF Token.');
return $this->redirectToRoute('admin_tags_index'); return $this->redirectToRoute('admin_tags_index');
} }
$label = trim((string)$request->request->get('label', '')); try {
$slug = trim((string)$request->request->get('slug', '')); $tagService->create(
$desc = trim((string)$request->request->get('description', '')); (string) $request->request->get('slug', ''),
(string) $request->request->get('label', ''),
if ($label === '' || $slug === '') { $request->request->get('description')
$this->addFlash('danger', 'Label und Slug sind Pflichtfelder.'); ? (string) $request->request->get('description')
return $this->redirectToRoute('admin_tags_index'); : null
} );
$exists = (int)$em->createQueryBuilder()
->select('COUNT(t.id)')
->from(Tag::class, 't')
->where('t.slug = :slug')
->setParameter('slug', $slug)
->getQuery()
->getSingleScalarResult();
if ($exists > 0) {
$this->addFlash('danger', 'Slug existiert bereits.');
return $this->redirectToRoute('admin_tags_index');
}
$tag = new Tag($slug, $label, $desc !== '' ? $desc : null);
$em->persist($tag);
$em->flush();
// enqueue async rebuild
$jobs->enqueueAndStartAsync();
$this->addFlash('success', 'Tag wurde erstellt. Rebuild läuft im Hintergrund.'); $this->addFlash('success', 'Tag wurde erstellt. Rebuild läuft im Hintergrund.');
} catch (\Throwable $e) {
$this->addFlash('danger', $e->getMessage());
}
return $this->redirectToRoute('admin_tags_index'); return $this->redirectToRoute('admin_tags_index');
} }
#[Route('/{id}/delete', name: 'admin_tags_delete', methods: ['POST'])] #[Route('/{id}/delete', name: 'admin_tags_delete', methods: ['POST'])]
public function delete(string $id, Request $request, EntityManagerInterface $em, TagRebuildJobService $jobs): RedirectResponse public function delete(
{ string $id,
Request $request,
TagService $tagService
): RedirectResponse {
$token = (string) $request->request->get('_token', ''); $token = (string) $request->request->get('_token', '');
if (!$this->isCsrfTokenValid('admin_tag_delete_' . $id, $token)) { if (!$this->isCsrfTokenValid('admin_tag_delete_' . $id, $token)) {
$this->addFlash('danger', 'Ungültiges CSRF Token.'); $this->addFlash('danger', 'Ungültiges CSRF Token.');
return $this->redirectToRoute('admin_tags_index'); return $this->redirectToRoute('admin_tags_index');
} }
$tag = $em->getRepository(Tag::class)->find($id); try {
if (!$tag instanceof Tag) { $tagService->deleteById($id);
$this->addFlash('danger', 'Tag nicht gefunden.');
return $this->redirectToRoute('admin_tags_index');
}
$em->remove($tag);
$em->flush();
// enqueue async rebuild
$jobs->enqueueAndStartAsync();
$this->addFlash('success', 'Tag wurde gelöscht. Rebuild läuft im Hintergrund.'); $this->addFlash('success', 'Tag wurde gelöscht. Rebuild läuft im Hintergrund.');
} catch (\Throwable $e) {
$this->addFlash('danger', $e->getMessage());
}
return $this->redirectToRoute('admin_tags_index'); return $this->redirectToRoute('admin_tags_index');
} }
} }

View File

@@ -0,0 +1,62 @@
<?php
declare(strict_types=1);
namespace App\Controller\Admin;
use App\Entity\TagRebuildJob;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\Routing\Attribute\Route;
final class TagRebuildStreamController
{
#[Route('/admin/tags/rebuild/stream', name: 'admin_tags_rebuild_stream')]
public function stream(EntityManagerInterface $em): StreamedResponse
{
$response = new StreamedResponse(function () use ($em) {
// Sofort erstes Event senden (wichtig!)
echo "event: ping\n";
echo "data: " . json_encode(['init' => true]) . "\n\n";
@ob_flush();
@flush();
while (!connection_aborted()) {
$em->clear();
$job = $em->createQueryBuilder()
->select('j')
->from(TagRebuildJob::class, 'j')
->orderBy('j.createdAt', 'DESC')
->setMaxResults(1)
->getQuery()
->getOneOrNullResult();
if ($job) {
echo "event: message\n";
echo "data: " . json_encode([
'status' => $job->getStatus(),
'startedAt' => $job->getStartedAt()?->format(DATE_ATOM),
'finishedAt' => $job->getFinishedAt()?->format(DATE_ATOM),
'error' => $job->getErrorMessage(),
]) . "\n\n";
@ob_flush();
@flush();
}
sleep(2);
}
});
$response->headers->set('Content-Type', 'text/event-stream');
$response->headers->set('Cache-Control', 'no-cache');
$response->headers->set('Connection', 'keep-alive');
$response->headers->set('X-Accel-Buffering', 'no'); // 🔥 wichtig bei nginx
return $response;
}
}

View File

@@ -10,6 +10,12 @@ use Psr\Log\LoggerInterface;
final readonly class TagRebuildJobService final readonly class TagRebuildJobService
{ {
/**
* Wenn ein QUEUED-Job länger nicht startet, gilt er als "stale" und wird auf FAILED gesetzt,
* damit das System nicht dauerhaft blockiert.
*/
private const STALE_QUEUED_AFTER_SECONDS = 300; // 5 Minuten
public function __construct( public function __construct(
private EntityManagerInterface $em, private EntityManagerInterface $em,
private LoggerInterface $agentLogger, private LoggerInterface $agentLogger,
@@ -28,24 +34,153 @@ final readonly class TagRebuildJobService
return $job; return $job;
} }
public function enqueueIfIdle(): ?TagRebuildJob
{
// Coalescing: Wenn ein Job läuft oder queued ist -> nichts tun
if ($this->hasActiveJob()) {
return null;
}
return $this->enqueueAndStartAsync();
}
/**
* Letzter Job (egal welcher Status).
*/
public function getLatestJob(): ?TagRebuildJob
{
return $this->em->createQueryBuilder()
->select('j')
->from(TagRebuildJob::class, 'j')
->orderBy('j.createdAt', 'DESC')
->setMaxResults(1)
->getQuery()
->getOneOrNullResult();
}
/**
* Letzter Job mit Status COMPLETED.
*/
public function getLatestCompletedJob(): ?TagRebuildJob
{
return $this->em->createQueryBuilder()
->select('j')
->from(TagRebuildJob::class, 'j')
->where('j.status = :status')
->setParameter('status', TagRebuildJob::STATUS_COMPLETED)
->orderBy('j.createdAt', 'DESC')
->setMaxResults(1)
->getQuery()
->getOneOrNullResult();
}
/**
* Ob gerade ein Job aktiv ist:
* - RUNNING ist immer aktiv
* - QUEUED ist nur aktiv, wenn er nicht stale ist
*
* Zusätzlich: stale QUEUED Jobs werden auf FAILED gesetzt (Recovery).
*/
public function hasActiveJob(): bool
{
$this->markStaleQueuedJobsFailed();
$cutoff = new \DateTimeImmutable('-' . self::STALE_QUEUED_AFTER_SECONDS . ' seconds');
$qb = $this->em->createQueryBuilder();
$qb->select('COUNT(j.id)')
->from(TagRebuildJob::class, 'j')
->where(
$qb->expr()->orX(
'j.status = :running',
$qb->expr()->andX(
'j.status = :queued',
'j.createdAt >= :cutoff'
)
)
)
->setParameter('running', TagRebuildJob::STATUS_RUNNING)
->setParameter('queued', TagRebuildJob::STATUS_QUEUED)
->setParameter('cutoff', $cutoff);
return (int) $qb->getQuery()->getSingleScalarResult() > 0;
}
/**
* Startet den Job async über bin/console.
* Wichtige Fixes:
* - php explizit verwenden
* - --no-interaction
* - Logfile statt /dev/null
*/
private function startAsync(TagRebuildJob $job): void private function startAsync(TagRebuildJob $job): void
{ {
$php = PHP_BINARY; // safest in runtime $projectDir = rtrim($this->projectDir, '/');
$console = rtrim($this->projectDir, '/') . '/bin/console'; $console = $projectDir . '/bin/console';
$jobId = (string) $job->getId();
$logDir = $projectDir . '/var/log/tags';
if (!is_dir($logDir)) {
@mkdir($logDir, 0777, true);
}
$logFile = $logDir . '/job_' . $jobId . '.log';
// Robust: cd ins Projekt, dann nohup php bin/console ...
$cmd = sprintf( $cmd = sprintf(
'%s %s %s %s > /dev/null 2>&1 &', 'cd %s && nohup %s %s %s %s --no-interaction >> %s 2>&1 &',
escapeshellarg($php), escapeshellarg($projectDir),
escapeshellcmd('php'),
escapeshellarg($console), escapeshellarg($console),
'mto:agent:tags:job:run', escapeshellarg('mto:agent:tags:job:run'),
escapeshellarg((string)$job->getId()) escapeshellarg($jobId),
escapeshellarg($logFile)
); );
$this->agentLogger->info('[tags] enqueue job async', [ $this->agentLogger->info('[tags] enqueue job async', [
'job' => (string)$job->getId(), 'job' => $jobId,
'cmd' => $cmd, 'cmd' => $cmd,
'log' => $logFile,
]); ]);
@exec($cmd); @exec($cmd);
} }
/**
* Recovery gegen "ewig QUEUED":
* Setzt alte QUEUED Jobs auf FAILED, damit enqueueIfIdle() nicht dauerhaft blockiert.
*/
private function markStaleQueuedJobsFailed(): void
{
$cutoff = new \DateTimeImmutable('-' . self::STALE_QUEUED_AFTER_SECONDS . ' seconds');
$qb = $this->em->createQueryBuilder();
$qb->select('j')
->from(TagRebuildJob::class, 'j')
->where('j.status = :queued')
->andWhere('j.createdAt < :cutoff')
->setParameter('queued', TagRebuildJob::STATUS_QUEUED)
->setParameter('cutoff', $cutoff)
->setMaxResults(25);
/** @var TagRebuildJob[] $stale */
$stale = $qb->getQuery()->getResult();
if (!$stale) {
return;
}
foreach ($stale as $job) {
$jobId = (string) $job->getId();
$job->markFailed('Stale QUEUED job detected (async start likely failed).');
$this->agentLogger->warning('[tags] stale QUEUED job marked FAILED', [
'job' => $jobId,
'cutoff' => $cutoff->format(\DateTimeInterface::ATOM),
]);
}
$this->em->flush();
}
} }

134
src/Tag/TagService.php Normal file
View File

@@ -0,0 +1,134 @@
<?php
declare(strict_types=1);
namespace App\Tag;
use App\Entity\Tag;
use App\Entity\Document;
use App\Entity\DocumentTag;
use App\Service\TagRebuildJobService;
use Doctrine\ORM\EntityManagerInterface;
final class TagService
{
public function __construct(
private EntityManagerInterface $em,
private TagRebuildJobService $jobs,
) {}
// =========================================================
// TAG CREATE
// =========================================================
public function create(string $slug, string $label, ?string $description = null): Tag
{
$slug = trim($slug);
$label = trim($label);
if ($label === '' || $slug === '') {
throw new \InvalidArgumentException('Label und Slug sind Pflichtfelder.');
}
if ($this->slugExists($slug)) {
throw new \RuntimeException('Slug existiert bereits.');
}
$tag = new Tag($slug, $label, $description);
$this->em->persist($tag);
$this->em->flush();
$this->triggerRebuildIfIdle();
return $tag;
}
// =========================================================
// TAG DELETE
// =========================================================
public function deleteById(string $tagId): void
{
$tag = $this->em->getRepository(Tag::class)->find($tagId);
if (!$tag instanceof Tag) {
throw new \RuntimeException('Tag nicht gefunden.');
}
$this->delete($tag);
}
public function delete(Tag $tag): void
{
$this->em->remove($tag);
$this->em->flush();
$this->triggerRebuildIfIdle();
}
// =========================================================
// DOCUMENT TAG SYNC
// =========================================================
/**
* Synchronisiert alle Tags eines Dokuments.
* Löst einen Rebuild aus, da document_ids Teil des NDJSON sind.
*/
public function syncDocumentTags(Document $document, array $newTagIds): void
{
$newTagIds = array_unique($newTagIds);
$currentRelations = $this->em
->getRepository(DocumentTag::class)
->findBy(['document' => $document]);
$currentTagIds = array_map(
fn(DocumentTag $dt) => (string) $dt->getTag()->getId(),
$currentRelations
);
$toAdd = array_diff($newTagIds, $currentTagIds);
$toRemove = array_diff($currentTagIds, $newTagIds);
foreach ($toAdd as $tagId) {
$tag = $this->em->getRepository(Tag::class)->find($tagId);
if ($tag instanceof Tag) {
$this->em->persist(new DocumentTag($document, $tag));
}
}
foreach ($currentRelations as $relation) {
if (in_array((string) $relation->getTag()->getId(), $toRemove, true)) {
$this->em->remove($relation);
}
}
if ($toAdd || $toRemove) {
$this->em->flush();
$this->triggerRebuildIfIdle();
}
}
// =========================================================
// INTERNAL HELPERS
// =========================================================
private function slugExists(string $slug): bool
{
return (int) $this->em->createQueryBuilder()
->select('COUNT(t.id)')
->from(Tag::class, 't')
->where('t.slug = :slug')
->setParameter('slug', $slug)
->getQuery()
->getSingleScalarResult() > 0;
}
private function triggerRebuildIfIdle(): void
{
if (!$this->jobs->hasActiveJob()) {
$this->jobs->enqueueAndStartAsync();
}
}
}

View File

@@ -4,6 +4,76 @@
{% block body %} {% block body %}
{# ============================================= #}
{# Tag-Rebuild Status (Echte Live-Anzeige) #}
{# ============================================= #}
<div id="rebuild-status"></div>
<script>
let polling = null;
function renderStatus(status) {
const el = document.getElementById('rebuild-status');
if (!status) {
el.innerHTML = '';
return;
}
if (status === 'RUNNING') {
el.innerHTML = `
<div class="alert alert-info d-flex justify-content-between align-items-center">
<div><strong>Rebuild läuft…</strong></div>
<div class="spinner-border spinner-border-sm"></div>
</div>
`;
} else if (status === 'QUEUED') {
el.innerHTML = `
<div class="alert alert-secondary">
Rebuild in Warteschlange…
</div>
`;
} else if (status === 'COMPLETED') {
el.innerHTML = `
<div class="alert alert-success">
Rebuild abgeschlossen.
</div>
`;
stopPolling();
} else if (status === 'FAILED') {
el.innerHTML = `
<div class="alert alert-danger">
Rebuild fehlgeschlagen.
</div>
`;
stopPolling();
}
}
function checkStatus() {
fetch('{{ path('admin_tags_status') }}')
.then(r => r.json())
.then(data => renderStatus(data.status))
.catch(() => stopPolling());
}
function startPolling() {
polling = setInterval(checkStatus, 2000);
}
function stopPolling() {
if (polling) {
clearInterval(polling);
polling = null;
}
}
// Start polling sofort
checkStatus();
startPolling();
</script>
<div class="d-flex justify-content-between align-items-center mb-4"> <div class="d-flex justify-content-between align-items-center mb-4">
<h1 class="h3 mb-0"> <h1 class="h3 mb-0">
Tags für Dokument Tags für Dokument

View File

@@ -5,80 +5,179 @@
{% block body %} {% block body %}
<div class="d-flex justify-content-between align-items-center mb-4"> <div class="d-flex justify-content-between align-items-center mb-4">
<h1 class="h3">Tags</h1> <h1 class="h3 mb-0">Tag-Management</h1>
</div> </div>
{# ========================================================= #}
{# Flash Messages #}
{# ========================================================= #}
{% for message in app.flashes('success') %} {% for message in app.flashes('success') %}
<div class="alert alert-success">{{ message }}</div> <div class="alert alert-success shadow-sm">{{ message }}</div>
{% endfor %} {% endfor %}
{% for message in app.flashes('danger') %} {% for message in app.flashes('danger') %}
<div class="alert alert-danger">{{ message }}</div> <div class="alert alert-danger shadow-sm">{{ message }}</div>
{% endfor %} {% endfor %}
<div class="card bg-black border-secondary mb-4 text-light"> {# ========================================================= #}
{# LIVE REBUILD STATUS (SSE) #}
{# ========================================================= #}
<div id="rebuild-status">
{% if latestJob %}
<div class="alert alert-secondary shadow-sm">
Status wird geladen…
</div>
{% endif %}
</div>
<script>
const statusBox = document.getElementById('rebuild-status');
const source = new EventSource("{{ path('admin_tags_rebuild_stream') }}");
source.onmessage = function (event) {
const data = JSON.parse(event.data);
let html = '';
if (data.status === '{{ statusRunning }}') {
html = `
<div class="alert alert-info shadow-sm d-flex justify-content-between align-items-center">
<div>
<strong>Rebuild läuft</strong><br>
${data.startedAt ? 'Gestartet: ' + new Date(data.startedAt).toLocaleString() : ''}
</div>
<div class="spinner-border spinner-border-sm"></div>
</div>
`;
}
else if (data.status === '{{ statusQueued }}') {
html = `
<div class="alert alert-secondary shadow-sm">
<strong>Rebuild in Warteschlange</strong>
</div>
`;
}
else if (data.status === '{{ statusCompleted }}') {
html = `
<div class="alert alert-success shadow-sm">
<strong>Rebuild erfolgreich abgeschlossen</strong>
</div>
`;
}
else if (data.status === '{{ statusFailed }}') {
html = `
<div class="alert alert-danger shadow-sm">
<strong>Rebuild fehlgeschlagen</strong><br>
${data.error ? '<code>' + data.error + '</code>' : ''}
</div>
`;
}
statusBox.innerHTML = html;
};
source.onerror = function () {
console.warn('SSE Verbindung verloren');
};
</script>
{# ========================================================= #}
{# Create Tag Card #}
{# ========================================================= #}
<div class="card bg-black border-secondary text-light mb-4 shadow-sm">
<div class="card-body"> <div class="card-body">
<h5 class="text-info mb-3">Neuen Tag erstellen</h5> <h5 class="text-info mb-3">Neuen Tag erstellen</h5>
<form method="post" action="{{ path('admin_tags_create') }}" class="row g-2"> <form method="post" action="{{ path('admin_tags_create') }}" class="row g-3">
<input type="hidden" name="_token" value="{{ csrf_token('admin_tag_create') }}"/> <input type="hidden" name="_token" value="{{ csrf_token('admin_tag_create') }}"/>
<div class="col-md-3"> <div class="col-md-3">
<input class="form-control form-control-sm" name="label" placeholder="Label (z.B. Testomat 808)" required /> <label class="form-label small text-muted">Label</label>
<input class="form-control form-control-sm"
name="label"
placeholder="z. B. Testomat 808"
required />
</div> </div>
<div class="col-md-3"> <div class="col-md-3">
<input class="form-control form-control-sm" name="slug" placeholder="Slug (z.B. testomat-808)" required /> <label class="form-label small text-muted">Slug</label>
<input class="form-control form-control-sm"
name="slug"
placeholder="z. B. testomat-808"
required />
</div> </div>
<div class="col-md-4"> <div class="col-md-4">
<input class="form-control form-control-sm" name="description" placeholder="Beschreibung (optional)" /> <label class="form-label small text-muted">Beschreibung</label>
<input class="form-control form-control-sm"
name="description"
placeholder="Optional" />
</div> </div>
<div class="col-md-2 d-grid"> <div class="col-md-2 d-grid align-items-end">
<button class="btn btn-sm btn-outline-info" type="submit">Anlegen</button> <button class="btn btn-sm btn-outline-info">
</div> Anlegen
</button>
<div class="col-12">
<small class="text-light">
Hinweis: Nach Änderungen an Tags/Zuweisungen bitte <code>bin/console mto:agent:tags:rebuild</code> ausführen.
</small>
</div> </div>
</form> </form>
</div> </div>
</div> </div>
<div class="card bg-black border-secondary text-light"> {# ========================================================= #}
{# Tag Table #}
{# ========================================================= #}
<div class="card bg-black border-secondary text-light shadow-sm">
<div class="card-body p-0"> <div class="card-body p-0">
<div class="px-3 py-2 border-bottom border-secondary">
<strong>Vorhandene Tags</strong>
<span class="text-muted small ms-2">
{{ tags|length }} Einträge
</span>
</div>
<table class="table table-dark table-striped table-hover mb-0 align-middle"> <table class="table table-dark table-striped table-hover mb-0 align-middle">
<thead class="table-secondary text-dark"> <thead class="table-secondary text-dark">
<tr> <tr>
<th>Label</th> <th style="width: 25%">Label</th>
<th>Slug</th> <th style="width: 25%">Slug</th>
<th>Beschreibung</th> <th style="width: 35%">Beschreibung</th>
<th class="text-end">Aktion</th> <th class="text-end" style="width: 15%">Aktion</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{% for tag in tags %} {% for tag in tags %}
<tr> <tr>
<td>{{ tag.label }}</td> <td class="fw-semibold">{{ tag.label }}</td>
<td><code>{{ tag.slug }}</code></td> <td><code>{{ tag.slug }}</code></td>
<td class="text-light">{{ tag.description ?: '' }}</td> <td>{{ tag.description ?: '-' }}</td>
<td class="text-end"> <td class="text-end">
<form method="post" action="{{ path('admin_tags_delete', {id: tag.id}) }}" style="display:inline"> <form method="post"
<input type="hidden" name="_token" value="{{ csrf_token('admin_tag_delete_' ~ tag.id) }}"/> action="{{ path('admin_tags_delete', {id: tag.id}) }}">
<button class="btn btn-sm btn-outline-danger" type="submit" <input type="hidden"
onclick="return confirm('Tag wirklich löschen? (Zuweisungen werden mit gelöscht)')"> name="_token"
value="{{ csrf_token('admin_tag_delete_' ~ tag.id) }}"/>
<button class="btn btn-sm btn-outline-danger"
onclick="return confirm('Tag wirklich löschen? Zuweisungen werden entfernt.')">
Löschen Löschen
</button> </button>
</form> </form>
</td> </td>
</tr> </tr>
{% else %} {% else %}
<tr><td colspan="4" class="text-light p-3">Noch keine Tags vorhanden.</td></tr> <tr>
<td colspan="4" class="p-4 text-center text-muted">
Noch keine Tags vorhanden.
</td>
</tr>
{% endfor %} {% endfor %}
</tbody> </tbody>
</table> </table>
</div> </div>
</div> </div>