@@ -332,16 +332,21 @@ protected function _runJob($scheduledTime, $currentTime, $jobConfig, $schedule,
332
332
$ this ->stopProfiling ();
333
333
}
334
334
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
+ );
345
350
}
346
351
347
352
/**
@@ -391,6 +396,28 @@ private function getPendingSchedules($groupId)
391
396
return $ pendingJobs ;
392
397
}
393
398
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
+
394
421
/**
395
422
* Generate cron schedule
396
423
*
@@ -422,7 +449,7 @@ private function generateSchedules($groupId)
422
449
null
423
450
);
424
451
425
- $ schedules = $ this ->getPendingSchedules ($ groupId );
452
+ $ schedules = $ this ->getNonExitedSchedules ($ groupId );
426
453
$ exists = [];
427
454
/** @var Schedule $schedule */
428
455
foreach ($ schedules as $ schedule ) {
@@ -653,11 +680,14 @@ private function cleanupScheduleMismatches()
653
680
/** @var \Magento\Cron\Model\ResourceModel\Schedule $scheduleResource */
654
681
$ scheduleResource = $ this ->_scheduleFactory ->create ()->getResource ();
655
682
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
+ );
661
691
}
662
692
return $ this ;
663
693
}
0 commit comments