-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from 21TORR/log-cleaner
- Loading branch information
Showing
11 changed files
with
245 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Torr\TaskManager\Listener; | ||
|
||
use Symfony\Component\EventDispatcher\Attribute\AsEventListener; | ||
use Torr\TaskManager\Event\RegisterTasksEvent; | ||
use Torr\TaskManager\Log\Task\CleanOutdatedLogsTask; | ||
|
||
final readonly class TaskIntegrationListener | ||
{ | ||
#[AsEventListener] | ||
public function onRegisterTasks (RegisterTasksEvent $event) : void | ||
{ | ||
$event->register(new CleanOutdatedLogsTask()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Torr\TaskManager\Log; | ||
|
||
use Torr\TaskManager\Model\TaskLogModel; | ||
|
||
final readonly class LogCleaner | ||
{ | ||
public function __construct ( | ||
private int $logTtlInDays, | ||
private TaskLogModel $model, | ||
) {} | ||
|
||
/** | ||
* @return string[] labels for the removed tasks | ||
*/ | ||
public function cleanLogEntries () : array | ||
{ | ||
$deleted = []; | ||
|
||
foreach ($this->model->fetchOutdatedTasks($this->logTtlInDays) as $logEntry) | ||
{ | ||
$deleted[] = sprintf( | ||
"<fg=yellow>%s</> (%s)", | ||
$logEntry->getTaskLabel(), | ||
$logEntry->getTimeQueued()->format("c"), | ||
); | ||
|
||
$this->model->remove($logEntry); | ||
} | ||
|
||
$this->model->flush(); | ||
|
||
return $deleted; | ||
} | ||
|
||
/** | ||
* Returns the maximum age of log entries to keep (in days) | ||
*/ | ||
public function getMaxLogEntryAge () : int | ||
{ | ||
return $this->logTtlInDays; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Torr\TaskManager\Log\Task; | ||
|
||
use Torr\TaskManager\Task\Task; | ||
use Torr\TaskManager\Task\TaskMetaData; | ||
|
||
final readonly class CleanOutdatedLogsTask extends Task implements \Stringable | ||
{ | ||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getMetaData () : TaskMetaData | ||
{ | ||
return new TaskMetaData( | ||
label: "Clean log entries", | ||
group: "Task Manager", | ||
uniqueTaskId: "task-manager.clean-log", | ||
); | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
public function __toString () : string | ||
{ | ||
return $this->getMetaData()->label; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Torr\TaskManager\Log\Task; | ||
|
||
use Symfony\Component\Messenger\Attribute\AsMessageHandler; | ||
use Torr\TaskManager\Director\TaskDirector; | ||
use Torr\TaskManager\Log\LogCleaner; | ||
|
||
final readonly class CleanOutdatedLogsTaskHandler | ||
{ | ||
/** | ||
*/ | ||
public function __construct ( | ||
private LogCleaner $logCleaner, | ||
private TaskDirector $taskDirector, | ||
) {} | ||
|
||
/** | ||
*/ | ||
#[AsMessageHandler] | ||
public function onCleanOutdatedLogs (CleanOutdatedLogsTask $task) : void | ||
{ | ||
$run = $this->taskDirector->startRun($task); | ||
$io = $run->getIo(); | ||
|
||
$io->title("Task Manager: Cleaning Outdated Log Entries"); | ||
$io->comment(sprintf( | ||
"Cleaning log entries older than <fg=blue>%d days</>", | ||
$this->logCleaner->getMaxLogEntryAge(), | ||
)); | ||
|
||
$deletedEntries = $this->logCleaner->cleanLogEntries(); | ||
|
||
if ([] === $deletedEntries) | ||
{ | ||
$io->success("No entries to remove found"); | ||
$run->finish(true); | ||
|
||
return; | ||
} | ||
|
||
$io->writeln("Removed:"); | ||
$io->listing($deletedEntries); | ||
|
||
$io->success(sprintf( | ||
"Deleted <fg=yellow>%d</> %s:", | ||
\count($deletedEntries), | ||
1 !== \count($deletedEntries) | ||
? "entries" | ||
: "entry", | ||
)); | ||
|
||
$run->finish(true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Torr\TaskManager\Schedule; | ||
|
||
use Symfony\Component\Scheduler\Attribute\AsSchedule; | ||
use Symfony\Component\Scheduler\RecurringMessage; | ||
use Symfony\Component\Scheduler\Schedule; | ||
use Symfony\Component\Scheduler\ScheduleProviderInterface; | ||
use Torr\TaskManager\Log\Task\CleanOutdatedLogsTask; | ||
|
||
#[AsSchedule("task_manager")] | ||
final readonly class TaskManagerSchedule implements ScheduleProviderInterface | ||
{ | ||
/** | ||
* | ||
*/ | ||
public function getSchedule () : Schedule | ||
{ | ||
return (new Schedule()) | ||
->with( | ||
RecurringMessage::cron( | ||
"#daily", | ||
new CleanOutdatedLogsTask(), | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters