From 6874fc5a0a40e6812cc1c7429d893a083038cce1 Mon Sep 17 00:00:00 2001 From: Vaidas Bagdonas Date: Fri, 22 Nov 2019 17:11:49 +0100 Subject: [PATCH] add old message removal command --- README.md | 3 - config/services.yaml | 2 +- src/Command/CleanMessagesCommand.php | 73 +++++++++++++++++++ .../Factory/BroadcastMessagePathFactory.php | 2 +- src/Services/NextMessageSelector.php | 10 ++- 5 files changed, 83 insertions(+), 7 deletions(-) create mode 100644 src/Command/CleanMessagesCommand.php diff --git a/README.md b/README.md index 7cf66f3..f1e8d6d 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,3 @@ Routes are as follows: Automatic listening for unprocessed messages & marking as processed afterwards. [salesforce-outbound-message-tower-bundle](https://github.com/comsave/salesforce-outbound-message-tower-bundle) - -## To do -* cleanup command for notifications older than X diff --git a/config/services.yaml b/config/services.yaml index fdfe97e..a11c95d 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -1,5 +1,5 @@ parameters: - broadcast_messages_dir: '../%env(APP_BROADCAST_MESSAGES_DIR)%' + broadcast_messages_dir: '%kernel.project_dir%/%env(APP_BROADCAST_MESSAGES_DIR)%' services: _defaults: diff --git a/src/Command/CleanMessagesCommand.php b/src/Command/CleanMessagesCommand.php new file mode 100644 index 0000000..169046a --- /dev/null +++ b/src/Command/CleanMessagesCommand.php @@ -0,0 +1,73 @@ +nextMessageSelector = $nextMessageSelector; + $this->processedMessageRemover = $processedMessageRemover; + } + + protected function configure() + { + $this + ->setDescription('Removes old unprocessed outbound messages.') + ->addArgument('maxDays', InputArgument::OPTIONAL, 'Maximum age of unprocessed messages in days.', 30); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $io = new SymfonyStyle($input, $output); + $maxDays = $input->getArgument('maxDays'); + + $removedMessageCount = $this->removeOldUnprocessedMessages($maxDays); + + $io->success(sprintf('%s messages removed.', $removedMessageCount)); + + return 0; + } + + public function removeOldUnprocessedMessages(int $maxDaysOld): int + { + $beforeTimestamp = (new \DateTime('now'))->modify(sprintf('-%s day', $maxDaysOld))->getTimestamp(); + $allMessageFilePaths = $this->nextMessageSelector->getAllMessageFiles('*'); + $removedMessageCount = 0; + + foreach ($allMessageFilePaths as $messageFilePath) { + if (filemtime($messageFilePath) < $beforeTimestamp) { + $messageFilePathData = explode('/', $messageFilePath); + + $notificationId = array_pop($messageFilePathData); + $notificationId = explode('.', basename($notificationId))[0]; + + $channelName = array_pop($messageFilePathData); + + $this->processedMessageRemover->remove($channelName, $notificationId); + + $removedMessageCount++; + } + } + + return $removedMessageCount; + } +} diff --git a/src/Services/Factory/BroadcastMessagePathFactory.php b/src/Services/Factory/BroadcastMessagePathFactory.php index a2f89b5..f52c3a5 100644 --- a/src/Services/Factory/BroadcastMessagePathFactory.php +++ b/src/Services/Factory/BroadcastMessagePathFactory.php @@ -16,7 +16,7 @@ public function getMessageDirectory(string $channelName): string { $messageDir = sprintf('%s/%s', $this->broadcastMessagesDir, $channelName); - if (!file_exists($messageDir)) { + if ($channelName !== '*' && !file_exists($messageDir)) { mkdir($messageDir); } diff --git a/src/Services/NextMessageSelector.php b/src/Services/NextMessageSelector.php index 470143b..8cf688a 100644 --- a/src/Services/NextMessageSelector.php +++ b/src/Services/NextMessageSelector.php @@ -26,12 +26,18 @@ public function nextMessage(string $channelName): ?string public function nextMessageFile(string $channelName): ?string { - $messageFiles = glob($this->broadcastMessageFactory->getMessageFilePath($channelName, '*')); + return $this->getAllMessageFiles($channelName)[0] ?? null; + } + + public function getAllMessageFiles(string $channelName): array + { + $messageDir = $this->broadcastMessageFactory->getMessageFilePath($channelName, '*'); + $messageFiles = glob($messageDir) ?: []; usort($messageFiles, function (string $messageFileA, string $messageFileB) { return filemtime($messageFileA) > filemtime($messageFileB); }); - return $messageFiles[0] ?? null; + return $messageFiles; } } \ No newline at end of file