@@ -362,17 +362,7 @@ public function generate($groupId)
362
362
public function generateJobs ($ jobs , $ exists , $ groupId )
363
363
{
364
364
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 );
376
366
if (!$ cronExpression ) {
377
367
continue ;
378
368
}
@@ -384,13 +374,15 @@ public function generateJobs($jobs, $exists, $groupId)
384
374
}
385
375
386
376
/**
387
- * Clean existed jobs
377
+ * Clean expired jobs
388
378
*
389
379
* @param string $groupId
390
380
* @return $this
391
381
*/
392
382
public function cleanup ($ groupId )
393
383
{
384
+ $ this ->cleanupDisabledJobs ($ groupId );
385
+
394
386
// check if history cleanup is needed
395
387
$ lastCleanup = (int )$ this ->cache ->load (self ::CACHE_KEY_LAST_HISTORY_CLEANUP_AT . $ groupId );
396
388
$ historyCleanUp = (int )$ this ->scopeConfig ->getValue (
@@ -522,4 +514,41 @@ public function getScheduleTimeInterval($groupId)
522
514
523
515
return $ scheduleAheadFor ;
524
516
}
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
+ }
525
554
}
0 commit comments