Use WP transient for lock running migrations

[MAILPOET-3537]
This commit is contained in:
Jan Lysý
2021-05-20 13:59:21 +02:00
committed by Veljko V
parent 86e89f7fc1
commit a0a0313c46
3 changed files with 31 additions and 2 deletions

View File

@ -2,9 +2,13 @@
namespace MailPoet\Config;
use MailPoet\InvalidStateException;
use MailPoet\Settings\SettingsController;
use MailPoet\WP\Functions as WPFunctions;
class Activator {
public const TRANSIENT_ACTIVATE_KEY = 'mailpoet_activator_activate';
private const TRANSIENT_EXPIRATION = 120;
/** @var SettingsController */
private $settings;
@ -12,12 +16,27 @@ class Activator {
/** @var Populator */
private $populator;
public function __construct(SettingsController $settings, Populator $populator) {
/** @var WPFunctions */
private $wp;
public function __construct(SettingsController $settings, Populator $populator, WPFunctions $wp) {
$this->settings = $settings;
$this->populator = $populator;
$this->wp = $wp;
}
public function activate() {
$isRunning = $this->wp->getTransient(self::TRANSIENT_ACTIVATE_KEY);
if ($isRunning === false) {
$this->wp->setTransient(self::TRANSIENT_ACTIVATE_KEY, '1', self::TRANSIENT_EXPIRATION);
$this->processActivate();
$this->wp->deleteTransient(self::TRANSIENT_ACTIVATE_KEY);
} else {
throw new InvalidStateException(__('MailPoet version update is in progress, please refresh the page in a minute.', 'mailpoet'));
}
}
private function processActivate(): void {
$migrator = new Migrator();
$migrator->up();

View File

@ -5,6 +5,7 @@ namespace MailPoet\Config;
use MailPoet\API\JSON\API;
use MailPoet\AutomaticEmails\AutomaticEmails;
use MailPoet\Cron\CronTrigger;
use MailPoet\InvalidStateException;
use MailPoet\PostEditorBlocks\PostEditorBlock;
use MailPoet\Router;
use MailPoet\Settings\SettingsController;
@ -227,6 +228,8 @@ class Initializer {
$this->postEditorBlock->init();
WPFunctions::get()->doAction('mailpoet_initialized', MAILPOET_VERSION);
} catch (InvalidStateException $e) {
return $this->handleRunningInitialization($e);
} catch (\Exception $e) {
return $this->handleFailedInitialization($e);
}
@ -346,6 +349,13 @@ class Initializer {
return WPNotice::displayError($exception);
}
private function handleRunningInitialization(InvalidStateException $exception) {
if (function_exists('wp_get_current_user')) {
Menu::addErrorPage($this->accessControl);
}
return WPNotice::displayWarning($exception->getMessage());
}
public function setupDeactivationSurvey() {
$survey = new DeactivationSurvey($this->renderer);
$survey->init();

View File

@ -40,7 +40,7 @@ class SetupTest extends \MailPoetTest {
$this->entityManager,
$this->diContainer->get(WP::class)
);
$router = new Setup($wpStub, new Activator($settings, $populator));
$router = new Setup($wpStub, new Activator($settings, $populator, $wpStub));
$response = $router->reset();
expect($response->status)->equals(APIResponse::STATUS_OK);