Add check and cleanup for Orphaned sending task subscribers

This commit is contained in:
Rostislav Wolny
2024-08-01 12:38:23 +02:00
committed by Aschepikov
parent 34feae9f1f
commit bbed9c35d2
4 changed files with 46 additions and 1 deletions

View File

@@ -6,9 +6,11 @@ use MailPoet\UnexpectedValueException;
class DataInconsistencyController {
const ORPHANED_SENDING_TASKS = 'orphaned_sending_tasks';
const ORPHANED_SENDING_TASK_SUBSCRIBERS = 'orphaned_sending_task_subscribers';
const SUPPORTED_INCONSISTENCY_CHECKS = [
self::ORPHANED_SENDING_TASKS,
self::ORPHANED_SENDING_TASK_SUBSCRIBERS,
];
private DataInconsistencyRepository $repository;
@@ -22,6 +24,7 @@ class DataInconsistencyController {
public function getInconsistentDataStatus(): array {
$result = [
self::ORPHANED_SENDING_TASKS => $this->repository->getOrphanedSendingTasksCount(),
self::ORPHANED_SENDING_TASK_SUBSCRIBERS => $this->repository->getOrphanedScheduledTasksSubscribersCount(),
];
$result['total'] = array_sum($result);
return $result;
@@ -33,7 +36,8 @@ class DataInconsistencyController {
}
if ($inconsistency === self::ORPHANED_SENDING_TASKS) {
$this->repository->cleanupOrphanedSendingTasks();
return;
} elseif ($inconsistency === self::ORPHANED_SENDING_TASK_SUBSCRIBERS) {
$this->repository->cleanupOrphanedScheduledTaskSubscribers();
}
}
}

View File

@@ -24,6 +24,17 @@ class DataInconsistencyRepository {
return (int)$this->buildOrphanedSendingTasksQuery($builder)->getSingleScalarResult();
}
public function getOrphanedScheduledTasksSubscribersCount(): int {
$stTable = $this->entityManager->getClassMetadata(ScheduledTaskEntity::class)->getTableName();
$stsTable = $this->entityManager->getClassMetadata(ScheduledTaskSubscriberEntity::class)->getTableName();
$count = $this->entityManager->getConnection()->executeQuery("
SELECT count(*) FROM $stsTable sts
LEFT JOIN $stTable st ON st.`id` = sts.`task_id`
WHERE st.`id` IS NULL
")->fetchOne();
return intval($count);
}
public function cleanupOrphanedSendingTasks(): int {
$ids = $this->buildOrphanedSendingTasksQuery(
$this->entityManager->createQueryBuilder()
@@ -53,6 +64,16 @@ class DataInconsistencyRepository {
return $countDeletedTasks;
}
public function cleanupOrphanedScheduledTaskSubscribers(): int {
$stTable = $this->entityManager->getClassMetadata(ScheduledTaskEntity::class)->getTableName();
$stsTable = $this->entityManager->getClassMetadata(ScheduledTaskSubscriberEntity::class)->getTableName();
return (int)$this->entityManager->getConnection()->executeStatement("
DELETE sts FROM $stsTable sts
LEFT JOIN $stTable st ON st.`id` = sts.`task_id`
WHERE st.`id` IS NULL
");
}
private function buildOrphanedSendingTasksQuery(QueryBuilder $queryBuilder): Query {
return $queryBuilder
->from(ScheduledTaskEntity::class, 'st')