Skip to content

Commit e22fb23

Browse files
chickenlandihor-sviziev
authored andcommitted
Added function to check against running/pending/successful cron tasks
Added type missing for static call Updated static code tests again
1 parent 2c80d2a commit e22fb23

File tree

1 file changed

+46
-16
lines changed

1 file changed

+46
-16
lines changed

app/code/Magento/Cron/Observer/ProcessCronQueueObserver.php

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -332,16 +332,21 @@ protected function _runJob($scheduledTime, $currentTime, $jobConfig, $schedule,
332332
$this->stopProfiling();
333333
}
334334

335-
$schedule->setStatus(Schedule::STATUS_SUCCESS)->setFinishedAt(strftime(
336-
'%Y-%m-%d %H:%M:%S',
337-
$this->dateTime->gmtTimestamp()
338-
));
339-
340-
$this->logger->info(sprintf(
341-
'Cron Job %s is successfully finished. Statistics: %s',
342-
$jobCode,
343-
$this->getProfilingStat()
344-
));
335+
$schedule->setStatus(
336+
Schedule::STATUS_SUCCESS)->setFinishedAt(
337+
strftime(
338+
'%Y-%m-%d %H:%M:%S',
339+
$this->dateTime->gmtTimestamp()
340+
)
341+
);
342+
343+
$this->logger->info(
344+
sprintf(
345+
'Cron Job %s is successfully finished. Statistics: %s',
346+
$jobCode,
347+
$this->getProfilingStat()
348+
)
349+
);
345350
}
346351

347352
/**
@@ -391,6 +396,28 @@ private function getPendingSchedules($groupId)
391396
return $pendingJobs;
392397
}
393398

399+
/**
400+
* Return job collection from database with status 'pending', 'running' or 'success'
401+
*
402+
* @param string $groupId
403+
* @return \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
404+
*/
405+
private function getNonExitedSchedules($groupId)
406+
{
407+
$jobs = $this->_config->getJobs();
408+
$pendingJobs = $this->_scheduleFactory->create()->getCollection();
409+
$pendingJobs->addFieldToFilter(
410+
'status',
411+
[
412+
'in' => [
413+
Schedule::STATUS_PENDING, Schedule::STATUS_RUNNING, Schedule::STATUS_SUCCESS
414+
]
415+
]
416+
);
417+
$pendingJobs->addFieldToFilter('job_code', ['in' => array_keys($jobs[$groupId])]);
418+
return $pendingJobs;
419+
}
420+
394421
/**
395422
* Generate cron schedule
396423
*
@@ -422,7 +449,7 @@ private function generateSchedules($groupId)
422449
null
423450
);
424451

425-
$schedules = $this->getPendingSchedules($groupId);
452+
$schedules = $this->getNonExitedSchedules($groupId);
426453
$exists = [];
427454
/** @var Schedule $schedule */
428455
foreach ($schedules as $schedule) {
@@ -653,11 +680,14 @@ private function cleanupScheduleMismatches()
653680
/** @var \Magento\Cron\Model\ResourceModel\Schedule $scheduleResource */
654681
$scheduleResource = $this->_scheduleFactory->create()->getResource();
655682
foreach ($this->invalid as $jobCode => $scheduledAtList) {
656-
$scheduleResource->getConnection()->delete($scheduleResource->getMainTable(), [
657-
'status = ?' => Schedule::STATUS_PENDING,
658-
'job_code = ?' => $jobCode,
659-
'scheduled_at in (?)' => $scheduledAtList,
660-
]);
683+
$scheduleResource->getConnection()->delete(
684+
$scheduleResource->getMainTable(),
685+
[
686+
'status = ?' => Schedule::STATUS_PENDING,
687+
'job_code = ?' => $jobCode,
688+
'scheduled_at in (?)' => $scheduledAtList,
689+
]
690+
);
661691
}
662692
return $this;
663693
}

0 commit comments

Comments
 (0)