Use single instance protection for inactive subscribers sync [MAILPOET-2385]

This commit is contained in:
wxa
2019-09-25 12:01:58 +03:00
committed by Jack Kitterhing
parent 9b1f7f2d8c
commit ce645306a5
2 changed files with 45 additions and 19 deletions

View File

@@ -6,7 +6,7 @@ use MailPoet\Models\ScheduledTask;
use MailPoet\Settings\SettingsController;
use MailPoet\Subscribers\InactiveSubscribersController;
class InactiveSubscribers extends SimpleWorker {
class InactiveSubscribers extends SingleInstanceSimpleWorker {
const TASK_TYPE = 'inactive_subscribers';
const BATCH_SIZE = 1000;
@@ -28,6 +28,7 @@ class InactiveSubscribers extends SimpleWorker {
function processTaskStrategy(ScheduledTask $task) {
try {
$tracking_enabled = (bool)$this->settings->get('tracking.enabled');
if (!$tracking_enabled) {
self::schedule();
@@ -48,6 +49,10 @@ class InactiveSubscribers extends SimpleWorker {
CronHelper::enforceExecutionLimit($this->timer);
};
self::schedule();
} catch (\Exception $e) {
$this->stopProgress($task);
throw $e;
}
return true;
}
}

View File

@@ -100,4 +100,25 @@ class InactiveSubscribersTest extends \MailPoetTest {
$this->setExpectedException(\Exception::class, 'Maximum execution time has been reached.');
$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]);
}
}
}