mtdowling / cron-expression

CRON for PHP: Calculate the next or previous run date and determine if a CRON expression is due
http://mtdowling.com/blog/2012/06/03/cron-expressions-in-php/
MIT License
4.89k stars 339 forks source link

getMultipleRunDates fails with bigger $total values #66

Closed tuomassalo closed 8 years ago

tuomassalo commented 10 years ago

This works:

$cron=Cron\CronExpression::factory("0 0 * * *");
$cron->getMultipleRunDates(12, "2008-11-09 00:00:00");

This fails:

$cron=Cron\CronExpression::factory("0 0 * * *");
$cron->getMultipleRunDates(13, "2008-11-09 00:00:00");

with error:

[04-Nov-2014 09:51:22 Europe/Berlin] PHP Fatal error:  Uncaught exception 'RuntimeException' with message 'Impossible CRON expression' in .../mtdowling/cron-expression/src/Cron/CronExpression.php:321
Stack trace:
  #0 .../mtdowling/cron-expression/src/Cron/CronExpression.php(192): Cron\CronExpression->getRunDate('2008-11-09 00:0...', 12, false, false)
  #1 Command line code(1): Cron\CronExpression->getMultipleRunDates(13, '2008-11-09 00:0...')
  #2 {main}
  thrown in .../mtdowling/cron-expression/src/Cron/CronExpression.php on line 321

The same error happens with different values of $total depending on the cron expression.

tuomassalo commented 10 years ago

In some cases, this even happens for $total == 3:

$cron=Cron\CronExpression::factory("0 0 12 * * 2014");
$cron->getMultipleRunDates(3, "2014-11-04 00:00:00");'
webpunk commented 9 years ago

Just ran into the same problem. Is there any workaround?

webpunk commented 9 years ago

Okay. Probably not the best solutions but should be enough for a workaround:

$rd = null;
$rds = array();
for ($i = 0; $i < 50; $i++) {
    $rd = $cron->getNextRunDate($rd);
    $rds[] = $rd;                   
}

After this $rds is an array which contains the next 50 run dates.

gaomd commented 9 years ago

@webpunk Your workaround is exactly the getMultipleRunDates method did, and it fails, too.

One could increase the brute-force hard limit to workaround this Exception, just go ahead and increase 1000 to any bigger value you would like in src/Cron/CronExpression.php.

dragonmantank commented 8 years ago

Can you re-test this with v1.1.0? I just tested locally and this seems to have cleared up this issue.

dragonmantank commented 8 years ago

Closing since 1.1.0 seems to have cleared this up, and haven't heard anything back.