Skip to content

Commit 1766563

Browse files
committed
3666: Set last contact at for servers
1 parent 8c46cd1 commit 1766563

4 files changed

Lines changed: 72 additions & 13 deletions

File tree

.env

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,6 @@ OIDC_CLI_LOGIN_ROUTE=OIDC_CLI_LOGIN_ROUTE_TEST
6060
VAULT_URL=https://vault.itkdev.dk
6161
VAULT_ROLE_ID="CHANGE_ME_IN_LOCAL_ENV"
6262
VAULT_SECRET_ID="CHANGE_ME_IN_LOCAL_ENV"
63+
64+
# The number of old results for each server/result-type combination
65+
APP_KEEP_RESULTS=5
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DoctrineMigrations;
6+
7+
use Doctrine\DBAL\Schema\Schema;
8+
use Doctrine\Migrations\AbstractMigration;
9+
10+
/**
11+
* Auto-generated Migration: Please modify to your needs!
12+
*/
13+
final class Version20250123132456 extends AbstractMigration
14+
{
15+
public function getDescription(): string
16+
{
17+
return '';
18+
}
19+
20+
public function up(Schema $schema): void
21+
{
22+
// this up() migration is auto-generated, please modify it to your needs
23+
$this->addSql('ALTER TABLE server ADD last_contact_at DATETIME NULL');
24+
}
25+
26+
public function down(Schema $schema): void
27+
{
28+
// this down() migration is auto-generated, please modify it to your needs
29+
$this->addSql('ALTER TABLE server DROP last_contact_at');
30+
}
31+
}

src/Entity/Server.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ class Server extends AbstractBaseEntity implements UserInterface, \Stringable
8585
#[ORM\Column(type: 'string', length: 10)]
8686
private string $type;
8787

88+
#[ORM\Column(type: 'datetime_immutable', nullable: true)]
89+
private ?\DateTimeImmutable $lastContactAt;
90+
8891
/**
8992
* @throws \Exception
9093
*/
@@ -389,4 +392,18 @@ public function setType(string $type): self
389392

390393
return $this;
391394
}
395+
396+
public function getLastContactAt(): ?\DateTimeImmutable
397+
{
398+
return $this->lastContactAt;
399+
}
400+
401+
public function updateLastContactAt(\DateTimeImmutable $lastContactAt): static
402+
{
403+
if (null === $this->lastContactAt || $this->lastContactAt < $lastContactAt) {
404+
$this->lastContactAt = $lastContactAt;
405+
}
406+
407+
return $this;
408+
}
392409
}

src/MessageHandler/PersistDetectionResultHandler.php

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use App\Repository\ServerRepository;
1111
use Doctrine\ORM\EntityManagerInterface;
1212
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
13+
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
1314
use Symfony\Component\Messenger\MessageBusInterface;
1415

1516
#[AsMessageHandler]
@@ -26,30 +27,37 @@ public function __construct(
2627
public function __invoke(PersistDetectionResult $message): void
2728
{
2829
$server = $this->serverRepository->findOneBy(['apiKey' => $message->serverApiKey]);
29-
$detectionResult = $message->detectionResult;
3030

31-
$detectionResult->setServer($server);
31+
if (null === $server) {
32+
throw new UnrecoverableMessageHandlingException('Server not found.');
33+
}
3234

33-
$hash = $detectionResult->generateHash()->getHash();
34-
$result = $this->detectionResultRepository->findOneBy(['server' => $server, 'hash' => $hash]);
35+
$server->updateLastContactAt($message->receivedAt);
3536

36-
if (null === $result) {
37-
$this->entityManager->persist($detectionResult);
38-
$detectionResult->setLastContact($message->receivedAt);
39-
} else {
37+
$result = $message->detectionResult;
38+
$result->setServer($server);
39+
40+
$hash = $result->generateHash()->getHash();
41+
$existingResult = $this->detectionResultRepository->findOneBy(['server' => $server, 'hash' => $hash]);
42+
43+
// New results should trigger cleanup and be sent to the queue for further processing.
44+
if (null === $existingResult) {
45+
$this->entityManager->persist($result);
4046
$result->setLastContact($message->receivedAt);
41-
}
4247

43-
$this->entityManager->flush();
48+
$this->entityManager->flush();
4449

45-
// New DetectionResults should be sent to processing
46-
if (null === $result) {
47-
$id = $detectionResult->getId();
50+
$id = $result->getId();
4851
if (null !== $id) {
4952
$this->messageBus->dispatch(
5053
new ProcessDetectionResult($id)
5154
);
5255
}
56+
// Existing results will only update timestamps
57+
} else {
58+
$existingResult->setLastContact($message->receivedAt);
59+
60+
$this->entityManager->flush();
5361
}
5462

5563
$this->entityManager->clear();

0 commit comments

Comments
 (0)