Compare commits

...
1 Commits
Author SHA1 Message Date
Fabian Gruber 1b0a3a2f0a fix: don't run disabled functions in schedules 2024-10-11 14:22:47 +02:00
2 changed files with 57 additions and 0 deletions
@@ -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 */
@@ -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([