Skip to content

Commit 468e86e

Browse files
committed
3666: Add cleanup for detection results to only keep X results for each server/type combo
1 parent 1766563 commit 468e86e

3 files changed

Lines changed: 34 additions & 5 deletions

File tree

config/services.yaml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,13 @@ services:
2727
App\Handler\DetectionResultHandlerInterface:
2828
tags: [ app.handler.detection_result_handler ]
2929

30-
App\MessageHandler\ProcessDetectionResultHandler:
31-
arguments:
32-
$resultHandlers: !tagged_iterator app.handler.detection_result_handler
33-
3430
App\EventListener\RemovedRelationsListener:
3531
tags:
3632
-
3733
name: 'doctrine.event_listener'
3834
event: 'postFlush'
3935

40-
36+
App\MessageHandler\ProcessDetectionResultHandler:
37+
arguments:
38+
$resultHandlers: !tagged_iterator app.handler.detection_result_handler
39+
$keepResults: '%env(int:APP_KEEP_RESULTS)%'

src/MessageHandler/ProcessDetectionResultHandler.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public function __construct(
1717
private EntityManagerInterface $entityManager,
1818
private DetectionResultRepository $detectionResultRepository,
1919
private iterable $resultHandlers,
20+
private int $keepResults,
2021
) {
2122
}
2223

@@ -33,6 +34,9 @@ public function __invoke(ProcessDetectionResult $message): void
3334
}
3435

3536
$this->entityManager->flush();
37+
38+
$this->detectionResultRepository->cleanup($detectionResult, $this->keepResults, true);
39+
3640
$this->entityManager->clear();
3741
gc_collect_cycles();
3842
}

src/Repository/DetectionResultRepository.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,30 @@ public function remove(\DateTime $date): mixed
3939
->getQuery()
4040
->getResult();
4141
}
42+
43+
public function cleanup(DetectionResult $detectionResult, int $keep = 5, bool $flush = false): void
44+
{
45+
if ($keep < 1) {
46+
return;
47+
}
48+
49+
$em = $this->getEntityManager();
50+
51+
$results = $this->findBy(
52+
[
53+
'server' => $detectionResult->getServer(),
54+
'type' => $detectionResult->getType(),
55+
'rootDir' => $detectionResult->getRootDir(),
56+
],
57+
['lastContact' => 'DESC'], null, $keep
58+
);
59+
60+
foreach ($results as $result) {
61+
$em->remove($result);
62+
}
63+
64+
if ($flush) {
65+
$em->flush();
66+
}
67+
}
4268
}

0 commit comments

Comments
 (0)