Skip to content

Commit 8f24e0f

Browse files
author
Anton Evers
committed
clean up pending jobs that have been disabled in the config
1 parent 5053fa0 commit 8f24e0f

File tree

1 file changed

+41
-12
lines changed

1 file changed

+41
-12
lines changed

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

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -362,17 +362,7 @@ public function generate($groupId)
362362
public function generateJobs($jobs, $exists, $groupId)
363363
{
364364
foreach ($jobs as $jobCode => $jobConfig) {
365-
$cronExpression = null;
366-
if (isset($jobConfig['config_path'])) {
367-
$cronExpression = $this->getConfigSchedule($jobConfig) ?: null;
368-
}
369-
370-
if (!$cronExpression) {
371-
if (isset($jobConfig['schedule'])) {
372-
$cronExpression = $jobConfig['schedule'];
373-
}
374-
}
375-
365+
$cronExpression = $this->getCronExpression($jobConfig);
376366
if (!$cronExpression) {
377367
continue;
378368
}
@@ -384,13 +374,15 @@ public function generateJobs($jobs, $exists, $groupId)
384374
}
385375

386376
/**
387-
* Clean existed jobs
377+
* Clean expired jobs
388378
*
389379
* @param string $groupId
390380
* @return $this
391381
*/
392382
public function cleanup($groupId)
393383
{
384+
$this->cleanupDisabledJobs($groupId);
385+
394386
// check if history cleanup is needed
395387
$lastCleanup = (int)$this->cache->load(self::CACHE_KEY_LAST_HISTORY_CLEANUP_AT . $groupId);
396388
$historyCleanUp = (int)$this->scopeConfig->getValue(
@@ -522,4 +514,41 @@ public function getScheduleTimeInterval($groupId)
522514

523515
return $scheduleAheadFor;
524516
}
517+
518+
/**
519+
* @param $groupId
520+
*/
521+
public function cleanupDisabledJobs($groupId)
522+
{
523+
$jobs = $this->config->getJobs();
524+
foreach ($jobs[$groupId] as $jobCode => $jobConfig) {
525+
if (!$this->getCronExpression($jobConfig)) {
526+
/** @var \Magento\Cron\Model\ResourceModel\Schedule $scheduleResource */
527+
$scheduleResource = $this->scheduleFactory->create()->getResource();
528+
$scheduleResource->getConnection()->delete($scheduleResource->getMainTable(), [
529+
'status=?' => Schedule::STATUS_PENDING,
530+
'job_code=?' => $jobCode,
531+
]);
532+
}
533+
}
534+
}
535+
536+
/**
537+
* @param $jobConfig
538+
* @return null|string
539+
*/
540+
public function getCronExpression($jobConfig)
541+
{
542+
$cronExpression = null;
543+
if (isset($jobConfig['config_path'])) {
544+
$cronExpression = $this->getConfigSchedule($jobConfig) ?: null;
545+
}
546+
547+
if (!$cronExpression) {
548+
if (isset($jobConfig['schedule'])) {
549+
$cronExpression = $jobConfig['schedule'];
550+
}
551+
}
552+
return $cronExpression;
553+
}
525554
}

0 commit comments

Comments
 (0)