From 9b4585345f0e436497fd762dc1e96ac85ffa7953 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Thu, 28 May 2026 21:09:45 +0530 Subject: [PATCH 1/3] Fix impossible function cron schedules --- src/Appwrite/Platform/Tasks/ScheduleFunctions.php | 5 ++++- src/Appwrite/Task/Validator/Cron.php | 7 ++++++- tests/unit/Task/Validator/CronTest.php | 2 ++ 3 files changed, 12 insertions(+), 2 deletions(-) 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); } From 902b1d580e0fbd9f71a33b1816673a1f2a466a9a Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Fri, 29 May 2026 10:21:55 +0530 Subject: [PATCH 2/3] Fix deployment cancel race in build worker --- .../Modules/Functions/Workers/Builds.php | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php index f04f7c3807..95756addce 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -246,14 +246,18 @@ class Builds extends Action ->setParam($resourceKey, $resource->getId()) ->setParam('deploymentId', $deployment->getId()); + $deploymentId = $deployment->getId(); + $deployment = $dbForProject->getDocument('deployments', $deploymentId); + if ($deployment->isEmpty()) { + throw new \Exception('Deployment not found'); + } + if ($deployment->getAttribute('status') === 'canceled') { - $this->cancelDeployment($deployment->getId(), $dbForProject, $queueForRealtime); + $this->cancelDeployment($deploymentId, $dbForProject, $queueForRealtime); return; } - $deploymentId = $deployment->getId(); - $deployment->setAttribute('buildStartedAt', $startTime); $deployment->setAttribute('status', 'processing'); $deployment = $dbForProject->updateDocument('deployments', $deployment->getId(), new Document([ @@ -534,6 +538,12 @@ class Builds extends Action } /** Request the executor to build the code... */ + $deployment = $dbForProject->getDocument('deployments', $deploymentId); + if ($deployment->getAttribute('status') === 'canceled') { + $this->cancelDeployment($deploymentId, $dbForProject, $queueForRealtime); + return; + } + $deployment->setAttribute('status', 'building'); $deployment = $dbForProject->updateDocument('deployments', $deployment->getId(), new Document([ 'status' => 'building', From 939d2f41a86ba481cf3c01ffc3e0bcdd1b6a98ea Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Fri, 29 May 2026 10:54:24 +0530 Subject: [PATCH 3/3] Revert "Fix deployment cancel race in build worker" This reverts commit 902b1d580e0fbd9f71a33b1816673a1f2a466a9a. --- .../Modules/Functions/Workers/Builds.php | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php index 95756addce..f04f7c3807 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -246,18 +246,14 @@ class Builds extends Action ->setParam($resourceKey, $resource->getId()) ->setParam('deploymentId', $deployment->getId()); - $deploymentId = $deployment->getId(); - $deployment = $dbForProject->getDocument('deployments', $deploymentId); - if ($deployment->isEmpty()) { - throw new \Exception('Deployment not found'); - } - if ($deployment->getAttribute('status') === 'canceled') { - $this->cancelDeployment($deploymentId, $dbForProject, $queueForRealtime); + $this->cancelDeployment($deployment->getId(), $dbForProject, $queueForRealtime); return; } + $deploymentId = $deployment->getId(); + $deployment->setAttribute('buildStartedAt', $startTime); $deployment->setAttribute('status', 'processing'); $deployment = $dbForProject->updateDocument('deployments', $deployment->getId(), new Document([ @@ -538,12 +534,6 @@ class Builds extends Action } /** Request the executor to build the code... */ - $deployment = $dbForProject->getDocument('deployments', $deploymentId); - if ($deployment->getAttribute('status') === 'canceled') { - $this->cancelDeployment($deploymentId, $dbForProject, $queueForRealtime); - return; - } - $deployment->setAttribute('status', 'building'); $deployment = $dbForProject->updateDocument('deployments', $deployment->getId(), new Document([ 'status' => 'building',