Skip to content

Commit

Permalink
Added function to check against running/pending/successful cron tasks
Browse files Browse the repository at this point in the history
Added type missing for static call

Updated static code tests again
  • Loading branch information
chickenland committed Jun 19, 2019
1 parent 6e534ad commit 7859a2f
Showing 1 changed file with 46 additions and 16 deletions.
62 changes: 46 additions & 16 deletions app/code/Magento/Cron/Observer/ProcessCronQueueObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,16 +335,21 @@ protected function _runJob($scheduledTime, $currentTime, $jobConfig, $schedule,
$this->stopProfiling();
}

$schedule->setStatus(Schedule::STATUS_SUCCESS)->setFinishedAt(strftime(
'%Y-%m-%d %H:%M:%S',
$this->dateTime->gmtTimestamp()
));

$this->logger->info(sprintf(
'Cron Job %s is successfully finished. Statistics: %s',
$jobCode,
$this->getProfilingStat()
));
$schedule->setStatus(
Schedule::STATUS_SUCCESS)->setFinishedAt(
strftime(
'%Y-%m-%d %H:%M:%S',
$this->dateTime->gmtTimestamp()
)
);

$this->logger->info(
sprintf(
'Cron Job %s is successfully finished. Statistics: %s',
$jobCode,
$this->getProfilingStat()
)
);
}

/**
Expand Down Expand Up @@ -395,6 +400,28 @@ private function getPendingSchedules($groupId)
return $pendingJobs;
}

/**
* Return job collection from database with status 'pending', 'running' or 'success'
*
* @param string $groupId
* @return \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
*/
private function getNonExitedSchedules($groupId)
{
$jobs = $this->_config->getJobs();
$pendingJobs = $this->_scheduleFactory->create()->getCollection();
$pendingJobs->addFieldToFilter(
'status',
[
'in' => [
Schedule::STATUS_PENDING, Schedule::STATUS_RUNNING, Schedule::STATUS_SUCCESS
]
]
);
$pendingJobs->addFieldToFilter('job_code', ['in' => array_keys($jobs[$groupId])]);
return $pendingJobs;
}

/**
* Generate cron schedule
*
Expand Down Expand Up @@ -426,7 +453,7 @@ private function generateSchedules($groupId)
null
);

$schedules = $this->getPendingSchedules($groupId);
$schedules = $this->getNonExitedSchedules($groupId);
$exists = [];
/** @var Schedule $schedule */
foreach ($schedules as $schedule) {
Expand Down Expand Up @@ -669,11 +696,14 @@ private function cleanupScheduleMismatches()
/** @var \Magento\Cron\Model\ResourceModel\Schedule $scheduleResource */
$scheduleResource = $this->_scheduleFactory->create()->getResource();
foreach ($this->invalid as $jobCode => $scheduledAtList) {
$scheduleResource->getConnection()->delete($scheduleResource->getMainTable(), [
'status = ?' => Schedule::STATUS_PENDING,
'job_code = ?' => $jobCode,
'scheduled_at in (?)' => $scheduledAtList,
]);
$scheduleResource->getConnection()->delete(
$scheduleResource->getMainTable(),
[
'status = ?' => Schedule::STATUS_PENDING,
'job_code = ?' => $jobCode,
'scheduled_at in (?)' => $scheduledAtList,
]
);
}
return $this;
}
Expand Down

0 comments on commit 7859a2f

Please sign in to comment.