Unschedule cron actions before running migrations

We don't want to run any actions during migrations.
This will also clean duplicate trigger actions
that were potentially created because of requests race conditions.
[MAILPOET-4684]
This commit is contained in:
Rostislav Wolny
2022-09-28 14:10:59 +02:00
committed by Aschepikov
parent 1711d60871
commit b96b549f43
2 changed files with 27 additions and 2 deletions

View File

@ -2,6 +2,8 @@
namespace MailPoet\Config;
use MailPoet\Cron\ActionScheduler\ActionScheduler as CronActionScheduler;
use MailPoet\Cron\CronTrigger;
use MailPoet\InvalidStateException;
use MailPoet\Settings\SettingsController;
use MailPoet\WP\Functions as WPFunctions;
@ -22,16 +24,21 @@ class Activator {
/** @var Migrator */
private $migrator;
/** @var CronActionScheduler */
private $cronActionSchedulerRunner;
public function __construct(
SettingsController $settings,
Populator $populator,
WPFunctions $wp,
Migrator $migrator
Migrator $migrator,
CronActionScheduler $cronActionSchedulerRunner
) {
$this->settings = $settings;
$this->populator = $populator;
$this->wp = $wp;
$this->migrator = $migrator;
$this->cronActionSchedulerRunner = $cronActionSchedulerRunner;
}
public function activate() {
@ -58,6 +65,7 @@ class Activator {
private function processActivate(): void {
$this->migrator->up();
$this->deactivateCronActions();
$this->populator->up();
$this->updateDbVersion();
@ -78,6 +86,21 @@ class Activator {
$this->unlockActivation();
}
/**
* Deactivate action scheduler cron actions when the migration run.
* This should prevent processing actions during migrations.
* They are later re-activated in CronTrigger
*
* @return void
*/
private function deactivateCronActions(): void {
$currentMethod = $this->settings->get(CronTrigger::SETTING_NAME . '.method');
if ($currentMethod !== CronTrigger::METHOD_ACTION_SCHEDULER) {
return;
}
$this->cronActionSchedulerRunner->unscheduleAllCronActions();
}
public function updateDbVersion() {
try {
$currentDbVersion = $this->settings->get('db_version');

View File

@ -9,6 +9,7 @@ use MailPoet\API\JSON\v1\Setup;
use MailPoet\Config\Activator;
use MailPoet\Config\Migrator;
use MailPoet\Config\Populator;
use MailPoet\Cron\ActionScheduler\ActionScheduler;
use MailPoet\Form\FormsRepository;
use MailPoet\Referrals\ReferralDetector;
use MailPoet\Segments\WP;
@ -34,7 +35,8 @@ class SetupTest extends \MailPoetTest {
$subscriptionCaptcha = $this->diContainer->get(Captcha::class);
$populator = $this->getServiceWithOverrides(Populator::class, ['wp' => $wpStub, 'referralDetector' => $referralDetector]);
$migrator = $this->diContainer->get(Migrator::class);
$router = new Setup($wpStub, new Activator($settings, $populator, $wpStub, $migrator));
$cronActionScheduler = $this->diContainer->get(ActionScheduler::class);
$router = new Setup($wpStub, new Activator($settings, $populator, $wpStub, $migrator, $cronActionScheduler));
$response = $router->reset();
expect($response->status)->equals(APIResponse::STATUS_OK);