@@ -335,16 +335,21 @@ protected function _runJob($scheduledTime, $currentTime, $jobConfig, $schedule,
335335 $ this ->stopProfiling ();
336336 }
337337
338- $ schedule ->setStatus (Schedule::STATUS_SUCCESS )->setFinishedAt (strftime (
339- '%Y-%m-%d %H:%M:%S ' ,
340- $ this ->dateTime ->gmtTimestamp ()
341- ));
342-
343- $ this ->logger ->info (sprintf (
344- 'Cron Job %s is successfully finished. Statistics: %s ' ,
345- $ jobCode ,
346- $ this ->getProfilingStat ()
347- ));
338+ $ schedule ->setStatus (
339+ Schedule::STATUS_SUCCESS )->setFinishedAt (
340+ strftime (
341+ '%Y-%m-%d %H:%M:%S ' ,
342+ $ this ->dateTime ->gmtTimestamp ()
343+ )
344+ );
345+
346+ $ this ->logger ->info (
347+ sprintf (
348+ 'Cron Job %s is successfully finished. Statistics: %s ' ,
349+ $ jobCode ,
350+ $ this ->getProfilingStat ()
351+ )
352+ );
348353 }
349354
350355 /**
@@ -395,6 +400,28 @@ private function getPendingSchedules($groupId)
395400 return $ pendingJobs ;
396401 }
397402
403+ /**
404+ * Return job collection from database with status 'pending', 'running' or 'success'
405+ *
406+ * @param string $groupId
407+ * @return \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
408+ */
409+ private function getNonExitedSchedules ($ groupId )
410+ {
411+ $ jobs = $ this ->_config ->getJobs ();
412+ $ pendingJobs = $ this ->_scheduleFactory ->create ()->getCollection ();
413+ $ pendingJobs ->addFieldToFilter (
414+ 'status ' ,
415+ [
416+ 'in ' => [
417+ Schedule::STATUS_PENDING , Schedule::STATUS_RUNNING , Schedule::STATUS_SUCCESS
418+ ]
419+ ]
420+ );
421+ $ pendingJobs ->addFieldToFilter ('job_code ' , ['in ' => array_keys ($ jobs [$ groupId ])]);
422+ return $ pendingJobs ;
423+ }
424+
398425 /**
399426 * Generate cron schedule
400427 *
@@ -426,7 +453,7 @@ private function generateSchedules($groupId)
426453 null
427454 );
428455
429- $ schedules = $ this ->getPendingSchedules ($ groupId );
456+ $ schedules = $ this ->getNonExitedSchedules ($ groupId );
430457 $ exists = [];
431458 /** @var Schedule $schedule */
432459 foreach ($ schedules as $ schedule ) {
@@ -669,11 +696,14 @@ private function cleanupScheduleMismatches()
669696 /** @var \Magento\Cron\Model\ResourceModel\Schedule $scheduleResource */
670697 $ scheduleResource = $ this ->_scheduleFactory ->create ()->getResource ();
671698 foreach ($ this ->invalid as $ jobCode => $ scheduledAtList ) {
672- $ scheduleResource ->getConnection ()->delete ($ scheduleResource ->getMainTable (), [
673- 'status = ? ' => Schedule::STATUS_PENDING ,
674- 'job_code = ? ' => $ jobCode ,
675- 'scheduled_at in (?) ' => $ scheduledAtList ,
676- ]);
699+ $ scheduleResource ->getConnection ()->delete (
700+ $ scheduleResource ->getMainTable (),
701+ [
702+ 'status = ? ' => Schedule::STATUS_PENDING ,
703+ 'job_code = ? ' => $ jobCode ,
704+ 'scheduled_at in (?) ' => $ scheduledAtList ,
705+ ]
706+ );
677707 }
678708 return $ this ;
679709 }
0 commit comments