Skip to content

Commit

Permalink
TracyHandler: add garbage collection
Browse files Browse the repository at this point in the history
  • Loading branch information
VojtaStanek committed Jul 29, 2024
1 parent 22f574c commit b503d7d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
33 changes: 32 additions & 1 deletion src/TracyHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class TracyHandler extends AbstractProcessingHandler

private ?array $lastContext = null;

private const UPLOADED_FILE_CONTENTS = 'Uploaded to remote storage.';


public function __construct(
private string $localBlueScreenDirectory,
Expand Down Expand Up @@ -56,11 +58,15 @@ protected function write(LogRecord $record): void
if ($this->remoteStorageDriver !== null) {
$uploaded = $this->remoteStorageDriver->upload($localPath);
if ($uploaded && $this->removeUploads) {
file_put_contents($localPath, 'Uploaded to remote storage.');
file_put_contents($localPath, self::UPLOADED_FILE_CONTENTS);
}
}
}

if ($this->removeUploads && $this->remoteStorageDriver !== null) {
$this->maybeRunGarbageCollection();
}

$this->lastMessage = null;
$this->lastContext = null;
}
Expand All @@ -87,4 +93,29 @@ public function renderPsrLogPanel(?Throwable $e): ?array
'panel' => "$messageHtml\n$contextHtml",
];
}


private function maybeRunGarbageCollection(): void
{
if (rand(0, 100) !== 0) {
return;
}

$deleteOlderThan = new \DateTimeImmutable("-2 days");

foreach (scandir($this->localBlueScreenDirectory) as $file) {
$filePath = "{$this->localBlueScreenDirectory}/{$file}";
if (!is_file($filePath)) {
continue;
}

$date = TracyProcessor::getDateFromFileName($file);
if ($date !== null && $date < $deleteOlderThan) {
$fileContents = @file_get_contents($filePath);
if ($fileContents === self::UPLOADED_FILE_CONTENTS) {
unlink($filePath);
}
}
}
}
}
11 changes: 11 additions & 0 deletions src/TracyProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,15 @@ static function (array $item): array {
$hash = substr(md5(serialize($data)), 0, 10);
return "exception--$date--$hash.html";
}


public static function getDateFromFileName(string $fileName): ?\DateTimeImmutable
{
$matches = [];
if (!preg_match('/^exception--(?P<date>\d{4}-\d{2}-\d{2})--\w{10}\.html$/', $fileName, $matches)) {
return null;
}

return \DateTimeImmutable::createFromFormat('Y-m-d', $matches['date']);
}
}
15 changes: 15 additions & 0 deletions tests/unit/TracyProcessorTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,21 @@ use Tester\TestCase;
}


public function testFileNameParsing(): void
{
Assert::same('2018-10-09', TracyProcessor::getDateFromFileName('exception--2018-10-09--96577eb4c8.html')->format('Y-m-d'));

Assert::null(TracyProcessor::getDateFromFileName('exception--2018-10-09--96577eb4c8EXTRA.html'));
Assert::null(TracyProcessor::getDateFromFileName('exception--2018-10-09--96577eb4c8'));
Assert::null(TracyProcessor::getDateFromFileName('exception--2018-10-09--96577eb4c8.'));
Assert::null(TracyProcessor::getDateFromFileName('exception--2018-10-09--96577eb4c8.json'));
Assert::null(TracyProcessor::getDateFromFileName('.'));
Assert::null(TracyProcessor::getDateFromFileName('directory'));
Assert::null(TracyProcessor::getDateFromFileName('exception--2018-10-09--96577eb4c8.htmlEXTRA'));
Assert::null(TracyProcessor::getDateFromFileName('EXTRAexception--2018-10-09--96577eb4c8.html'));
}


private function createException(): \Exception
{
$exception = new \Exception();
Expand Down

0 comments on commit b503d7d

Please sign in to comment.