Skip to content

Commit

Permalink
feat: add cleanup for inactive redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
tinect committed Jul 8, 2024
1 parent f6ec01f commit 004e08b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/Resources/config/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@
<defaultValue>30</defaultValue>
</input-field>

<input-field type="int">
<name>deleteInactiveEntriesAfterDays</name>
<label>Delete inactive redirects after x days</label>
<label lang="de-DE">Inaktive Weiterleitungen löschen, die älter als X Tage sind</label>
<defaultValue>30</defaultValue>
</input-field>

<input-field type="int">
<name>deleteInactiveEntriesCount</name>
<label>Delete inactive redirects with less than x count</label>
<label lang="de-DE">Inaktive Weiterleitungen löschen, die weniger als X Aufrufe haben</label>
<defaultValue>2</defaultValue>
</input-field>

<input-field type="bool">
<name>saveIpAddresses</name>
<label>Save IP addresses</label>
Expand Down
54 changes: 54 additions & 0 deletions src/Task/CleanupTaskHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Shopware\Core\Framework\MessageQueue\ScheduledTask\ScheduledTaskHandler;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Tinect\Redirects\Content\Redirect\RedirectDefinition;
use Tinect\Redirects\Content\Redirect\RedirectRequestDefinition;

#[AsMessageHandler(handles: CleanupTask::class)]
Expand All @@ -27,6 +28,12 @@ public function __construct(
}

public function run(): void
{
$this->cleanupRequests();
$this->cleanupInactiveRedirects();
}

private function cleanupRequests(): void
{
$days = $this->configService->getInt('TinectRedirects.config.deleteRequestsAfterDays');

Expand Down Expand Up @@ -67,4 +74,51 @@ public function run(): void
`count` = (SELECT COUNT(*) FROM tinect_redirects_redirect_request WHERE tinect_redirects_redirect_request.tinect_redirects_redirect_id = tinect_redirects_redirect.id)
');
}

private function cleanupInactiveRedirects(): void
{
$days = $this->configService->getInt('TinectRedirects.config.deleteInactiveEntriesAfterDays');

if ($days === 0) {
$days = 30;
}

$count = $this->configService->getInt('TinectRedirects.config.deleteInactiveEntriesCount');

if ($count === 0) {
$count = 2;
}

$time = new \DateTime();
$time->modify(sprintf('-%s days', $days));

$query = $this->connection->createQueryBuilder();

$query->select('id');
$query->from(RedirectDefinition::ENTITY_NAME);
$query->where('`active` = 0');
$query->andWhere('`count` < :count');
$query->andWhere(
$query->expr()->lte(
'created_at',
$query->createNamedParameter($time->format(Defaults::STORAGE_DATE_TIME_FORMAT))
)
);
$query->setParameter('count', $count);

$ids = $query->executeQuery()->fetchFirstColumn();

if (\count($ids) === 0) {
return;
}

$deleteQuery = $this->connection->createQueryBuilder();
$deleteQuery->delete(RedirectDefinition::ENTITY_NAME);
$deleteQuery->where('id IN (:ids)');

foreach (\array_chunk($ids, 1000) as $chunk) {
$deleteQuery->setParameter('ids', $chunk, ArrayParameterType::STRING);
$deleteQuery->executeQuery();
}
}
}

0 comments on commit 004e08b

Please sign in to comment.