From 6dd0bb69333c549d2267cfc7a054eb66da1deadf Mon Sep 17 00:00:00 2001 From: Anton Evers Date: Mon, 15 Apr 2019 14:08:49 +0200 Subject: [PATCH] Support cron expressions like `3/10 * * * *` and handle hyphen expressions like `3-23/5 * * * *` correctly --- app/code/Magento/Cron/Model/Schedule.php | 10 +++++++++- .../Cron/Test/Unit/Model/ScheduleTest.php | 16 ++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Cron/Model/Schedule.php b/app/code/Magento/Cron/Model/Schedule.php index 3769b8f12cad2..136e2ef191084 100644 --- a/app/code/Magento/Cron/Model/Schedule.php +++ b/app/code/Magento/Cron/Model/Schedule.php @@ -189,6 +189,7 @@ public function matchCronExpression($expr, $num) } // handle all match by modulus + $offset = 0; if ($expr === '*') { $from = 0; $to = 60; @@ -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); @@ -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; } /** diff --git a/app/code/Magento/Cron/Test/Unit/Model/ScheduleTest.php b/app/code/Magento/Cron/Test/Unit/Model/ScheduleTest.php index 25e9a8347b2cd..81e96c6ea75b3 100644 --- a/app/code/Magento/Cron/Test/Unit/Model/ScheduleTest.php +++ b/app/code/Magento/Cron/Test/Unit/Model/ScheduleTest.php @@ -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', '*']], @@ -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']], @@ -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']], @@ -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], ]; }