Move exception handling from concrete workers into processTask() [MAILPOET-2385]

This commit is contained in:
wxa
2019-09-30 07:59:33 +03:00
committed by Jack Kitterhing
parent 2a0bf3f3c8
commit 2a48d0846d
6 changed files with 29 additions and 68 deletions

View File

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

View File

@ -23,7 +23,14 @@ abstract class SingleInstanceSimpleWorker extends SimpleWorker {
}
$this->startProgress($task);
try {
$completed = $this->processTaskStrategy($task);
} catch (\Exception $e) {
$this->stopProgress($task);
throw $e;
}
if ($completed) {
$this->complete($task);
}

View File

@ -27,12 +27,7 @@ class WooCommerceSync extends SingleInstanceSimpleWorker {
}
function processTaskStrategy(ScheduledTask $task) {
try {
$this->woocommerce_segment->synchronizeCustomers();
} catch (\Exception $e) {
$this->stopProgress($task);
throw $e;
}
return true;
}

View File

@ -100,25 +100,4 @@ 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]);
}
}
}

View File

@ -26,7 +26,7 @@ class SingleInstanceSimpleWorkerTest extends \MailPoetTest {
expect($this->worker->processTask($task))->equals(false);
}
function testItWillKeepTheInProgressFlagOnFail() {
function testItWillResetTheInProgressFlagOnFail() {
$task = $this->createScheduledTask();
$this->worker->expects($this->once())
->method('processTaskStrategy')
@ -36,7 +36,7 @@ class SingleInstanceSimpleWorkerTest extends \MailPoetTest {
$this->fail('An exception should be thrown');
} catch (\Exception $e) {
expect($e->getMessage())->equals('test error');
expect(empty($task->getMeta()['in_progress']))->equals(false);
expect(empty($task->getMeta()['in_progress']))->equals(true);
}
}

View File

@ -33,21 +33,6 @@ class WooCommerceSyncTest extends \MailPoetTest {
expect($this->worker->processTaskStrategy($task))->equals(true);
}
function testItWillResetTheInProgressFlagOnFail() {
$task = $this->createScheduledTask();
$this->worker->startProgress($task);
$this->woocommerce_segment->expects($this->once())
->method('synchronizeCustomers')
->willThrowException(new \Exception('test error'));
try {
$this->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]);
}
}
private function createScheduledTask() {
$task = ScheduledTask::create();
$task->type = WooCommerceSync::TASK_TYPE;