Migration_20221028_105818_App.php
3 years ago
Migration_20230109_144830.php
3 years ago
Migration_20230131_121621.php
3 years ago
Migration_20230419_080000.php
3 years ago
Migration_20230425_211517.php
3 years ago
Migration_20230712_180341.php
3 years ago
Migration_20230803_200413_App.php
2 years ago
Migration_20230825_093531_App.php
1 year ago
Migration_20231128_120355_App.php
1 year ago
Migration_20240202_130053_App.php
2 years ago
Migration_20240207_105912_App.php
1 year ago
Migration_20240322_110443_App.php
2 years ago
Migration_20240730_212419_App.php
2 years ago
Migration_20241015_105511_App.php
1 year ago
Migration_20241128_114257_App.php
1 year ago
Migration_20250120_094614_App.php
1 year ago
Migration_20250501_114655_App.php
11 months ago
Migration_20260421_155908_App.php
3 months ago
Migration_20260515_120000_App.php
3 months ago
Migration_20260623_120000_App.php
1 month ago
Migration_20260805_120000_App.php
1 week ago
index.php
3 years ago
Migration_20241128_114257_App.php
73 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Migrations\App; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Entities\NewsletterEntity; |
| 9 | use MailPoet\Entities\ScheduledTaskEntity; |
| 10 | use MailPoet\Entities\ScheduledTaskSubscriberEntity; |
| 11 | use MailPoet\Migrator\AppMigration; |
| 12 | use MailPoet\Newsletter\NewslettersRepository; |
| 13 | use MailPoetVendor\Carbon\Carbon; |
| 14 | |
| 15 | /** |
| 16 | * Some newsletters might have an incorrect status due to a bug where we set the status 'sending' |
| 17 | * to automation emails. |
| 18 | * |
| 19 | * See https://mailpoet.atlassian.net/browse/MAILPOET-6241 |
| 20 | */ |
| 21 | class Migration_20241128_114257_App extends AppMigration { |
| 22 | public function run(): void { |
| 23 | $newsletterRepository = $this->container->get(NewslettersRepository::class); |
| 24 | $newsletters = $newsletterRepository->findBy([ |
| 25 | 'type' => NewsletterEntity::ACTIVABLE_EMAILS, |
| 26 | 'status' => NewsletterEntity::STATUS_SENDING, |
| 27 | ]); |
| 28 | |
| 29 | foreach ($newsletters as $newsletter) { |
| 30 | $newsletter->setStatus(NewsletterEntity::STATUS_ACTIVE); |
| 31 | // As a consequence of the bug, some tasks might be paused, we need to unpause them |
| 32 | $this->updateTasks($newsletter); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | private function updateTasks(NewsletterEntity $newsletter): void { |
| 37 | $oldTaskThreshold = (new Carbon())->subDays(30); |
| 38 | $queues = $newsletter->getUnfinishedQueues(); |
| 39 | foreach ($queues as $queue) { |
| 40 | $task = $queue->getTask(); |
| 41 | |
| 42 | // Switch relatively new paused tasks to scheduled |
| 43 | if ($task && ($task->getScheduledAt() > $oldTaskThreshold) && $task->getStatus() === ScheduledTaskEntity::STATUS_PAUSED) { |
| 44 | $task->setStatus(ScheduledTaskEntity::STATUS_SCHEDULED); |
| 45 | $this->entityManager->flush(); |
| 46 | continue; |
| 47 | } |
| 48 | |
| 49 | // Switch old paused tasks to completed and mark scheduled task subscribers as failed |
| 50 | // This will prevent sending outdated automatic emails. When marked as failed, the user still can resend them in Sending Status screen. |
| 51 | if ($task && ($task->getScheduledAt() <= $oldTaskThreshold) && $task->getStatus() === ScheduledTaskEntity::STATUS_PAUSED) { |
| 52 | $task->setStatus(ScheduledTaskEntity::STATUS_COMPLETED); |
| 53 | $task->setProcessedAt(new Carbon()); |
| 54 | $this->entityManager->flush(); |
| 55 | $scheduledTaskSubscribersTable = $this->entityManager->getClassMetadata(ScheduledTaskSubscriberEntity::class)->getTableName(); |
| 56 | $this->entityManager->getConnection()->executeQuery( |
| 57 | "UPDATE $scheduledTaskSubscribersTable |
| 58 | SET `processed` = :processed, `failed` = :failed, `error` = :error |
| 59 | WHERE task_id = :task_id", |
| 60 | [ |
| 61 | 'processed' => ScheduledTaskSubscriberEntity::STATUS_PROCESSED, |
| 62 | 'failed' => ScheduledTaskSubscriberEntity::FAIL_STATUS_FAILED, |
| 63 | 'error' => 'Sending timed out for being paused too long.', |
| 64 | 'task_id' => $task->getId(), |
| 65 | ] |
| 66 | ); |
| 67 | $this->entityManager->refresh($task); |
| 68 | } |
| 69 | } |
| 70 | $this->entityManager->flush(); |
| 71 | } |
| 72 | } |
| 73 |