From 1b0a3a2f0a8fcce6014b92b1a38bdc386864b641 Mon Sep 17 00:00:00 2001 From: Fabian Gruber Date: Fri, 11 Oct 2024 12:37:25 +0200 Subject: [PATCH] fix: don't run disabled functions in schedules --- src/Appwrite/Platform/Workers/Functions.php | 11 +++++ .../FunctionsScheduleTest.php | 46 +++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/src/Appwrite/Platform/Workers/Functions.php b/src/Appwrite/Platform/Workers/Functions.php index dfe7435426..b78361d741 100644 --- a/src/Appwrite/Platform/Workers/Functions.php +++ b/src/Appwrite/Platform/Workers/Functions.php @@ -130,6 +130,11 @@ class Functions extends Action if (!array_intersect($events, $function->getAttribute('events', []))) { continue; } + // filter disabled functions from getting executed. + if ($function->getAttribute('enabled', true) === false) { + continue; + } + Console::success('Iterating function: ' . $function->getAttribute('name')); $this->execute( @@ -322,6 +327,12 @@ class Functions extends Action $deploymentId = $function->getAttribute('deployment', ''); $spec = Config::getParam('runtime-specifications')[$function->getAttribute('specification', APP_FUNCTION_SPECIFICATION_DEFAULT)]; + if ($function->getAttribute('enabled', true) === false) { + $errorMessage = 'The function is disabled. Re-enable the function and try again.'; + $this->fail($errorMessage, $dbForProject, $function, $trigger, $path, $method, $user, $jwt, $event); + return; + } + $log->addTag('deploymentId', $deploymentId); /** Check if deployment exists */ diff --git a/tests/e2e/Services/FunctionsSchedule/FunctionsScheduleTest.php b/tests/e2e/Services/FunctionsSchedule/FunctionsScheduleTest.php index b1315103b1..79a79ff2ad 100644 --- a/tests/e2e/Services/FunctionsSchedule/FunctionsScheduleTest.php +++ b/tests/e2e/Services/FunctionsSchedule/FunctionsScheduleTest.php @@ -172,6 +172,52 @@ class FunctionsScheduleTest extends Scope $this->cleanupFunction($functionId, $executionId); } + public function testCreateScheduledExecutionForDisabledFunction() + { + $functionId = $this->setupFunction([ + 'functionId' => ID::unique(), + 'name' => 'Test Disabled', + 'enabled' => false, + 'execute' => [Role::user($this->getUser()['$id'])->toString()], + 'runtime' => 'php-8.0', + 'entrypoint' => 'index.php', + 'events' => [ + 'users.*.create', + 'users.*.delete', + ], + 'schedule' => '* * * * *', // Execute every 60 seconds + 'timeout' => 10, + ]); + + $this->setupDeployment($functionId, [ + 'entrypoint' => 'index.php', + 'code' => $this->packageFunction('php'), + 'activate' => true + ]); + + // Wait for scheduled execution + \sleep(60); + + $this->assertEventually(function () use ($functionId) { + $executions = $this->client->call(Client::METHOD_GET, '/functions/' . $functionId . '/executions', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ]); + + $this->assertEquals(200, $executions['headers']['status-code']); + $this->assertCount(1, $executions['body']['executions']); + + $asyncExecution = $executions['body']['executions'][0]; + + $this->assertEquals('schedule', $asyncExecution['trigger']); + $this->assertEquals('failed', $asyncExecution['status']); + $this->assertEquals('The function is disabled. Re-enable the function and try again.', $asyncExecution['errors']); + }, 60000, 500); + + $this->cleanupFunction($functionId); + } + public function testDeleteScheduledExecution() { $functionId = $this->setupFunction([