diff --git a/lib/Cron/Workers/StatsNotifications/StatsNotificationsRepository.php b/lib/Cron/Workers/StatsNotifications/StatsNotificationsRepository.php index cc2954dc8b..41bdebcfc6 100644 --- a/lib/Cron/Workers/StatsNotifications/StatsNotificationsRepository.php +++ b/lib/Cron/Workers/StatsNotifications/StatsNotificationsRepository.php @@ -55,4 +55,14 @@ class StatsNotificationsRepository extends Repository { } return $query->getQuery()->getResult(); } + + public function deleteOrphanedScheduledTasks() { + $scheduledTasksTable = $this->entityManager->getClassMetadata(ScheduledTaskEntity::class)->getTableName(); + $statsNotificationsTable = $this->entityManager->getClassMetadata(StatsNotificationEntity::class)->getTableName(); + $this->entityManager->getConnection()->executeUpdate(" + DELETE st FROM $scheduledTasksTable st + LEFT JOIN $statsNotificationsTable sn ON sn.task_id = st.id + WHERE sn.id IS NULL AND st.type = :taskType; + ", ['taskType' => Worker::TASK_TYPE]); + } } diff --git a/lib/Cron/Workers/StatsNotifications/Worker.php b/lib/Cron/Workers/StatsNotifications/Worker.php index 309db8a04b..88fce88fc1 100644 --- a/lib/Cron/Workers/StatsNotifications/Worker.php +++ b/lib/Cron/Workers/StatsNotifications/Worker.php @@ -89,6 +89,8 @@ class Worker { public function process($timer = false) { $timer = $timer ?: microtime(true); $settings = $this->settings->get(self::SETTINGS_KEY); + // Cleanup potential orphaned task created due bug MAILPOET-3015 + $this->repository->deleteOrphanedScheduledTasks(); foreach ($this->repository->findScheduled(Sending::RESULT_BATCH_SIZE) as $statsNotificationEntity) { try { $extraParams = [