Skip to content
This repository has been archived by the owner on Aug 20, 2020. It is now read-only.

Commit

Permalink
add old message removal command
Browse files Browse the repository at this point in the history
  • Loading branch information
Vaidas Bagdonas committed Nov 22, 2019
1 parent 4cf7328 commit 6874fc5
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 7 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion config/services.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
73 changes: 73 additions & 0 deletions src/Command/CleanMessagesCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace App\Command;

use App\Services\NextMessageSelector;
use App\Services\ProcessedMessageRemover;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

class CleanMessagesCommand extends Command
{
protected static $defaultName = 'app:clean:messages';

/** @var NextMessageSelector */
private $nextMessageSelector;

/** @var ProcessedMessageRemover */
private $processedMessageRemover;

public function __construct(NextMessageSelector $nextMessageSelector, ProcessedMessageRemover $processedMessageRemover)
{
parent::__construct();

$this->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;
}
}
2 changes: 1 addition & 1 deletion src/Services/Factory/BroadcastMessagePathFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
10 changes: 8 additions & 2 deletions src/Services/NextMessageSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 6874fc5

Please sign in to comment.