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_20260623_120000_App.php
75 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Migrations\App; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Cron\CronWorkerScheduler; |
| 9 | use MailPoet\Cron\Workers\InactiveSubscribersMaintenance; |
| 10 | use MailPoet\Entities\ScheduledTaskEntity; |
| 11 | use MailPoet\Migrator\AppMigration; |
| 12 | use MailPoet\Settings\SettingsController; |
| 13 | use MailPoetVendor\Carbon\Carbon; |
| 14 | use MailPoetVendor\Doctrine\DBAL\ArrayParameterType; |
| 15 | use MailPoetVendor\Doctrine\DBAL\Connection; |
| 16 | |
| 17 | class Migration_20260623_120000_App extends AppMigration { |
| 18 | public function run(): void { |
| 19 | $connection = $this->container->get(Connection::class); |
| 20 | $scheduledTasksTable = $this->entityManager->getClassMetadata(ScheduledTaskEntity::class)->getTableName(); |
| 21 | |
| 22 | // Seed the email-count baseline from the last completed legacy task so the new worker |
| 23 | // continues incrementally instead of doing a full recount. Afterwards the worker only |
| 24 | // reads the setting -- it never looks at the legacy tasks again. |
| 25 | $this->seedEmailCountBaseline($connection, $scheduledTasksTable); |
| 26 | |
| 27 | $connection->executeStatement( |
| 28 | " |
| 29 | DELETE FROM {$scheduledTasksTable} |
| 30 | WHERE type IN (:types) |
| 31 | AND (status != 'completed' OR status IS NULL) |
| 32 | ", |
| 33 | [ |
| 34 | 'types' => [ |
| 35 | 'subscribers_email_count', |
| 36 | 'inactive_subscribers', |
| 37 | ], |
| 38 | ], |
| 39 | [ |
| 40 | 'types' => ArrayParameterType::STRING, |
| 41 | ] |
| 42 | ); |
| 43 | |
| 44 | $scheduler = $this->container->get(CronWorkerScheduler::class); |
| 45 | $scheduler->schedule( |
| 46 | InactiveSubscribersMaintenance::TASK_TYPE, |
| 47 | Carbon::now()->millisecond(0)->addHour() |
| 48 | ); |
| 49 | } |
| 50 | |
| 51 | private function seedEmailCountBaseline(Connection $connection, string $scheduledTasksTable): void { |
| 52 | $lastCompletedScheduledAt = $connection->executeQuery( |
| 53 | " |
| 54 | SELECT scheduled_at FROM {$scheduledTasksTable} |
| 55 | WHERE type = 'subscribers_email_count' |
| 56 | AND status = 'completed' |
| 57 | AND deleted_at IS NULL |
| 58 | AND scheduled_at IS NOT NULL |
| 59 | ORDER BY scheduled_at DESC |
| 60 | LIMIT 1 |
| 61 | " |
| 62 | )->fetchOne(); |
| 63 | |
| 64 | if (!is_string($lastCompletedScheduledAt) || $lastCompletedScheduledAt === '') { |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | $settings = $this->container->get(SettingsController::class); |
| 69 | $settings->set( |
| 70 | InactiveSubscribersMaintenance::LAST_EMAIL_COUNT_AT_SETTING, |
| 71 | Carbon::parse($lastCompletedScheduledAt)->format(\DateTimeInterface::ATOM) |
| 72 | ); |
| 73 | } |
| 74 | } |
| 75 |