@@ -335,16 +335,21 @@ protected function _runJob($scheduledTime, $currentTime, $jobConfig, $schedule,
335
335
$ this ->stopProfiling ();
336
336
}
337
337
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
+ );
348
353
}
349
354
350
355
/**
@@ -395,6 +400,28 @@ private function getPendingSchedules($groupId)
395
400
return $ pendingJobs ;
396
401
}
397
402
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
+
398
425
/**
399
426
* Generate cron schedule
400
427
*
@@ -426,7 +453,7 @@ private function generateSchedules($groupId)
426
453
null
427
454
);
428
455
429
- $ schedules = $ this ->getPendingSchedules ($ groupId );
456
+ $ schedules = $ this ->getNonExitedSchedules ($ groupId );
430
457
$ exists = [];
431
458
/** @var Schedule $schedule */
432
459
foreach ($ schedules as $ schedule ) {
@@ -669,11 +696,14 @@ private function cleanupScheduleMismatches()
669
696
/** @var \Magento\Cron\Model\ResourceModel\Schedule $scheduleResource */
670
697
$ scheduleResource = $ this ->_scheduleFactory ->create ()->getResource ();
671
698
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
+ );
677
707
}
678
708
return $ this ;
679
709
}
0 commit comments