Skip to content

Commit

Permalink
Support cron expressions like 3/10 * * * * and handle hyphen expres…
Browse files Browse the repository at this point in the history
…sions like `3-23/5 * * * *` correctly
  • Loading branch information
AntonEvers committed Jun 30, 2020
1 parent f3a160c commit 6dd0bb6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
10 changes: 9 additions & 1 deletion app/code/Magento/Cron/Model/Schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ public function matchCronExpression($expr, $num)
}

// handle all match by modulus
$offset = 0;
if ($expr === '*') {
$from = 0;
$to = 60;
Expand All @@ -201,6 +202,13 @@ public function matchCronExpression($expr, $num)

$from = $this->getNumeric($e[0]);
$to = $this->getNumeric($e[1]);
if ($mod !== 1) {
$offset = $from;
}
} elseif ($mod !== 1) {
$offset = $this->getNumeric($expr);
$from = 0;
$to = 60;
} else {
// handle regular token
$from = $this->getNumeric($expr);
Expand All @@ -211,7 +219,7 @@ public function matchCronExpression($expr, $num)
throw new CronException(__('Invalid cron expression: %1', $expr));
}

return $num >= $from && $num <= $to && $num % $mod === 0;
return $num >= $from && $num <= $to && ($num - $offset) % $mod === 0;
}

/**
Expand Down
16 changes: 16 additions & 0 deletions app/code/Magento/Cron/Test/Unit/Model/ScheduleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,24 +128,28 @@ public function setCronExprDataProvider(): array
[', * * * *', [',', '*', '*', '*', '*']],
['1-2 * * * *', ['1-2', '*', '*', '*', '*']],
['0/5 * * * *', ['0/5', '*', '*', '*', '*']],
['3/5 * * * *', ['3/5', '*', '*', '*', '*']],

['* 0 * * *', ['*', '0', '*', '*', '*']],
['* 59 * * *', ['*', '59', '*', '*', '*']],
['* , * * *', ['*', ',', '*', '*', '*']],
['* 1-2 * * *', ['*', '1-2', '*', '*', '*']],
['* 0/5 * * *', ['*', '0/5', '*', '*', '*']],
['* 3/5 * * *', ['*', '3/5', '*', '*', '*']],

['* * 0 * *', ['*', '*', '0', '*', '*']],
['* * 23 * *', ['*', '*', '23', '*', '*']],
['* * , * *', ['*', '*', ',', '*', '*']],
['* * 1-2 * *', ['*', '*', '1-2', '*', '*']],
['* * 0/5 * *', ['*', '*', '0/5', '*', '*']],
['* * 3/5 * *', ['*', '*', '3/5', '*', '*']],

['* * * 1 *', ['*', '*', '*', '1', '*']],
['* * * 31 *', ['*', '*', '*', '31', '*']],
['* * * , *', ['*', '*', '*', ',', '*']],
['* * * 1-2 *', ['*', '*', '*', '1-2', '*']],
['* * * 0/5 *', ['*', '*', '*', '0/5', '*']],
['* * * 3/5 *', ['*', '*', '*', '3/5', '*']],
['* * * ? *', ['*', '*', '*', '?', '*']],
['* * * L *', ['*', '*', '*', 'L', '*']],
['* * * W *', ['*', '*', '*', 'W', '*']],
Expand All @@ -156,6 +160,7 @@ public function setCronExprDataProvider(): array
['* * * * ,', ['*', '*', '*', '*', ',']],
['* * * * 1-2', ['*', '*', '*', '*', '1-2']],
['* * * * 0/5', ['*', '*', '*', '*', '0/5']],
['* * * * 3/5', ['*', '*', '*', '*', '3/5']],
['* * * * JAN', ['*', '*', '*', '*', 'JAN']],
['* * * * DEC', ['*', '*', '*', '*', 'DEC']],
['* * * * JAN-DEC', ['*', '*', '*', '*', 'JAN-DEC']],
Expand All @@ -165,6 +170,7 @@ public function setCronExprDataProvider(): array
['* * * * * ,', ['*', '*', '*', '*', '*', ',']],
['* * * * * 1-2', ['*', '*', '*', '*', '*', '1-2']],
['* * * * * 0/5', ['*', '*', '*', '*', '*', '0/5']],
['* * * * * 3/5', ['*', '*', '*', '*', '*', '3/5']],
['* * * * * ?', ['*', '*', '*', '*', '*', '?']],
['* * * * * L', ['*', '*', '*', '*', '*', 'L']],
['* * * * * 6#3', ['*', '*', '*', '*', '*', '6#3']],
Expand Down Expand Up @@ -372,9 +378,19 @@ public function matchCronExpressionDataProvider(): array
['0-20/5', 21, false],
['0-20/5', 25, false],

['3-20/5', 3, true],
['3-20/5', 8, true],
['3-20/5', 13, true],
['3-20/5', 24, false],
['3-20/5', 28, false],

['1/5', 5, false],
['5/5', 5, true],
['10/5', 10, true],

['4/5', 8, false],
['8/5', 8, true],
['13/5', 13, true],
];
}

Expand Down

0 comments on commit 6dd0bb6

Please sign in to comment.