-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLogCleaner.php
More file actions
130 lines (111 loc) · 2.9 KB
/
LogCleaner.php
File metadata and controls
130 lines (111 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php declare(strict_types=1);
namespace Torr\TaskManager\Log;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Clock\ClockInterface;
use Torr\TaskManager\Entity\TaskLog;
use Torr\TaskManager\Entity\TaskRun;
final readonly class LogCleaner
{
public function __construct (
private int $logTtlInDays,
private int $logMaxEntries,
private EntityManagerInterface $entityManager,
private ClockInterface $clock,
) {}
/**
* @return int the number of deleted tasks
*/
public function cleanLogEntries () : int
{
$taskIdToDelete = $this->fetchIdsToDelete();
if (empty($taskIdToDelete))
{
return 0;
}
// first delete runs, as they are a foreign key on the task logs
$this->deleteRuns($taskIdToDelete);
// then delete tasks
$this->deleteTasks($taskIdToDelete);
return \count($taskIdToDelete);
}
/**
*
*/
private function deleteRuns (array $taskIdsToDelete) : void
{
$this->entityManager->createQueryBuilder()
->delete()
->from(TaskRun::class, "run")
->andWhere("IDENTITY(run.taskLog) IN (:taskIds)")
->setParameter("taskIds", $taskIdsToDelete)
->getQuery()
->execute();
}
/**
*
*/
private function deleteTasks (array $taskIdsToDelete) : void
{
$this->entityManager->createQueryBuilder()
->delete()
->from(TaskLog::class, "task")
->andWhere("task.id IN (:taskIds)")
->setParameter("taskIds", $taskIdsToDelete)
->getQuery()
->execute();
}
/**
*/
private function fetchIdsToDelete () : array
{
// start with a fixed TTL
$purgeBefore = $this->clock->now()
->sub(new \DateInterval("P{$this->logTtlInDays}D"));
// check whether the last entry at "max entries" would be newer than the
// TTL. If so, then adjust the purge date to fulfill both
$cutOffEntry = $this->getCutoffEntry($this->logMaxEntries);
if (null !== $cutOffEntry && $cutOffEntry->timeQueued > $purgeBefore)
{
$purgeBefore = $cutOffEntry->timeQueued;
}
$rows = $this->entityManager->createQueryBuilder()
->select("distinct task.id")
->from(TaskLog::class, "task")
->leftJoin("task.runs", "run")
->where("task.timeQueued <= :oldestTimestamp")
->setParameter("oldestTimestamp", $purgeBefore)
->getQuery()
->getArrayResult();
return array_column($rows, "id");
}
/**
*
*/
private function getCutoffEntry (int $maxEntries) : ?TaskLog
{
/** @var TaskLog[] $result */
$result = $this->entityManager->createQueryBuilder()
->select("task")
->from(TaskLog::class, "task")
->addOrderBy("task.timeQueued", "DESC")
->setFirstResult($maxEntries)
->setMaxResults(1)
->getQuery()
->getResult();
return $result[0] ?? null;
}
/**
* Returns the maximum age of log entries to keep (in days)
*/
public function getMaxLogEntryAge () : int
{
return $this->logTtlInDays;
}
/**
* Returns the maximum number of log entries to keep
*/
public function getMaxLogEntryNumber () : int
{
return $this->logMaxEntries;
}
}