Skip to content

Commit 6dd0bb6

Browse files
committed
Support cron expressions like 3/10 * * * * and handle hyphen expressions like 3-23/5 * * * * correctly
1 parent f3a160c commit 6dd0bb6

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

app/code/Magento/Cron/Model/Schedule.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ public function matchCronExpression($expr, $num)
189189
}
190190

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

202203
$from = $this->getNumeric($e[0]);
203204
$to = $this->getNumeric($e[1]);
205+
if ($mod !== 1) {
206+
$offset = $from;
207+
}
208+
} elseif ($mod !== 1) {
209+
$offset = $this->getNumeric($expr);
210+
$from = 0;
211+
$to = 60;
204212
} else {
205213
// handle regular token
206214
$from = $this->getNumeric($expr);
@@ -211,7 +219,7 @@ public function matchCronExpression($expr, $num)
211219
throw new CronException(__('Invalid cron expression: %1', $expr));
212220
}
213221

214-
return $num >= $from && $num <= $to && $num % $mod === 0;
222+
return $num >= $from && $num <= $to && ($num - $offset) % $mod === 0;
215223
}
216224

217225
/**

app/code/Magento/Cron/Test/Unit/Model/ScheduleTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,24 +128,28 @@ public function setCronExprDataProvider(): array
128128
[', * * * *', [',', '*', '*', '*', '*']],
129129
['1-2 * * * *', ['1-2', '*', '*', '*', '*']],
130130
['0/5 * * * *', ['0/5', '*', '*', '*', '*']],
131+
['3/5 * * * *', ['3/5', '*', '*', '*', '*']],
131132

132133
['* 0 * * *', ['*', '0', '*', '*', '*']],
133134
['* 59 * * *', ['*', '59', '*', '*', '*']],
134135
['* , * * *', ['*', ',', '*', '*', '*']],
135136
['* 1-2 * * *', ['*', '1-2', '*', '*', '*']],
136137
['* 0/5 * * *', ['*', '0/5', '*', '*', '*']],
138+
['* 3/5 * * *', ['*', '3/5', '*', '*', '*']],
137139

138140
['* * 0 * *', ['*', '*', '0', '*', '*']],
139141
['* * 23 * *', ['*', '*', '23', '*', '*']],
140142
['* * , * *', ['*', '*', ',', '*', '*']],
141143
['* * 1-2 * *', ['*', '*', '1-2', '*', '*']],
142144
['* * 0/5 * *', ['*', '*', '0/5', '*', '*']],
145+
['* * 3/5 * *', ['*', '*', '3/5', '*', '*']],
143146

144147
['* * * 1 *', ['*', '*', '*', '1', '*']],
145148
['* * * 31 *', ['*', '*', '*', '31', '*']],
146149
['* * * , *', ['*', '*', '*', ',', '*']],
147150
['* * * 1-2 *', ['*', '*', '*', '1-2', '*']],
148151
['* * * 0/5 *', ['*', '*', '*', '0/5', '*']],
152+
['* * * 3/5 *', ['*', '*', '*', '3/5', '*']],
149153
['* * * ? *', ['*', '*', '*', '?', '*']],
150154
['* * * L *', ['*', '*', '*', 'L', '*']],
151155
['* * * W *', ['*', '*', '*', 'W', '*']],
@@ -156,6 +160,7 @@ public function setCronExprDataProvider(): array
156160
['* * * * ,', ['*', '*', '*', '*', ',']],
157161
['* * * * 1-2', ['*', '*', '*', '*', '1-2']],
158162
['* * * * 0/5', ['*', '*', '*', '*', '0/5']],
163+
['* * * * 3/5', ['*', '*', '*', '*', '3/5']],
159164
['* * * * JAN', ['*', '*', '*', '*', 'JAN']],
160165
['* * * * DEC', ['*', '*', '*', '*', 'DEC']],
161166
['* * * * JAN-DEC', ['*', '*', '*', '*', 'JAN-DEC']],
@@ -165,6 +170,7 @@ public function setCronExprDataProvider(): array
165170
['* * * * * ,', ['*', '*', '*', '*', '*', ',']],
166171
['* * * * * 1-2', ['*', '*', '*', '*', '*', '1-2']],
167172
['* * * * * 0/5', ['*', '*', '*', '*', '*', '0/5']],
173+
['* * * * * 3/5', ['*', '*', '*', '*', '*', '3/5']],
168174
['* * * * * ?', ['*', '*', '*', '*', '*', '?']],
169175
['* * * * * L', ['*', '*', '*', '*', '*', 'L']],
170176
['* * * * * 6#3', ['*', '*', '*', '*', '*', '6#3']],
@@ -372,9 +378,19 @@ public function matchCronExpressionDataProvider(): array
372378
['0-20/5', 21, false],
373379
['0-20/5', 25, false],
374380

381+
['3-20/5', 3, true],
382+
['3-20/5', 8, true],
383+
['3-20/5', 13, true],
384+
['3-20/5', 24, false],
385+
['3-20/5', 28, false],
386+
375387
['1/5', 5, false],
376388
['5/5', 5, true],
377389
['10/5', 10, true],
390+
391+
['4/5', 8, false],
392+
['8/5', 8, true],
393+
['13/5', 13, true],
378394
];
379395
}
380396

0 commit comments

Comments
 (0)