Ensure that getMeta() returns an array in the scheduled task model [MAILPOET-2385]

This commit is contained in:
wxa
2019-09-30 07:59:00 +03:00
committed by Jack Kitterhing
parent ce645306a5
commit f598137f3b
2 changed files with 2 additions and 13 deletions

View File

@ -36,7 +36,6 @@ abstract class SingleInstanceSimpleWorker extends SimpleWorker {
// If the task is running for too long consider it stuck and reschedule // 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) { if (!empty($task->updated_at) && $updated_at->diffInMinutes($current_time, false) > self::TASK_RUN_TIMEOUT) {
$this->ensureMetaIsArray($task);
$task->meta = array_merge($task->getMeta(), ['in_progress' => null]); $task->meta = array_merge($task->getMeta(), ['in_progress' => null]);
$this->reschedule($task, self::TIMED_OUT_TASK_RESCHEDULE_TIMEOUT); $this->reschedule($task, self::TIMED_OUT_TASK_RESCHEDULE_TIMEOUT);
return true; return true;
@ -49,23 +48,12 @@ abstract class SingleInstanceSimpleWorker extends SimpleWorker {
} }
function startProgress(ScheduledTask $task) { function startProgress(ScheduledTask $task) {
$this->ensureMetaIsArray($task);
$task->meta = array_merge($task->getMeta(), ['in_progress' => true]); $task->meta = array_merge($task->getMeta(), ['in_progress' => true]);
$task->save(); $task->save();
} }
function stopProgress(ScheduledTask $task) { function stopProgress(ScheduledTask $task) {
$this->ensureMetaIsArray($task);
$task->meta = array_merge($task->getMeta(), ['in_progress' => null]); $task->meta = array_merge($task->getMeta(), ['in_progress' => null]);
$task->save(); $task->save();
} }
private function ensureMetaIsArray(ScheduledTask $task) {
$meta = $task->getMeta();
if (empty($meta)) {
$task->meta = [];
} elseif (!is_array($meta)) {
$task->meta = (array)$meta;
}
}
} }

View File

@ -120,7 +120,8 @@ class ScheduledTask extends Model {
} }
function getMeta() { function getMeta() {
return (Helpers::isJson($this->meta)) ? json_decode($this->meta, true) : $this->meta; $meta = (Helpers::isJson($this->meta)) ? json_decode($this->meta, true) : $this->meta;
return !empty($meta) ? (array)$meta : [];
} }
function delete() { function delete() {