Store the 'in_progress' flag for workers in a separate column [MAILPOET-2443]

This commit is contained in:
wxa
2019-10-08 16:17:36 +03:00
committed by Jack Kitterhing
parent 6668be4a03
commit f98ed72d83
4 changed files with 9 additions and 8 deletions

View File

@ -129,6 +129,7 @@ class Migrator {
'created_at timestamp NULL,', // must be NULL, see comment at the top 'created_at timestamp NULL,', // must be NULL, see comment at the top
'updated_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,', 'updated_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,',
'deleted_at timestamp NULL,', 'deleted_at timestamp NULL,',
'in_progress int(1),',
'reschedule_count int(11) NOT NULL DEFAULT 0,', 'reschedule_count int(11) NOT NULL DEFAULT 0,',
'meta longtext,', 'meta longtext,',
'PRIMARY KEY (id),', 'PRIMARY KEY (id),',

View File

@ -150,8 +150,7 @@ abstract class SimpleWorker {
} }
private function isInProgress(ScheduledTask $task) { private function isInProgress(ScheduledTask $task) {
$meta = $task->getMeta(); if (!empty($task->in_progress)) {
if (!empty($meta['in_progress'])) {
// Do not run multiple instances of the task // Do not run multiple instances of the task
return true; return true;
} }
@ -159,12 +158,12 @@ abstract class SimpleWorker {
} }
private function startProgress(ScheduledTask $task) { private function startProgress(ScheduledTask $task) {
$task->meta = array_merge($task->getMeta(), ['in_progress' => true]); $task->in_progress = true;
$task->save(); $task->save();
} }
private function stopProgress(ScheduledTask $task) { private function stopProgress(ScheduledTask $task) {
$task->meta = array_merge($task->getMeta(), ['in_progress' => null]); $task->in_progress = false;
$task->save(); $task->save();
} }

View File

@ -14,6 +14,7 @@ use MailPoet\WP\Functions as WPFunctions;
* @property string|null $type * @property string|null $type
* @property int $priority * @property int $priority
* @property string|null $scheduled_at * @property string|null $scheduled_at
* @property boolean|null $in_progress
* @property int $reschedule_count * @property int $reschedule_count
* @property string|array|null $meta * @property string|array|null $meta
*/ */

View File

@ -214,9 +214,9 @@ class SimpleWorkerTest extends \MailPoetTest {
->method('processTaskStrategy') ->method('processTaskStrategy')
->willReturn(true); ->willReturn(true);
$task = $this->createRunningTask(); $task = $this->createRunningTask();
expect(empty($task->getMeta()['in_progress']))->equals(true); expect(empty($task->in_progress))->equals(true);
expect($worker->processTask($task))->equals(true); expect($worker->processTask($task))->equals(true);
$task->meta = ['in_progress' => true]; $task->in_progress = true;
expect($worker->processTask($task))->equals(false); expect($worker->processTask($task))->equals(false);
} }
@ -233,7 +233,7 @@ class SimpleWorkerTest extends \MailPoetTest {
$this->fail('An exception should be thrown'); $this->fail('An exception should be thrown');
} catch (\Exception $e) { } catch (\Exception $e) {
expect($e->getMessage())->equals('test error'); expect($e->getMessage())->equals('test error');
expect(empty($task->getMeta()['in_progress']))->equals(true); expect(empty($task->in_progress))->equals(true);
} }
} }
@ -254,7 +254,7 @@ class SimpleWorkerTest extends \MailPoetTest {
$task = ScheduledTask::findOne($task->id); $task = ScheduledTask::findOne($task->id);
expect($scheduled_at < $task->scheduled_at)->true(); expect($scheduled_at < $task->scheduled_at)->true();
expect($task->status)->equals(ScheduledTask::STATUS_SCHEDULED); expect($task->status)->equals(ScheduledTask::STATUS_SCHEDULED);
expect(empty($task->getMeta()['in_progress']))->equals(true); expect(empty($task->in_progress))->equals(true);
} }
function testItCalculatesNextRunDateWithinNextWeekBoundaries() { function testItCalculatesNextRunDateWithinNextWeekBoundaries() {