Use single instance protection for inactive subscribers sync [MAILPOET-2385]
This commit is contained in:
@@ -6,7 +6,7 @@ use MailPoet\Models\ScheduledTask;
|
|||||||
use MailPoet\Settings\SettingsController;
|
use MailPoet\Settings\SettingsController;
|
||||||
use MailPoet\Subscribers\InactiveSubscribersController;
|
use MailPoet\Subscribers\InactiveSubscribersController;
|
||||||
|
|
||||||
class InactiveSubscribers extends SimpleWorker {
|
class InactiveSubscribers extends SingleInstanceSimpleWorker {
|
||||||
const TASK_TYPE = 'inactive_subscribers';
|
const TASK_TYPE = 'inactive_subscribers';
|
||||||
const BATCH_SIZE = 1000;
|
const BATCH_SIZE = 1000;
|
||||||
|
|
||||||
@@ -28,26 +28,31 @@ class InactiveSubscribers extends SimpleWorker {
|
|||||||
|
|
||||||
|
|
||||||
function processTaskStrategy(ScheduledTask $task) {
|
function processTaskStrategy(ScheduledTask $task) {
|
||||||
$tracking_enabled = (bool)$this->settings->get('tracking.enabled');
|
try {
|
||||||
if (!$tracking_enabled) {
|
$tracking_enabled = (bool)$this->settings->get('tracking.enabled');
|
||||||
|
if (!$tracking_enabled) {
|
||||||
|
self::schedule();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
$days_to_inactive = (int)$this->settings->get('deactivate_subscriber_after_inactive_days');
|
||||||
|
// Activate all inactive subscribers in case the feature is turned off
|
||||||
|
if ($days_to_inactive === 0) {
|
||||||
|
$this->inactive_subscribers_controller->reactivateInactiveSubscribers();
|
||||||
|
self::schedule();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
// Handle activation/deactivation within interval
|
||||||
|
while ($this->inactive_subscribers_controller->markInactiveSubscribers($days_to_inactive, self::BATCH_SIZE) === self::BATCH_SIZE) {
|
||||||
|
CronHelper::enforceExecutionLimit($this->timer);
|
||||||
|
};
|
||||||
|
while ($this->inactive_subscribers_controller->markActiveSubscribers($days_to_inactive, self::BATCH_SIZE) === self::BATCH_SIZE) {
|
||||||
|
CronHelper::enforceExecutionLimit($this->timer);
|
||||||
|
};
|
||||||
self::schedule();
|
self::schedule();
|
||||||
return true;
|
} catch (\Exception $e) {
|
||||||
|
$this->stopProgress($task);
|
||||||
|
throw $e;
|
||||||
}
|
}
|
||||||
$days_to_inactive = (int)$this->settings->get('deactivate_subscriber_after_inactive_days');
|
|
||||||
// Activate all inactive subscribers in case the feature is turned off
|
|
||||||
if ($days_to_inactive === 0) {
|
|
||||||
$this->inactive_subscribers_controller->reactivateInactiveSubscribers();
|
|
||||||
self::schedule();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
// Handle activation/deactivation within interval
|
|
||||||
while ($this->inactive_subscribers_controller->markInactiveSubscribers($days_to_inactive, self::BATCH_SIZE) === self::BATCH_SIZE) {
|
|
||||||
CronHelper::enforceExecutionLimit($this->timer);
|
|
||||||
};
|
|
||||||
while ($this->inactive_subscribers_controller->markActiveSubscribers($days_to_inactive, self::BATCH_SIZE) === self::BATCH_SIZE) {
|
|
||||||
CronHelper::enforceExecutionLimit($this->timer);
|
|
||||||
};
|
|
||||||
self::schedule();
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -100,4 +100,25 @@ class InactiveSubscribersTest extends \MailPoetTest {
|
|||||||
$this->setExpectedException(\Exception::class, 'Maximum execution time has been reached.');
|
$this->setExpectedException(\Exception::class, 'Maximum execution time has been reached.');
|
||||||
$worker->processTaskStrategy(ScheduledTask::createOrUpdate([]));
|
$worker->processTaskStrategy(ScheduledTask::createOrUpdate([]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function testItWillResetTheInProgressFlagOnFail() {
|
||||||
|
$this->settings->set('deactivate_subscriber_after_inactive_days', 5);
|
||||||
|
$controller_mock = $this->createMock(InactiveSubscribersController::class);
|
||||||
|
|
||||||
|
$task = ScheduledTask::createOrUpdate([]);
|
||||||
|
|
||||||
|
$controller_mock->expects($this->once())
|
||||||
|
->method('markInactiveSubscribers')
|
||||||
|
->willThrowException(new \Exception('test error'));
|
||||||
|
|
||||||
|
try {
|
||||||
|
$worker = new InactiveSubscribers($controller_mock, $this->settings);
|
||||||
|
$worker->startProgress($task);
|
||||||
|
$worker->processTaskStrategy($task);
|
||||||
|
$this->fail('An exception should be thrown');
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
expect($e->getMessage())->equals('test error');
|
||||||
|
expect($task->getMeta())->equals(['in_progress' => null]);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user