Move SQL queries from SimpleWorker to model, improve naming
[MAILPOET-2538]
This commit is contained in:
committed by
Jack Kitterhing
parent
4e8165c4b4
commit
ba41f4901e
@ -248,7 +248,7 @@ class Scheduler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private function reScheduleBounceTask() {
|
private function reScheduleBounceTask() {
|
||||||
$bounce_tasks = Bounce::getScheduledTasks($future = true);
|
$bounce_tasks = ScheduledTask::findFutureScheduledByType(Bounce::TASK_TYPE);
|
||||||
if (count($bounce_tasks)) {
|
if (count($bounce_tasks)) {
|
||||||
$bounce_task = reset($bounce_tasks);
|
$bounce_task = reset($bounce_tasks);
|
||||||
if (Carbon::createFromTimestamp(current_time('timestamp'))->addHour(42)->lessThan($bounce_task->scheduled_at)) {
|
if (Carbon::createFromTimestamp(current_time('timestamp'))->addHour(42)->lessThan($bounce_task->scheduled_at)) {
|
||||||
|
@ -47,7 +47,7 @@ abstract class SimpleWorker {
|
|||||||
|
|
||||||
$this->init();
|
$this->init();
|
||||||
|
|
||||||
$scheduled_tasks = self::getScheduledTasks();
|
$scheduled_tasks = self::getDueTasks();
|
||||||
$running_tasks = self::getRunningTasks();
|
$running_tasks = self::getRunningTasks();
|
||||||
|
|
||||||
if (!$scheduled_tasks && !$running_tasks) {
|
if (!$scheduled_tasks && !$running_tasks) {
|
||||||
@ -192,42 +192,15 @@ abstract class SimpleWorker {
|
|||||||
return $date;
|
return $date;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
static function getDueTasks() {
|
||||||
* @param bool $future
|
return ScheduledTask::findDueByType(static::TASK_TYPE, self::TASK_BATCH_SIZE);
|
||||||
* @return ScheduledTask[]
|
|
||||||
*/
|
|
||||||
static function getScheduledTasks($future = false) {
|
|
||||||
$dateWhere = ($future) ? 'whereGt' : 'whereLte';
|
|
||||||
$wp = new WPFunctions();
|
|
||||||
return ScheduledTask::where('type', static::TASK_TYPE)
|
|
||||||
->$dateWhere('scheduled_at', Carbon::createFromTimestamp($wp->currentTime('timestamp')))
|
|
||||||
->whereNull('deleted_at')
|
|
||||||
->where('status', ScheduledTask::STATUS_SCHEDULED)
|
|
||||||
->limit(self::TASK_BATCH_SIZE)
|
|
||||||
->findMany();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static function getRunningTasks() {
|
static function getRunningTasks() {
|
||||||
$wp = new WPFunctions();
|
return ScheduledTask::findRunningByType(static::TASK_TYPE, self::TASK_BATCH_SIZE);
|
||||||
return ScheduledTask::where('type', static::TASK_TYPE)
|
|
||||||
->whereLte('scheduled_at', Carbon::createFromTimestamp($wp->currentTime('timestamp')))
|
|
||||||
->whereNull('deleted_at')
|
|
||||||
->whereNull('status')
|
|
||||||
->limit(self::TASK_BATCH_SIZE)
|
|
||||||
->findMany();
|
|
||||||
}
|
|
||||||
|
|
||||||
static function getDueTasks() {
|
|
||||||
$scheduled_tasks = self::getScheduledTasks();
|
|
||||||
$running_tasks = self::getRunningTasks();
|
|
||||||
return array_merge((array)$scheduled_tasks, (array)$running_tasks);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static function getCompletedTasks() {
|
static function getCompletedTasks() {
|
||||||
return ScheduledTask::where('type', static::TASK_TYPE)
|
return ScheduledTask::findCompletedByType(static::TASK_TYPE, self::TASK_BATCH_SIZE);
|
||||||
->whereNull('deleted_at')
|
|
||||||
->where('status', ScheduledTask::STATUS_COMPLETED)
|
|
||||||
->limit(self::TASK_BATCH_SIZE)
|
|
||||||
->findMany();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -160,4 +160,44 @@ class ScheduledTask extends Model {
|
|||||||
->whereNull('tasks.deleted_at')
|
->whereNull('tasks.deleted_at')
|
||||||
->findOne() ?: null;
|
->findOne() ?: null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static function findDueByType($type, $limit = null) {
|
||||||
|
return self::findByTypeAndStatus($type, ScheduledTask::STATUS_SCHEDULED, $limit);
|
||||||
|
}
|
||||||
|
|
||||||
|
static function findRunningByType($type, $limit = null) {
|
||||||
|
return self::findByTypeAndStatus($type, null, $limit);
|
||||||
|
}
|
||||||
|
|
||||||
|
static function findFutureScheduledByType($type, $limit = null) {
|
||||||
|
return self::findByTypeAndStatus($type, ScheduledTask::STATUS_SCHEDULED, $limit, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
static function findCompletedByType($type, $limit = null) {
|
||||||
|
return self::findByTypeAndStatus($type, ScheduledTask::STATUS_COMPLETED, $limit);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function findByTypeAndStatus($type, $status, $limit = null, $future = false) {
|
||||||
|
$query = ScheduledTask::where('type', $type)
|
||||||
|
->whereNull('deleted_at');
|
||||||
|
|
||||||
|
$now = Carbon::createFromTimestamp(WPFunctions::get()->currentTime('timestamp'));
|
||||||
|
if ($future) {
|
||||||
|
$query->whereGt('scheduled_at', $now);
|
||||||
|
} else {
|
||||||
|
$query->whereLte('scheduled_at', $now);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($status === null) {
|
||||||
|
$query->whereNull('status');
|
||||||
|
} else {
|
||||||
|
$query->where('status', $status);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($limit !== null) {
|
||||||
|
$query->limit($limit);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $query->findMany();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -65,16 +65,16 @@ class SimpleWorkerTest extends \MailPoetTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function testItCanGetScheduledTasks() {
|
function testItCanGetScheduledTasks() {
|
||||||
expect(MockSimpleWorker::getScheduledTasks())->isEmpty();
|
expect(MockSimpleWorker::getDueTasks())->isEmpty();
|
||||||
$this->createScheduledTask();
|
$this->createScheduledTask();
|
||||||
expect(MockSimpleWorker::getScheduledTasks())->notEmpty();
|
expect(MockSimpleWorker::getDueTasks())->notEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
function testItCanGetABatchOfScheduledTasks() {
|
function testItCanGetABatchOfScheduledTasks() {
|
||||||
for ($i = 0; $i < MockSimpleWorker::TASK_BATCH_SIZE + 5; $i += 1) {
|
for ($i = 0; $i < MockSimpleWorker::TASK_BATCH_SIZE + 5; $i += 1) {
|
||||||
$this->createScheduledTask();
|
$this->createScheduledTask();
|
||||||
}
|
}
|
||||||
expect(count(MockSimpleWorker::getScheduledTasks()))->equals(MockSimpleWorker::TASK_BATCH_SIZE);
|
expect(count(MockSimpleWorker::getDueTasks()))->equals(MockSimpleWorker::TASK_BATCH_SIZE);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testItCanGetRunningTasks() {
|
function testItCanGetRunningTasks() {
|
||||||
@ -97,28 +97,6 @@ class SimpleWorkerTest extends \MailPoetTest {
|
|||||||
expect(count(MockSimpleWorker::getCompletedTasks()))->equals(MockSimpleWorker::TASK_BATCH_SIZE);
|
expect(count(MockSimpleWorker::getCompletedTasks()))->equals(MockSimpleWorker::TASK_BATCH_SIZE);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testItCanGetDueTasks() {
|
|
||||||
expect(MockSimpleWorker::getDueTasks())->isEmpty();
|
|
||||||
|
|
||||||
// scheduled for now
|
|
||||||
$this->createScheduledTask();
|
|
||||||
|
|
||||||
// running
|
|
||||||
$this->createRunningTask();
|
|
||||||
|
|
||||||
// scheduled in the future (should not be retrieved)
|
|
||||||
$task = $this->createScheduledTask();
|
|
||||||
$task->scheduled_at = Carbon::createFromTimestamp(current_time('timestamp'))->addDays(7);
|
|
||||||
$task->save();
|
|
||||||
|
|
||||||
// completed (should not be retrieved)
|
|
||||||
$task = $this->createRunningTask();
|
|
||||||
$task->status = ScheduledTask::STATUS_COMPLETED;
|
|
||||||
$task->save();
|
|
||||||
|
|
||||||
expect(count(MockSimpleWorker::getDueTasks()))->equals(2);
|
|
||||||
}
|
|
||||||
|
|
||||||
function testItFailsToProcessWithoutTasks() {
|
function testItFailsToProcessWithoutTasks() {
|
||||||
expect($this->worker->process())->false();
|
expect($this->worker->process())->false();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user