60 lines
1.7 KiB
PHP
60 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace MailPoet\Cron\Workers;
|
|
|
|
use Carbon\Carbon;
|
|
use MailPoet\Cron\CronHelper;
|
|
use MailPoet\Models\ScheduledTask;
|
|
use MailPoet\WP\Functions as WPFunctions;
|
|
|
|
abstract class SingleInstanceSimpleWorker extends SimpleWorker {
|
|
const TASK_RUN_TIMEOUT = 120;
|
|
const TIMED_OUT_TASK_RESCHEDULE_TIMEOUT = 5;
|
|
|
|
function processTask(ScheduledTask $task) {
|
|
// abort if execution limit is reached
|
|
CronHelper::enforceExecutionLimit($this->timer);
|
|
|
|
if ($this->isInProgress($task)) {
|
|
return false;
|
|
}
|
|
|
|
$this->startProgress($task);
|
|
$completed = $this->processTaskStrategy($task);
|
|
if ($completed) {
|
|
$this->complete($task);
|
|
}
|
|
$this->stopProgress($task);
|
|
|
|
return (bool)$completed;
|
|
}
|
|
|
|
function isInProgress(ScheduledTask $task) {
|
|
$meta = $task->getMeta();
|
|
$current_time = Carbon::createFromTimestamp(WPFunctions::get()->currentTime('timestamp'));
|
|
$updated_at = Carbon::createFromTimestamp(strtotime($task->updated_at));
|
|
|
|
// If the task is running for too long consider it stuck and reschedule
|
|
if (!empty($task->updated_at) && $updated_at->diffInMinutes($current_time, false) > self::TASK_RUN_TIMEOUT) {
|
|
$task->meta = array_merge($task->getMeta(), ['in_progress' => null]);
|
|
$this->reschedule($task, self::TIMED_OUT_TASK_RESCHEDULE_TIMEOUT);
|
|
return true;
|
|
} elseif (!empty($meta['in_progress'])) {
|
|
// Do not run multiple instances of the task
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
function startProgress(ScheduledTask $task) {
|
|
$task->meta = array_merge($task->getMeta(), ['in_progress' => true]);
|
|
$task->save();
|
|
}
|
|
|
|
function stopProgress(ScheduledTask $task) {
|
|
$task->meta = array_merge($task->getMeta(), ['in_progress' => null]);
|
|
$task->save();
|
|
}
|
|
}
|