Add check and cleanup for Orphaned sending task subscribers
This commit is contained in:
committed by
Aschepikov
parent
34feae9f1f
commit
bbed9c35d2
@@ -19,6 +19,10 @@ export function DataInconsistencies({ dataInconsistencies }: Props) {
|
|||||||
const labelsMap = useMemo(
|
const labelsMap = useMemo(
|
||||||
() => ({
|
() => ({
|
||||||
orphaned_sending_tasks: __('Orphaned Sending Tasks', 'mailpoet'),
|
orphaned_sending_tasks: __('Orphaned Sending Tasks', 'mailpoet'),
|
||||||
|
orphaned_sending_task_subscribers: __(
|
||||||
|
'Orphaned Sending Task Subscribers',
|
||||||
|
'mailpoet',
|
||||||
|
),
|
||||||
}),
|
}),
|
||||||
[],
|
[],
|
||||||
);
|
);
|
||||||
|
@@ -6,9 +6,11 @@ use MailPoet\UnexpectedValueException;
|
|||||||
|
|
||||||
class DataInconsistencyController {
|
class DataInconsistencyController {
|
||||||
const ORPHANED_SENDING_TASKS = 'orphaned_sending_tasks';
|
const ORPHANED_SENDING_TASKS = 'orphaned_sending_tasks';
|
||||||
|
const ORPHANED_SENDING_TASK_SUBSCRIBERS = 'orphaned_sending_task_subscribers';
|
||||||
|
|
||||||
const SUPPORTED_INCONSISTENCY_CHECKS = [
|
const SUPPORTED_INCONSISTENCY_CHECKS = [
|
||||||
self::ORPHANED_SENDING_TASKS,
|
self::ORPHANED_SENDING_TASKS,
|
||||||
|
self::ORPHANED_SENDING_TASK_SUBSCRIBERS,
|
||||||
];
|
];
|
||||||
|
|
||||||
private DataInconsistencyRepository $repository;
|
private DataInconsistencyRepository $repository;
|
||||||
@@ -22,6 +24,7 @@ class DataInconsistencyController {
|
|||||||
public function getInconsistentDataStatus(): array {
|
public function getInconsistentDataStatus(): array {
|
||||||
$result = [
|
$result = [
|
||||||
self::ORPHANED_SENDING_TASKS => $this->repository->getOrphanedSendingTasksCount(),
|
self::ORPHANED_SENDING_TASKS => $this->repository->getOrphanedSendingTasksCount(),
|
||||||
|
self::ORPHANED_SENDING_TASK_SUBSCRIBERS => $this->repository->getOrphanedScheduledTasksSubscribersCount(),
|
||||||
];
|
];
|
||||||
$result['total'] = array_sum($result);
|
$result['total'] = array_sum($result);
|
||||||
return $result;
|
return $result;
|
||||||
@@ -33,7 +36,8 @@ class DataInconsistencyController {
|
|||||||
}
|
}
|
||||||
if ($inconsistency === self::ORPHANED_SENDING_TASKS) {
|
if ($inconsistency === self::ORPHANED_SENDING_TASKS) {
|
||||||
$this->repository->cleanupOrphanedSendingTasks();
|
$this->repository->cleanupOrphanedSendingTasks();
|
||||||
return;
|
} elseif ($inconsistency === self::ORPHANED_SENDING_TASK_SUBSCRIBERS) {
|
||||||
|
$this->repository->cleanupOrphanedScheduledTaskSubscribers();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -24,6 +24,17 @@ class DataInconsistencyRepository {
|
|||||||
return (int)$this->buildOrphanedSendingTasksQuery($builder)->getSingleScalarResult();
|
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 {
|
public function cleanupOrphanedSendingTasks(): int {
|
||||||
$ids = $this->buildOrphanedSendingTasksQuery(
|
$ids = $this->buildOrphanedSendingTasksQuery(
|
||||||
$this->entityManager->createQueryBuilder()
|
$this->entityManager->createQueryBuilder()
|
||||||
@@ -53,6 +64,16 @@ class DataInconsistencyRepository {
|
|||||||
return $countDeletedTasks;
|
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 {
|
private function buildOrphanedSendingTasksQuery(QueryBuilder $queryBuilder): Query {
|
||||||
return $queryBuilder
|
return $queryBuilder
|
||||||
->from(ScheduledTaskEntity::class, 'st')
|
->from(ScheduledTaskEntity::class, 'st')
|
||||||
|
@@ -60,4 +60,20 @@ class DataInconsistencyRepositoryTest extends \MailPoetTest {
|
|||||||
$taskSubscriberCount = $this->entityManager->getRepository(ScheduledTaskSubscriberEntity::class)->count([]);
|
$taskSubscriberCount = $this->entityManager->getRepository(ScheduledTaskSubscriberEntity::class)->count([]);
|
||||||
verify($taskSubscriberCount)->equals(0);
|
verify($taskSubscriberCount)->equals(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testItHandlesOrphanedScheduledTaskSubscribers(): void {
|
||||||
|
$taskWithSubscriber = (new ScheduledTask())->create(SendingQueueWorker::TASK_TYPE, ScheduledTaskEntity::STATUS_SCHEDULED);
|
||||||
|
|
||||||
|
$subscriber1 = (new Subscriber())->create();
|
||||||
|
(new ScheduledTaskSubscriber())->createProcessed($taskWithSubscriber, $subscriber1);
|
||||||
|
$subscriber2 = (new Subscriber())->create();
|
||||||
|
(new ScheduledTaskSubscriber())->createProcessed($taskWithSubscriber, $subscriber2);
|
||||||
|
|
||||||
|
$this->entityManager->remove($taskWithSubscriber);
|
||||||
|
$this->entityManager->flush();
|
||||||
|
|
||||||
|
verify($this->repository->getOrphanedScheduledTasksSubscribersCount())->equals(2);
|
||||||
|
$this->repository->cleanupOrphanedScheduledTaskSubscribers();
|
||||||
|
verify($this->repository->getOrphanedScheduledTasksSubscribersCount())->equals(0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user