diff --git a/src/Appwrite/Platform/Tasks/ScheduleFunctions.php b/src/Appwrite/Platform/Tasks/ScheduleFunctions.php index c1a1891386..6153af6095 100644 --- a/src/Appwrite/Platform/Tasks/ScheduleFunctions.php +++ b/src/Appwrite/Platform/Tasks/ScheduleFunctions.php @@ -56,12 +56,15 @@ class ScheduleFunctions extends ScheduleBase foreach ($this->schedules as $key => $schedule) { try { $cron = new CronExpression($schedule['schedule']); + $nextDate = $cron->getNextRunDate(); } catch (\InvalidArgumentException) { // ignore invalid cron expressions continue; + } catch (\RuntimeException) { + // ignore impossible cron expressions + continue; } - $nextDate = $cron->getNextRunDate(); $next = DateTime::format($nextDate); $currentTick = $next < $timeFrame; diff --git a/src/Appwrite/Task/Validator/Cron.php b/src/Appwrite/Task/Validator/Cron.php index 03bd1c5220..18f3333a57 100644 --- a/src/Appwrite/Task/Validator/Cron.php +++ b/src/Appwrite/Task/Validator/Cron.php @@ -38,7 +38,12 @@ class Cron extends Validator return false; } - return true; + try { + (new CronExpression($value))->getNextRunDate(); + return true; + } catch (\RuntimeException) { + return false; + } } /** diff --git a/tests/unit/Task/Validator/CronTest.php b/tests/unit/Task/Validator/CronTest.php index 4f9d8ede24..3809c825b5 100644 --- a/tests/unit/Task/Validator/CronTest.php +++ b/tests/unit/Task/Validator/CronTest.php @@ -28,12 +28,14 @@ class CronTest extends TestCase $this->assertEquals($this->object->isValid('* * * * *'), true); // execute on every minutes // $this->assertEquals($this->object->isValid('0 17 * * sun'), true); // execute on every Sunday at 5 PM $this->assertEquals($this->object->isValid('*/10 * * * *'), true); // execute on every 10 minutes + $this->assertEquals($this->object->isValid('*/5 22-23,0-3 * * *'), true); // execute every 5 minutes from 10pm to 3am // $this->assertEquals($this->object->isValid('* * * jan,may,aug *'), true); // execute on selected months // $this->assertEquals($this->object->isValid('0 17 * * sun,fri'), true); // execute on selected days // $this->assertEquals($this->object->isValid('0 2 * * sun'), true); // execute on first sunday of every month $this->assertEquals($this->object->isValid('0 */4 * * *'), true); // execute on every four hours // $this->assertEquals($this->object->isValid('0 4,17 * * sun,mon'), true); // execute twice on every Sunday and Monday $this->assertEquals($this->object->isValid('bad expression'), false); + $this->assertEquals($this->object->isValid('*/5 22-3 * * *'), false); $this->assertEquals(null, false); $this->assertEquals('', false); }