From 70666dc22641625b1253f4c1ca922818cba464b6 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Thu, 9 Jul 2026 12:13:36 +0530 Subject: [PATCH 01/15] (feat): move execution log persistence to cloud --- Dockerfile | 1 - app/controllers/general.php | 7 +- app/init/resources.php | 6 -- app/listeners.php | 2 - bin/worker-executions | 3 - docker-compose.yml | 34 +-------- src/Appwrite/Bus/Events/ExecutionLogged.php | 18 +++++ src/Appwrite/Bus/Listeners/Log.php | 50 ------------- .../Functions/Http/Executions/Create.php | 14 ++++ src/Appwrite/Platform/Services/Workers.php | 2 - src/Appwrite/Platform/Workers/Executions.php | 71 ------------------- src/Appwrite/Platform/Workers/Functions.php | 26 +++++-- .../Functions/FunctionsCustomClientTest.php | 5 +- .../Functions/FunctionsCustomServerTest.php | 12 +--- .../FunctionsScheduleTest.php | 8 --- tests/resources/docker/docker-compose.yml | 21 ------ tests/unit/Docker/ComposeTest.php | 2 +- 17 files changed, 62 insertions(+), 220 deletions(-) delete mode 100755 bin/worker-executions create mode 100644 src/Appwrite/Bus/Events/ExecutionLogged.php delete mode 100644 src/Appwrite/Bus/Listeners/Log.php delete mode 100644 src/Appwrite/Platform/Workers/Executions.php diff --git a/Dockerfile b/Dockerfile index 4fed75ac30..086387fced 100755 --- a/Dockerfile +++ b/Dockerfile @@ -94,7 +94,6 @@ RUN chmod +x /usr/local/bin/doctor && \ chmod +x /usr/local/bin/worker-certificates && \ chmod +x /usr/local/bin/worker-databases && \ chmod +x /usr/local/bin/worker-deletes && \ - chmod +x /usr/local/bin/worker-executions && \ chmod +x /usr/local/bin/worker-functions && \ chmod +x /usr/local/bin/worker-mails && \ chmod +x /usr/local/bin/worker-messaging && \ diff --git a/app/controllers/general.php b/app/controllers/general.php index c8d266cf8d..6aa4505aba 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -722,10 +722,11 @@ function router(Http $utopia, Database $dbForPlatform, callable $getProjectDB, S spec: $spec, resource: $resource->getArrayCopy(), )); - } - $execution->setAttribute('logs', ''); - $execution->setAttribute('errors', ''); + $execution->setAttribute('logs', ''); + $execution->setAttribute('errors', ''); + $execution = $authorization->skip(fn () => $dbForProject->createDocument('executions', $execution)); + } $headers = []; foreach (($executionResponse['headers'] ?? []) as $key => $value) { diff --git a/app/init/resources.php b/app/init/resources.php index 019bdf1e34..744ed49c2a 100644 --- a/app/init/resources.php +++ b/app/init/resources.php @@ -7,7 +7,6 @@ use Appwrite\Event\Publisher\Build as BuildPublisher; use Appwrite\Event\Publisher\Certificate as CertificatePublisher; use Appwrite\Event\Publisher\Database as DatabasePublisher; use Appwrite\Event\Publisher\Delete as DeletePublisher; -use Appwrite\Event\Publisher\Execution as ExecutionPublisher; use Appwrite\Event\Publisher\Func as FunctionPublisher; use Appwrite\Event\Publisher\Mail as MailPublisher; use Appwrite\Event\Publisher\Messaging as MessagingPublisher; @@ -106,11 +105,6 @@ $container->set('publisherForUsage', fn (Publisher $publisher) => new UsagePubli new Queue(System::getEnv('_APP_STATS_USAGE_QUEUE_NAME', Event::STATS_USAGE_QUEUE_NAME)) ), ['publisher']); -$container->set('publisherForExecutions', fn (Publisher $publisher) => new ExecutionPublisher( - $publisher, - new Queue(System::getEnv('_APP_EXECUTIONS_QUEUE_NAME', Event::EXECUTIONS_QUEUE_NAME)) -), ['publisher']); - $container->set('publisherForFunctions', fn (Publisher $publisher) => new FunctionPublisher( $publisher, new Queue(System::getEnv('_APP_FUNCTIONS_QUEUE_NAME', Event::FUNCTIONS_QUEUE_NAME), 'utopia-queue', Event::FUNCTIONS_QUEUE_TTL) diff --git a/app/listeners.php b/app/listeners.php index 240225dc45..9f959ac6ab 100644 --- a/app/listeners.php +++ b/app/listeners.php @@ -1,11 +1,9 @@ $execution + * @param array $project + */ + public function __construct( + public readonly array $execution, + public readonly array $project, + ) { + } +} diff --git a/src/Appwrite/Bus/Listeners/Log.php b/src/Appwrite/Bus/Listeners/Log.php deleted file mode 100644 index e0376a4f81..0000000000 --- a/src/Appwrite/Bus/Listeners/Log.php +++ /dev/null @@ -1,50 +0,0 @@ -desc('Persists execution logs to database via queue') - ->inject('publisherForExecutions') - ->callback($this->handle(...)); - } - - public function handle(ExecutionCompleted $event, ExecutionPublisher $publisherForExecutions): void - { - $project = new Document($event->project); - $execution = new Document($event->execution); - - if ($execution->getAttribute('resourceType', '') === 'functions') { - Span::add('project.id', $project->getId()); - Span::add('function.id', $execution->getAttribute('resourceId', '')); - Span::add('execution.id', $execution->getId()); - Span::add('deployment.id', $execution->getAttribute('deploymentId', '')); - Span::add('execution.status', $execution->getAttribute('status', '')); - } - - $publisherForExecutions->enqueue(new ExecutionMessage( - project: $project, - execution: $execution, - )); - } -} diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Executions/Create.php b/src/Appwrite/Platform/Modules/Functions/Http/Executions/Create.php index bee8bba380..9ef3df9106 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Executions/Create.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Executions/Create.php @@ -3,6 +3,7 @@ namespace Appwrite\Platform\Modules\Functions\Http\Executions; use Ahc\Jwt\JWT; +use Appwrite\Bus\Events\ExecutionLogged; use Appwrite\Event\Event; use Appwrite\Event\Message\Delete as DeleteMessage; use Appwrite\Event\Message\Func as FunctionMessage; @@ -23,6 +24,7 @@ use Executor\Exception\Timeout as ExecutorTimeout; use Executor\Executor; use Utopia\Auth\Proofs\Token; use Utopia\Auth\Store; +use Utopia\Bus\Bus; use Utopia\Config\Config; use Utopia\Console; use Utopia\Database\Database; @@ -106,6 +108,7 @@ class Create extends Base ->inject('authorization') ->inject('publisherForDeletes') ->inject('executionsRetentionCount') + ->inject('bus') ->callback($this->action(...)); } @@ -134,6 +137,7 @@ class Create extends Base Authorization $authorization, DeletePublisher $publisherForDeletes, int $executionsRetentionCount, + Bus $bus, ) { $async = \strval($async) === 'true' || \strval($async) === '1'; @@ -329,6 +333,11 @@ class Create extends Base ->setAttribute('scheduledAt', $scheduledAt); $execution = $authorization->skip(fn () => $dbForProject->createDocument('executions', $execution)); + + $bus->dispatch(new ExecutionLogged( + execution: $execution->getArrayCopy(), + project: $project->getArrayCopy(), + )); } if ($executionsRetentionCount > 0 && ENABLE_EXECUTIONS_LIMIT_ON_ROUTE) { @@ -499,6 +508,11 @@ class Create extends Base ; $execution = $authorization->skip(fn () => $dbForProject->createDocument('executions', $execution)); + + $bus->dispatch(new ExecutionLogged( + execution: $execution->getArrayCopy(), + project: $project->getArrayCopy(), + )); } $executionResponse['headers']['x-appwrite-execution-id'] = $execution->getId(); diff --git a/src/Appwrite/Platform/Services/Workers.php b/src/Appwrite/Platform/Services/Workers.php index ee7a2a6958..4abe572311 100644 --- a/src/Appwrite/Platform/Services/Workers.php +++ b/src/Appwrite/Platform/Services/Workers.php @@ -4,7 +4,6 @@ namespace Appwrite\Platform\Services; use Appwrite\Platform\Workers\Certificates; use Appwrite\Platform\Workers\Deletes; -use Appwrite\Platform\Workers\Executions; use Appwrite\Platform\Workers\Functions; use Appwrite\Platform\Workers\Mails; use Appwrite\Platform\Workers\Messaging; @@ -21,7 +20,6 @@ class Workers extends Service $this ->addAction(Certificates::getName(), new Certificates()) ->addAction(Deletes::getName(), new Deletes()) - ->addAction(Executions::getName(), new Executions()) ->addAction(Functions::getName(), new Functions()) ->addAction(Mails::getName(), new Mails()) ->addAction(Messaging::getName(), new Messaging()) diff --git a/src/Appwrite/Platform/Workers/Executions.php b/src/Appwrite/Platform/Workers/Executions.php deleted file mode 100644 index 30b4a43a55..0000000000 --- a/src/Appwrite/Platform/Workers/Executions.php +++ /dev/null @@ -1,71 +0,0 @@ -desc('Executions worker') - ->groups(['executions']) - ->inject('message') - ->inject('dbForProject') - ->callback($this->action(...)); - } - - public function action( - Message $message, - Database $dbForProject, - ): void { - $payload = $message->getPayload(); - $isBatch = isset($payload['executions']) && \is_array($payload['executions']); - - if ($isBatch) { - $executionMessage = ExecutionsMessage::fromArray($payload); - $executions = \array_values(\array_filter( - $executionMessage->executions, - fn ($execution) => !$execution->isEmpty() - )); - } else { - $executionMessage = Execution::fromArray($payload); - $executions = [$executionMessage->execution]; - } - - if (empty($executions)) { - throw new Exception($isBatch ? 'Missing executions' : 'Missing execution'); - } - - Span::add('project.id', $executionMessage->project->getId()); - - if ($isBatch) { - Span::add('executions.count', \count($executions)); - $dbForProject->upsertDocuments('executions', $executions, self::UPSERT_BATCH_SIZE); - } else { - $execution = $executions[0]; - Span::add('function.id', $execution->getAttribute('resourceId', '')); - Span::add('execution.id', $execution->getId()); - Span::add('deployment.id', $execution->getAttribute('deploymentId', '')); - Span::add('resource.type', $execution->getAttribute('resourceType', '')); - $dbForProject->upsertDocument('executions', $execution); - } - } -} diff --git a/src/Appwrite/Platform/Workers/Functions.php b/src/Appwrite/Platform/Workers/Functions.php index cf21163809..1c7946e419 100644 --- a/src/Appwrite/Platform/Workers/Functions.php +++ b/src/Appwrite/Platform/Workers/Functions.php @@ -263,6 +263,7 @@ class Functions extends Action /** * @param string $message + * @param Database $dbForProject * @param Document $function * @param string $trigger * @param string $path @@ -274,6 +275,7 @@ class Functions extends Action */ private function fail( string $message, + Database $dbForProject, Document $project, Bus $bus, Document $function, @@ -313,19 +315,24 @@ class Functions extends Action 'requestPath' => $path, 'requestMethod' => $method, 'requestHeaders' => $headersFiltered, - 'errors' => $message, + 'errors' => '', 'logs' => '', 'duration' => 0.0, ]); + $executionForEvent = (new Document($execution->getArrayCopy())) + ->setAttribute('errors', $message); + Span::add('function.id', $function->getId()); Span::add('execution.id', $execution->getId()); Span::add('deployment.id', $execution->getAttribute('deploymentId', '')); Span::add('execution.trigger', $trigger); Span::add('execution.status', $execution->getAttribute('status', '')); + $dbForProject->upsertDocument('executions', $execution); + $bus->dispatch(new ExecutionCompleted( - execution: $execution->getArrayCopy(), + execution: $executionForEvent->getArrayCopy(), project: $project->getArrayCopy(), resource: $function->getArrayCopy(), )); @@ -391,19 +398,19 @@ class Functions extends Action if ($deployment->getAttribute('resourceId') !== $functionId) { $errorMessage = 'The execution could not be completed because a corresponding deployment was not found. A function deployment needs to be created before it can be executed. Please create a deployment for your function and try again.'; - $this->fail($errorMessage, $project, $bus, $function, $trigger, $path, $method, $user, $jwt, $event); + $this->fail($errorMessage, $dbForProject, $project, $bus, $function, $trigger, $path, $method, $user, $jwt, $event); return; } if ($deployment->isEmpty()) { $errorMessage = 'The execution could not be completed because a corresponding deployment was not found. A function deployment needs to be created before it can be executed. Please create a deployment for your function and try again.'; - $this->fail($errorMessage, $project, $bus, $function, $trigger, $path, $method, $user, $jwt, $event); + $this->fail($errorMessage, $dbForProject, $project, $bus, $function, $trigger, $path, $method, $user, $jwt, $event); return; } if ($deployment->getAttribute('status') !== 'ready') { $errorMessage = 'The execution could not be completed because the build is not ready. Please wait for the build to complete and try again.'; - $this->fail($errorMessage, $project, $bus, $function, $trigger, $path, $method, $user, $jwt, $event); + $this->fail($errorMessage, $dbForProject, $project, $bus, $function, $trigger, $path, $method, $user, $jwt, $event); return; } @@ -472,6 +479,8 @@ class Functions extends Action 'duration' => 0.0, ]); + $dbForProject->upsertDocument('executions', $execution); + $durationStart = \microtime(true); $body = $eventData ?? ''; @@ -627,6 +636,13 @@ class Functions extends Action /** Persist final execution status and record usage */ Span::add('execution.status', $execution->getAttribute('status', '')); + $dbForProject->updateDocument('executions', $execution->getId(), new Document([ + 'status' => $execution->getAttribute('status'), + 'responseStatusCode' => $execution->getAttribute('responseStatusCode'), + 'responseHeaders' => $execution->getAttribute('responseHeaders'), + 'duration' => $execution->getAttribute('duration'), + ])); + $bus->dispatch(new ExecutionCompleted( execution: $execution->getArrayCopy(), project: $project->getArrayCopy(), diff --git a/tests/e2e/Services/Functions/FunctionsCustomClientTest.php b/tests/e2e/Services/Functions/FunctionsCustomClientTest.php index c04631bf60..a4b9797377 100644 --- a/tests/e2e/Services/Functions/FunctionsCustomClientTest.php +++ b/tests/e2e/Services/Functions/FunctionsCustomClientTest.php @@ -592,9 +592,7 @@ final class FunctionsCustomClientTest extends Scope 'data' => ['name' => 'Test Document'], ]); $this->assertEquals(201, $document['headers']['status-code']); - $documentId = $document['body']['$id']; - - $this->assertEventually(function () use ($functionId, $documentId) { + $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'], @@ -607,7 +605,6 @@ final class FunctionsCustomClientTest extends Scope $lastExecution = $executions['body']['executions'][0]; $this->assertEquals('completed', $lastExecution['status']); $this->assertEquals(204, $lastExecution['responseStatusCode']); - $this->assertStringContainsString($documentId, (string) $lastExecution['logs']); }, 20000, 500); $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId, [ diff --git a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php index b5ff53828f..fc20c2c379 100644 --- a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php +++ b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php @@ -752,15 +752,13 @@ final class FunctionsCustomServerTest extends Scope $executionId = $execution['body']['$id'] ?? ''; - $this->assertEventually(function () use ($functionId, $executionId, $totalUsers) { + $this->assertEventually(function () use ($functionId, $executionId) { $execution = $this->getExecution($functionId, $executionId); $this->assertEquals(200, $execution['headers']['status-code']); $this->assertEquals(200, $execution['body']['responseStatusCode']); $this->assertEquals('completed', $execution['body']['status']); $this->assertEmpty($execution['body']['responseBody']); - $this->assertEmpty($execution['body']['errors']); - $this->assertStringContainsString("Total users: " . $totalUsers, (string) $execution['body']['logs']); }, 10000, 500); $deployment = $this->getDeployment($functionId, $deployment['body']['$id']); @@ -2130,8 +2128,6 @@ final class FunctionsCustomServerTest extends Scope $this->assertGreaterThan(2, $execution['body']['duration']); $this->assertLessThan(20, $execution['body']['duration']); $this->assertEquals('', $execution['body']['responseBody']); - $this->assertEquals('', $execution['body']['logs']); - $this->assertStringContainsString('timed out', (string) $execution['body']['errors']); }, 10000, 500); $this->cleanupFunction($functionId); @@ -2348,7 +2344,7 @@ final class FunctionsCustomServerTest extends Scope $userId = $user['body']['$id'] ?? ''; - $this->assertEventually(function () use ($functionId, $userId) { + $this->assertEventually(function () use ($functionId) { $executions = $this->listExecutions($functionId); $this->assertEquals(200, $executions['headers']['status-code']); @@ -2358,8 +2354,6 @@ final class FunctionsCustomServerTest extends Scope $this->assertEquals('completed', $lastExecution['status']); $this->assertEquals(204, $lastExecution['responseStatusCode']); - $this->assertStringContainsString($userId, (string) $lastExecution['logs']); - $this->assertStringContainsString('Event User', (string) $lastExecution['logs']); $this->assertNotEmpty($lastExecution['$id']); $headers = array_column($lastExecution['requestHeaders'] ?? [], 'value', 'name'); $this->assertEmpty($headers['x-appwrite-client-ip'] ?? ''); @@ -2427,8 +2421,6 @@ final class FunctionsCustomServerTest extends Scope $this->assertEquals('completed', $execution['body']['status']); $this->assertEquals(200, $execution['body']['responseStatusCode']); $this->assertGreaterThan(0, $execution['body']['duration']); - $this->assertNotEmpty($execution['body']['logs']); - $this->assertStringContainsString("total", (string) $execution['body']['logs']); }, 10000, 500); $this->cleanupFunction($functionId); diff --git a/tests/e2e/Services/FunctionsSchedule/FunctionsScheduleTest.php b/tests/e2e/Services/FunctionsSchedule/FunctionsScheduleTest.php index 946f4069aa..0e45a46f64 100644 --- a/tests/e2e/Services/FunctionsSchedule/FunctionsScheduleTest.php +++ b/tests/e2e/Services/FunctionsSchedule/FunctionsScheduleTest.php @@ -60,8 +60,6 @@ final class FunctionsScheduleTest extends Scope $this->assertEquals('completed', $asyncExecution['status']); $this->assertEquals(200, $asyncExecution['responseStatusCode']); $this->assertEquals('', $asyncExecution['responseBody']); - $this->assertNotEmpty($asyncExecution['logs']); - $this->assertNotEmpty($asyncExecution['errors']); $this->assertGreaterThan(0, $asyncExecution['duration']); $this->assertNotEmpty($asyncExecution['$id']); $headers = array_column($asyncExecution['requestHeaders'] ?? [], 'value', 'name'); @@ -134,12 +132,6 @@ final class FunctionsScheduleTest extends Scope $this->assertEquals('completed', $execution['body']['status']); $this->assertEquals('/custom-path', $execution['body']['requestPath']); $this->assertEquals('PATCH', $execution['body']['requestMethod']); - $this->assertStringContainsString('body-is-custom-body', (string) $execution['body']['logs']); - $this->assertStringContainsString('custom-header-is-custom-value', (string) $execution['body']['logs']); - $this->assertStringContainsString('method-is-patch', (string) $execution['body']['logs']); - $this->assertStringContainsString('path-is-/custom-path', (string) $execution['body']['logs']); - $this->assertStringContainsString('user-is-' . $this->getUser()['$id'], (string) $execution['body']['logs']); - $this->assertStringContainsString('jwt-is-valid', (string) $execution['body']['logs']); $this->assertGreaterThan(0, $execution['body']['duration']); }, 120000, 500); diff --git a/tests/resources/docker/docker-compose.yml b/tests/resources/docker/docker-compose.yml index 48bd9d8702..377e2ad3df 100644 --- a/tests/resources/docker/docker-compose.yml +++ b/tests/resources/docker/docker-compose.yml @@ -180,27 +180,6 @@ services: - _APP_DB_USER - _APP_DB_PASS - appwrite-worker-executions: - entrypoint: worker-executions - container_name: appwrite-worker-executions - build: - context: . - restart: unless-stopped - networks: - - appwrite - depends_on: - - redis - - mariadb - environment: - - _APP_ENV - - _APP_REDIS_HOST - - _APP_REDIS_PORT - - _APP_DB_HOST - - _APP_DB_PORT - - _APP_DB_SCHEMA - - _APP_DB_USER - - _APP_DB_PASS - appwrite-worker-functions: entrypoint: worker-functions container_name: appwrite-worker-functions diff --git a/tests/unit/Docker/ComposeTest.php b/tests/unit/Docker/ComposeTest.php index 9ab8c7adbe..902f71b38f 100644 --- a/tests/unit/Docker/ComposeTest.php +++ b/tests/unit/Docker/ComposeTest.php @@ -25,7 +25,7 @@ final class ComposeTest extends TestCase public function testServices(): void { - $this->assertCount(15, $this->object->getServices()); + $this->assertCount(14, $this->object->getServices()); $this->assertSame('appwrite', $this->object->getService('appwrite')->getContainerName()); $this->assertSame('', $this->object->getService('appwrite')->getImageVersion()); $this->assertSame('3.6', $this->object->getService('traefik')->getImageVersion()); From d50a58d71d89c998be533dc6100dcc937f2fb895 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Thu, 9 Jul 2026 12:37:23 +0530 Subject: [PATCH 02/15] (test): assert errors stay empty for template execution --- tests/e2e/Services/Functions/FunctionsCustomServerTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php index fc20c2c379..999037a8d6 100644 --- a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php +++ b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php @@ -759,6 +759,7 @@ final class FunctionsCustomServerTest extends Scope $this->assertEquals(200, $execution['body']['responseStatusCode']); $this->assertEquals('completed', $execution['body']['status']); $this->assertEmpty($execution['body']['responseBody']); + $this->assertEmpty($execution['body']['errors']); }, 10000, 500); $deployment = $this->getDeployment($functionId, $deployment['body']['$id']); From 4c258c923d9d2650560a71c1fe4de94e43ee8b66 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Thu, 9 Jul 2026 15:00:43 +0530 Subject: [PATCH 03/15] (refactor): meter sync executions via Usage listener and add ExecutionScheduled event --- ...utionLogged.php => ExecutionScheduled.php} | 2 +- .../Functions/Http/Executions/Create.php | 24 +++++-------------- 2 files changed, 7 insertions(+), 19 deletions(-) rename src/Appwrite/Bus/Events/{ExecutionLogged.php => ExecutionScheduled.php} (87%) diff --git a/src/Appwrite/Bus/Events/ExecutionLogged.php b/src/Appwrite/Bus/Events/ExecutionScheduled.php similarity index 87% rename from src/Appwrite/Bus/Events/ExecutionLogged.php rename to src/Appwrite/Bus/Events/ExecutionScheduled.php index eabdc618e8..ff4091a90e 100644 --- a/src/Appwrite/Bus/Events/ExecutionLogged.php +++ b/src/Appwrite/Bus/Events/ExecutionScheduled.php @@ -4,7 +4,7 @@ namespace Appwrite\Bus\Events; use Utopia\Bus\Event; -class ExecutionLogged implements Event +class ExecutionScheduled implements Event { /** * @param array $execution diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Executions/Create.php b/src/Appwrite/Platform/Modules/Functions/Http/Executions/Create.php index 9ef3df9106..21135b537a 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Executions/Create.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Executions/Create.php @@ -3,7 +3,8 @@ namespace Appwrite\Platform\Modules\Functions\Http\Executions; use Ahc\Jwt\JWT; -use Appwrite\Bus\Events\ExecutionLogged; +use Appwrite\Bus\Events\ExecutionCompleted; +use Appwrite\Bus\Events\ExecutionScheduled; use Appwrite\Event\Event; use Appwrite\Event\Message\Delete as DeleteMessage; use Appwrite\Event\Message\Func as FunctionMessage; @@ -17,7 +18,6 @@ use Appwrite\Platform\Modules\Compute\Base; use Appwrite\SDK\AuthType; use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; -use Appwrite\Usage\Context; use Appwrite\Utopia\Database\Documents\User; use Appwrite\Utopia\Response; use Executor\Exception\Timeout as ExecutorTimeout; @@ -98,7 +98,6 @@ class Create extends Base ->inject('dbForPlatform') ->inject('user') ->inject('queueForEvents') - ->inject('usage') ->inject('publisherForFunctions') ->inject('geoRecord') ->inject('store') @@ -127,7 +126,6 @@ class Create extends Base Database $dbForPlatform, User $user, Event $queueForEvents, - Context $usage, FunctionPublisher $publisherForFunctions, GeoRecord $geoRecord, Store $store, @@ -334,7 +332,7 @@ class Create extends Base $execution = $authorization->skip(fn () => $dbForProject->createDocument('executions', $execution)); - $bus->dispatch(new ExecutionLogged( + $bus->dispatch(new ExecutionScheduled( execution: $execution->getArrayCopy(), project: $project->getArrayCopy(), )); @@ -495,23 +493,13 @@ class Create extends Base throw $th; } } finally { - $usage - ->addMetric(METRIC_EXECUTIONS, 1) - ->addMetric(str_replace(['{resourceType}'], [RESOURCE_TYPE_FUNCTIONS], METRIC_RESOURCE_TYPE_EXECUTIONS), 1) - ->addMetric(str_replace(['{resourceType}', '{resourceInternalId}'], [RESOURCE_TYPE_FUNCTIONS, $function->getSequence()], METRIC_RESOURCE_TYPE_ID_EXECUTIONS), 1) - ->addMetric(METRIC_EXECUTIONS_COMPUTE, (int)($execution->getAttribute('duration') * 1000)) // per project - ->addMetric(str_replace(['{resourceType}'], [RESOURCE_TYPE_FUNCTIONS], METRIC_RESOURCE_TYPE_EXECUTIONS_COMPUTE), (int)($execution->getAttribute('duration') * 1000)) // per function - ->addMetric(str_replace(['{resourceType}', '{resourceInternalId}'], [RESOURCE_TYPE_FUNCTIONS, $function->getSequence()], METRIC_RESOURCE_TYPE_ID_EXECUTIONS_COMPUTE), (int)($execution->getAttribute('duration') * 1000)) // per function - ->addMetric(METRIC_EXECUTIONS_MB_SECONDS, (int)(($spec['memory'] ?? APP_COMPUTE_MEMORY_DEFAULT) * $execution->getAttribute('duration', 0) * ($spec['cpus'] ?? APP_COMPUTE_CPUS_DEFAULT))) - ->addMetric(str_replace(['{resourceType}'], [RESOURCE_TYPE_FUNCTIONS], METRIC_RESOURCE_TYPE_EXECUTIONS_MB_SECONDS), (int)(($spec['memory'] ?? APP_COMPUTE_MEMORY_DEFAULT) * $execution->getAttribute('duration', 0) * ($spec['cpus'] ?? APP_COMPUTE_CPUS_DEFAULT))) - ->addMetric(str_replace(['{resourceType}', '{resourceInternalId}'], [RESOURCE_TYPE_FUNCTIONS, $function->getSequence()], METRIC_RESOURCE_TYPE_ID_EXECUTIONS_MB_SECONDS), (int)(($spec['memory'] ?? APP_COMPUTE_MEMORY_DEFAULT) * $execution->getAttribute('duration', 0) * ($spec['cpus'] ?? APP_COMPUTE_CPUS_DEFAULT))) - ; - $execution = $authorization->skip(fn () => $dbForProject->createDocument('executions', $execution)); - $bus->dispatch(new ExecutionLogged( + $bus->dispatch(new ExecutionCompleted( execution: $execution->getArrayCopy(), project: $project->getArrayCopy(), + spec: $spec, + resource: $function->getArrayCopy(), )); } From 185cf7f76bbc44c437ab94bac50b9d1571c41c69 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Fri, 10 Jul 2026 11:36:26 +0530 Subject: [PATCH 04/15] (feat): drop executions collection schema and document writes from server-ce --- app/config/collections/projects.php | 271 ------------------ app/controllers/general.php | 1 - .../Functions/Http/Executions/Create.php | 5 - src/Appwrite/Platform/Workers/Functions.php | 19 +- 4 files changed, 3 insertions(+), 293 deletions(-) diff --git a/app/config/collections/projects.php b/app/config/collections/projects.php index 12f1534377..9b1f5bc74d 100644 --- a/app/config/collections/projects.php +++ b/app/config/collections/projects.php @@ -1994,277 +1994,6 @@ return [ ], ], - 'executions' => [ - '$collection' => ID::custom(Database::METADATA), - '$id' => ID::custom('executions'), - 'name' => 'Executions', - 'attributes' => [ - [ - '$id' => ID::custom('resourceInternalId'), - 'type' => Database::VAR_STRING, - 'format' => '', - 'size' => Database::LENGTH_KEY, - 'signed' => true, - 'required' => false, - 'default' => null, - 'array' => false, - 'filters' => [], - ], - [ - '$id' => ID::custom('resourceId'), - 'type' => Database::VAR_STRING, - 'format' => '', - 'size' => Database::LENGTH_KEY, - 'signed' => true, - 'required' => false, - 'default' => null, - 'array' => false, - 'filters' => [], - ], - [ - '$id' => ID::custom('resourceType'), - 'type' => Database::VAR_STRING, - 'format' => '', - 'size' => Database::LENGTH_KEY, - 'signed' => true, - 'required' => false, - 'default' => null, - 'array' => false, - 'filters' => [], - ], - [ - '$id' => ID::custom('deploymentInternalId'), - 'type' => Database::VAR_STRING, - 'format' => '', - 'size' => Database::LENGTH_KEY, - 'signed' => true, - 'required' => false, - 'default' => null, - 'array' => false, - 'filters' => [], - ], - [ - '$id' => ID::custom('deploymentId'), - 'type' => Database::VAR_STRING, - 'format' => '', - 'size' => Database::LENGTH_KEY, - 'signed' => true, - 'required' => false, - 'default' => null, - 'array' => false, - 'filters' => [], - ], - [ - 'array' => false, - '$id' => ID::custom('trigger'), - 'type' => Database::VAR_STRING, - 'format' => '', - 'size' => 128, - 'signed' => true, - 'required' => false, - 'default' => null, - 'filters' => [], - ], - [ - '$id' => ID::custom('status'), - 'type' => Database::VAR_STRING, - 'format' => '', - 'size' => 128, - 'signed' => true, - 'required' => false, - 'default' => null, - 'array' => false, - 'filters' => [], - ], - [ - '$id' => ID::custom('duration'), - 'type' => Database::VAR_FLOAT, - 'format' => '', - 'size' => 0, - 'signed' => true, - 'required' => false, - 'default' => null, - 'array' => false, - 'filters' => [], - ], - [ - '$id' => ID::custom('errors'), - 'type' => Database::VAR_STRING, - 'format' => '', - 'size' => APP_FUNCTION_ERROR_LENGTH_LIMIT, - 'signed' => true, - 'required' => false, - 'default' => null, - 'array' => false, - 'filters' => [], - ], - [ - '$id' => ID::custom('logs'), - 'type' => Database::VAR_STRING, - 'format' => '', - 'size' => APP_FUNCTION_LOG_LENGTH_LIMIT, - 'signed' => true, - 'required' => false, - 'default' => null, - 'array' => false, - 'filters' => [], - ], - [ - 'array' => false, - '$id' => ID::custom('requestMethod'), - 'type' => Database::VAR_STRING, - 'format' => '', - 'size' => 128, - 'signed' => true, - 'required' => false, - 'default' => null, - 'filters' => [], - ], - [ - 'array' => false, - '$id' => ID::custom('requestPath'), - 'type' => Database::VAR_STRING, - 'format' => '', - 'size' => 2048, - 'signed' => true, - 'required' => false, - 'default' => null, - 'filters' => [], - ], - [ - '$id' => ID::custom('requestHeaders'), - 'type' => Database::VAR_STRING, - 'format' => '', - 'size' => 16384, - 'signed' => true, - 'required' => false, - 'default' => null, - 'array' => false, - 'filters' => ['json'], - ], - [ - '$id' => ID::custom('responseStatusCode'), - 'type' => Database::VAR_INTEGER, - 'format' => '', - 'size' => 0, - 'signed' => true, - 'required' => false, - 'default' => null, - 'array' => false, - 'filters' => [], - ], - [ - '$id' => ID::custom('responseHeaders'), - 'type' => Database::VAR_STRING, - 'format' => '', - 'size' => 16384, - 'signed' => true, - 'required' => false, - 'default' => null, - 'array' => false, - 'filters' => ['json'], - ], - [ - '$id' => ID::custom('scheduledAt'), - 'type' => Database::VAR_DATETIME, - 'format' => '', - 'size' => 0, - 'signed' => false, - 'required' => false, - 'default' => null, - 'array' => false, - 'filters' => ['datetime'], - ], - [ - '$id' => ID::custom('scheduleInternalId'), - 'type' => Database::VAR_STRING, - 'format' => '', - 'size' => Database::LENGTH_KEY, - 'signed' => true, - 'required' => false, - 'default' => null, - 'array' => false, - 'filters' => [], - ], - [ - '$id' => ID::custom('scheduleId'), - 'type' => Database::VAR_STRING, - 'format' => '', - 'size' => Database::LENGTH_KEY, - 'signed' => true, - 'required' => false, - 'default' => null, - 'array' => false, - 'filters' => [], - ], - ], - 'indexes' => [ - [ - '$id' => ID::custom('_key_resource'), - 'type' => Database::INDEX_KEY, - 'attributes' => ['resourceInternalId', 'resourceType', 'resourceId'], - 'lengths' => [Database::LENGTH_KEY], - 'orders' => [Database::ORDER_ASC], - ], - [ - '$id' => ID::custom('_key_trigger'), - 'type' => Database::INDEX_KEY, - 'attributes' => ['trigger'], - 'lengths' => [32], - 'orders' => [Database::ORDER_ASC], - ], - [ - '$id' => ID::custom('_key_status'), - 'type' => Database::INDEX_KEY, - 'attributes' => ['status'], - 'lengths' => [32], - 'orders' => [Database::ORDER_ASC], - ], - [ - '$id' => ID::custom('_key_requestMethod'), - 'type' => Database::INDEX_KEY, - 'attributes' => ['requestMethod'], - 'lengths' => [128], - 'orders' => [Database::ORDER_ASC], - ], - [ - '$id' => ID::custom('_key_requestPath'), - 'type' => Database::INDEX_KEY, - 'attributes' => ['requestPath'], - 'lengths' => [Database::LENGTH_KEY], - 'orders' => [Database::ORDER_ASC], - ], - [ - '$id' => ID::custom('_key_deployment'), - 'type' => Database::INDEX_KEY, - 'attributes' => ['deploymentId'], - 'lengths' => [Database::LENGTH_KEY], - 'orders' => [Database::ORDER_ASC], - ], - [ - '$id' => ID::custom('_key_responseStatusCode'), - 'type' => Database::INDEX_KEY, - 'attributes' => ['responseStatusCode'], - 'lengths' => [], - 'orders' => [Database::ORDER_ASC], - ], - [ - '$id' => ID::custom('_key_duration'), - 'type' => Database::INDEX_KEY, - 'attributes' => ['duration'], - 'lengths' => [], - 'orders' => [Database::ORDER_ASC], - ], - [ - '$id' => ID::custom('_key_resourceType'), - 'type' => Database::INDEX_KEY, - 'attributes' => ['resourceType'], - 'lengths' => [], - 'orders' => [Database::ORDER_ASC], - ], - ], - ], - 'variables' => [ '$collection' => Database::METADATA, '$id' => 'variables', diff --git a/app/controllers/general.php b/app/controllers/general.php index 6aa4505aba..bbc75930fa 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -725,7 +725,6 @@ function router(Http $utopia, Database $dbForPlatform, callable $getProjectDB, S $execution->setAttribute('logs', ''); $execution->setAttribute('errors', ''); - $execution = $authorization->skip(fn () => $dbForProject->createDocument('executions', $execution)); } $headers = []; diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Executions/Create.php b/src/Appwrite/Platform/Modules/Functions/Http/Executions/Create.php index 21135b537a..da36709cfe 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Executions/Create.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Executions/Create.php @@ -289,7 +289,6 @@ class Create extends Base if ($async) { if (is_null($scheduledAt)) { - $execution = $authorization->skip(fn () => $dbForProject->createDocument('executions', $execution)); $publisherForFunctions->enqueue(new FunctionMessage( project: $project, user: $user, @@ -330,8 +329,6 @@ class Create extends Base ->setAttribute('scheduleInternalId', $schedule->getSequence()) ->setAttribute('scheduledAt', $scheduledAt); - $execution = $authorization->skip(fn () => $dbForProject->createDocument('executions', $execution)); - $bus->dispatch(new ExecutionScheduled( execution: $execution->getArrayCopy(), project: $project->getArrayCopy(), @@ -493,8 +490,6 @@ class Create extends Base throw $th; } } finally { - $execution = $authorization->skip(fn () => $dbForProject->createDocument('executions', $execution)); - $bus->dispatch(new ExecutionCompleted( execution: $execution->getArrayCopy(), project: $project->getArrayCopy(), diff --git a/src/Appwrite/Platform/Workers/Functions.php b/src/Appwrite/Platform/Workers/Functions.php index 1c7946e419..369b426af8 100644 --- a/src/Appwrite/Platform/Workers/Functions.php +++ b/src/Appwrite/Platform/Workers/Functions.php @@ -263,7 +263,6 @@ class Functions extends Action /** * @param string $message - * @param Database $dbForProject * @param Document $function * @param string $trigger * @param string $path @@ -275,7 +274,6 @@ class Functions extends Action */ private function fail( string $message, - Database $dbForProject, Document $project, Bus $bus, Document $function, @@ -329,8 +327,6 @@ class Functions extends Action Span::add('execution.trigger', $trigger); Span::add('execution.status', $execution->getAttribute('status', '')); - $dbForProject->upsertDocument('executions', $execution); - $bus->dispatch(new ExecutionCompleted( execution: $executionForEvent->getArrayCopy(), project: $project->getArrayCopy(), @@ -398,19 +394,19 @@ class Functions extends Action if ($deployment->getAttribute('resourceId') !== $functionId) { $errorMessage = 'The execution could not be completed because a corresponding deployment was not found. A function deployment needs to be created before it can be executed. Please create a deployment for your function and try again.'; - $this->fail($errorMessage, $dbForProject, $project, $bus, $function, $trigger, $path, $method, $user, $jwt, $event); + $this->fail($errorMessage, $project, $bus, $function, $trigger, $path, $method, $user, $jwt, $event); return; } if ($deployment->isEmpty()) { $errorMessage = 'The execution could not be completed because a corresponding deployment was not found. A function deployment needs to be created before it can be executed. Please create a deployment for your function and try again.'; - $this->fail($errorMessage, $dbForProject, $project, $bus, $function, $trigger, $path, $method, $user, $jwt, $event); + $this->fail($errorMessage, $project, $bus, $function, $trigger, $path, $method, $user, $jwt, $event); return; } if ($deployment->getAttribute('status') !== 'ready') { $errorMessage = 'The execution could not be completed because the build is not ready. Please wait for the build to complete and try again.'; - $this->fail($errorMessage, $dbForProject, $project, $bus, $function, $trigger, $path, $method, $user, $jwt, $event); + $this->fail($errorMessage, $project, $bus, $function, $trigger, $path, $method, $user, $jwt, $event); return; } @@ -479,8 +475,6 @@ class Functions extends Action 'duration' => 0.0, ]); - $dbForProject->upsertDocument('executions', $execution); - $durationStart = \microtime(true); $body = $eventData ?? ''; @@ -636,13 +630,6 @@ class Functions extends Action /** Persist final execution status and record usage */ Span::add('execution.status', $execution->getAttribute('status', '')); - $dbForProject->updateDocument('executions', $execution->getId(), new Document([ - 'status' => $execution->getAttribute('status'), - 'responseStatusCode' => $execution->getAttribute('responseStatusCode'), - 'responseHeaders' => $execution->getAttribute('responseHeaders'), - 'duration' => $execution->getAttribute('duration'), - ])); - $bus->dispatch(new ExecutionCompleted( execution: $execution->getArrayCopy(), project: $project->getArrayCopy(), From 2e8bf522a4b402c4503e66c28417558374c5140a Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Fri, 10 Jul 2026 12:15:39 +0530 Subject: [PATCH 05/15] (fix): adapt endpoints, schedules, and tests to unpersisted executions --- .../Functions/Http/Executions/Create.php | 5 +- .../Functions/Http/Executions/Delete.php | 35 ++- .../Modules/Functions/Http/Executions/Get.php | 8 +- .../Functions/Http/Executions/XList.php | 10 +- .../Modules/Sites/Http/Logs/Delete.php | 8 +- .../Platform/Modules/Sites/Http/Logs/Get.php | 8 +- .../Modules/Sites/Http/Logs/XList.php | 10 +- src/Appwrite/Platform/Tasks/ScheduleBase.php | 12 + .../Platform/Tasks/ScheduleExecutions.php | 7 +- src/Appwrite/Platform/Workers/Deletes.php | 21 +- .../Functions/FunctionsCustomClientTest.php | 22 +- .../Functions/FunctionsCustomServerTest.php | 226 ++---------------- .../FunctionsScheduleTest.php | 35 --- .../Services/GraphQL/FunctionsClientTest.php | 9 +- .../Services/GraphQL/FunctionsServerTest.php | 9 +- 15 files changed, 134 insertions(+), 291 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Executions/Create.php b/src/Appwrite/Platform/Modules/Functions/Http/Executions/Create.php index da36709cfe..44f2b7b362 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Executions/Create.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Executions/Create.php @@ -265,6 +265,8 @@ class Create extends Base $execution = new Document([ '$id' => $executionId, '$permissions' => !$user->isEmpty() ? [Permission::read(Role::user($user->getId()))] : [], + '$createdAt' => DateTime::now(), + '$updatedAt' => DateTime::now(), 'resourceInternalId' => $function->getSequence(), 'resourceId' => $function->getId(), 'resourceType' => 'functions', @@ -308,7 +310,8 @@ class Create extends Base 'path' => $path, 'method' => $method, 'body' => $body, - 'userId' => $user->getId() + 'userId' => $user->getId(), + 'functionId' => $function->getId(), ]; $schedule = $dbForPlatform->createDocument('schedules', new Document([ diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Executions/Delete.php b/src/Appwrite/Platform/Modules/Functions/Http/Executions/Delete.php index 227793f264..821d14684c 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Executions/Delete.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Executions/Delete.php @@ -13,6 +13,7 @@ use Appwrite\Utopia\Response; use Utopia\Database\Database; use Utopia\Database\DateTime; use Utopia\Database\Document; +use Utopia\Database\Exception\NotFound as NotFoundException; use Utopia\Database\Query; use Utopia\Database\Validator\Authorization; use Utopia\Database\Validator\UID; @@ -60,6 +61,7 @@ class Delete extends Base ->param('functionId', '', fn (Database $dbForProject) => new UID($dbForProject->getAdapter()->getMaxUIDLength()), 'Function ID.', false, ['dbForProject']) ->param('executionId', '', fn (Database $dbForProject) => new UID($dbForProject->getAdapter()->getMaxUIDLength()), 'Execution ID.', false, ['dbForProject']) ->inject('response') + ->inject('project') ->inject('dbForProject') ->inject('dbForPlatform') ->inject('queueForEvents') @@ -71,6 +73,7 @@ class Delete extends Base string $functionId, string $executionId, Response $response, + Document $project, Database $dbForProject, Database $dbForPlatform, Event $queueForEvents, @@ -82,9 +85,37 @@ class Delete extends Base throw new Exception(Exception::FUNCTION_NOT_FOUND); } - $execution = $dbForProject->getDocument('executions', $executionId); + try { + $execution = $dbForProject->getDocument('executions', $executionId); + } catch (NotFoundException) { + $execution = new Document(); + } + if ($execution->isEmpty()) { - throw new Exception(Exception::EXECUTION_NOT_FOUND); + // Executions are not persisted, but a pending scheduled execution + // can still be cancelled through its schedule. + $schedule = $authorization->skip(fn () => $dbForPlatform->findOne('schedules', [ + Query::equal('resourceId', [$executionId]), + Query::equal('resourceType', [SCHEDULE_RESOURCE_TYPE_EXECUTION]), + Query::equal('projectInternalId', [$project->getSequence()]), + Query::equal('active', [true]), + ])); + + if ($schedule->isEmpty()) { + throw new Exception(Exception::EXECUTION_NOT_FOUND); + } + + $authorization->skip(fn () => $dbForPlatform->updateDocument('schedules', $schedule->getId(), new Document([ + 'resourceUpdatedAt' => DateTime::now(), + 'active' => false, + ]))); + + $queueForEvents + ->setParam('functionId', $function->getId()) + ->setParam('executionId', $executionId); + + $response->noContent(); + return; } if ($execution->getAttribute('resourceType') !== 'functions' && $execution->getAttribute('resourceInternalId') !== $function->getSequence()) { diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Executions/Get.php b/src/Appwrite/Platform/Modules/Functions/Http/Executions/Get.php index 027ff51166..4c351712d8 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Executions/Get.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Executions/Get.php @@ -10,6 +10,8 @@ use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Database\Documents\User; use Appwrite\Utopia\Response; use Utopia\Database\Database; +use Utopia\Database\Document; +use Utopia\Database\Exception\NotFound as NotFoundException; use Utopia\Database\Validator\Authorization; use Utopia\Database\Validator\UID; use Utopia\Platform\Action; @@ -75,7 +77,11 @@ class Get extends Base throw new Exception(Exception::FUNCTION_NOT_FOUND); } - $execution = $dbForProject->getDocument('executions', $executionId); + try { + $execution = $dbForProject->getDocument('executions', $executionId); + } catch (NotFoundException) { + $execution = new Document(); + } if ($execution->getAttribute('resourceType') !== 'functions' || $execution->getAttribute('resourceInternalId') !== $function->getSequence()) { throw new Exception(Exception::EXECUTION_NOT_FOUND); diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Executions/XList.php b/src/Appwrite/Platform/Modules/Functions/Http/Executions/XList.php index 29bed70cd0..772634d512 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Executions/XList.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Executions/XList.php @@ -13,6 +13,7 @@ use Appwrite\Utopia\Response; use Utopia\Database\Database; use Utopia\Database\DateTime; use Utopia\Database\Document; +use Utopia\Database\Exception\NotFound as NotFoundException; use Utopia\Database\Exception\Order as OrderException; use Utopia\Database\Exception\Query as QueryException; use Utopia\Database\Query; @@ -105,7 +106,11 @@ class XList extends Base } $executionId = $cursor->getValue(); - $cursorDocument = $dbForProject->getDocument('executions', $executionId); + try { + $cursorDocument = $dbForProject->getDocument('executions', $executionId); + } catch (NotFoundException) { + $cursorDocument = new Document(); + } if ($cursorDocument->isEmpty()) { throw new Exception(Exception::GENERAL_CURSOR_NOT_FOUND, "Execution '{$executionId}' for the 'cursor' value not found."); @@ -148,6 +153,9 @@ class XList extends Base try { $results = $dbForProject->find('executions', $queries); $total = $includeTotal ? $dbForProject->count('executions', $filterQueries, APP_LIMIT_COUNT) : 0; + } catch (NotFoundException) { + $results = []; + $total = 0; } catch (OrderException $e) { throw new Exception(Exception::DATABASE_QUERY_ORDER_NULL, "The order attribute '{$e->getAttribute()}' had a null value. Cursor pagination requires all documents order attribute values are non-null."); } diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Logs/Delete.php b/src/Appwrite/Platform/Modules/Sites/Http/Logs/Delete.php index 88927b9fb4..bf0d43f977 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Logs/Delete.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Logs/Delete.php @@ -10,6 +10,8 @@ use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; use Utopia\Database\Database; +use Utopia\Database\Document; +use Utopia\Database\Exception\NotFound as NotFoundException; use Utopia\Database\Validator\UID; use Utopia\Platform\Action; use Utopia\Platform\Scope\HTTP; @@ -67,7 +69,11 @@ class Delete extends Base throw new Exception(Exception::SITE_NOT_FOUND); } - $log = $dbForProject->getDocument('executions', $logId); + try { + $log = $dbForProject->getDocument('executions', $logId); + } catch (NotFoundException) { + $log = new Document(); + } if ($log->isEmpty()) { throw new Exception(Exception::LOG_NOT_FOUND); } diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Logs/Get.php b/src/Appwrite/Platform/Modules/Sites/Http/Logs/Get.php index c482add2da..0e00e23c38 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Logs/Get.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Logs/Get.php @@ -9,6 +9,8 @@ use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; use Utopia\Database\Database; +use Utopia\Database\Document; +use Utopia\Database\Exception\NotFound as NotFoundException; use Utopia\Database\Validator\UID; use Utopia\Platform\Action; use Utopia\Platform\Scope\HTTP; @@ -61,7 +63,11 @@ class Get extends Base throw new Exception(Exception::SITE_NOT_FOUND); } - $log = $dbForProject->getDocument('executions', $logId); + try { + $log = $dbForProject->getDocument('executions', $logId); + } catch (NotFoundException) { + $log = new Document(); + } if ($log->getAttribute('resourceType') !== 'sites' && $log->getAttribute('resourceInternalId') !== $site->getSequence()) { throw new Exception(Exception::LOG_NOT_FOUND); diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Logs/XList.php b/src/Appwrite/Platform/Modules/Sites/Http/Logs/XList.php index 89fb3ee2e6..66b86150db 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Logs/XList.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Logs/XList.php @@ -13,6 +13,7 @@ use Appwrite\Utopia\Response; use Utopia\Database\Database; use Utopia\Database\DateTime; use Utopia\Database\Document; +use Utopia\Database\Exception\NotFound as NotFoundException; use Utopia\Database\Exception\Order as OrderException; use Utopia\Database\Exception\Query as QueryException; use Utopia\Database\Query; @@ -91,7 +92,11 @@ class XList extends Base } $logId = $cursor->getValue(); - $cursorDocument = $dbForProject->getDocument('executions', $logId); + try { + $cursorDocument = $dbForProject->getDocument('executions', $logId); + } catch (NotFoundException) { + $cursorDocument = new Document(); + } if ($cursorDocument->isEmpty() || $cursorDocument->getAttribute('resourceType') !== 'sites') { throw new Exception(Exception::GENERAL_CURSOR_NOT_FOUND, "Log '{$logId}' for the 'cursor' value not found."); @@ -134,6 +139,9 @@ class XList extends Base try { $results = $dbForProject->find('executions', $queries); $total = $includeTotal ? $dbForProject->count('executions', $filterQueries, APP_LIMIT_COUNT) : 0; + } catch (NotFoundException) { + $results = []; + $total = 0; } catch (OrderException $e) { throw new Exception(Exception::DATABASE_QUERY_ORDER_NULL, "The order attribute '{$e->getAttribute()}' had a null value. Cursor pagination requires all documents order attribute values are non-null."); } diff --git a/src/Appwrite/Platform/Tasks/ScheduleBase.php b/src/Appwrite/Platform/Tasks/ScheduleBase.php index dfa33fad38..554dbf558b 100644 --- a/src/Appwrite/Platform/Tasks/ScheduleBase.php +++ b/src/Appwrite/Platform/Tasks/ScheduleBase.php @@ -35,6 +35,11 @@ abstract class ScheduleBase extends Action abstract public static function getName(): string; abstract public static function getSupportedResource(): string; abstract public static function getCollectionId(): string; + + public static function loadResource(): bool + { + return true; + } abstract protected function enqueueResources(Database $dbForPlatform, callable $getProjectDB): void; public function __construct() @@ -268,6 +273,13 @@ abstract class ScheduleBase extends Action $this->schedules[$sequence]['project'] = $project; + if (!static::loadResource()) { + $this->schedules[$sequence]['resource'] = new Document([ + '$id' => $schedule['resourceId'], + ]); + continue; + } + // In case the resource is not found (project deleted). try { $resource = $getProjectDB($project)->getDocument(static::getCollectionId(), $schedule['resourceId']); diff --git a/src/Appwrite/Platform/Tasks/ScheduleExecutions.php b/src/Appwrite/Platform/Tasks/ScheduleExecutions.php index 49dd851b6d..51d6662250 100644 --- a/src/Appwrite/Platform/Tasks/ScheduleExecutions.php +++ b/src/Appwrite/Platform/Tasks/ScheduleExecutions.php @@ -33,6 +33,11 @@ class ScheduleExecutions extends ScheduleBase return RESOURCE_TYPE_EXECUTIONS; } + public static function loadResource(): bool + { + return false; + } + protected function enqueueResources(Database $dbForPlatform, callable $getProjectDB): void { $intervalEnd = (new \DateTime())->modify('+' . self::ENQUEUE_TIMER . ' seconds'); @@ -75,7 +80,7 @@ class ScheduleExecutions extends ScheduleBase $publisherForFunctions->enqueue(new FunctionMessage( project: $schedule['project'], userId: $data['userId'] ?? '', - functionId: $schedule['resource']['resourceId'], + functionId: $data['functionId'] ?? '', execution: $schedule['resource'], type: 'schedule', body: $data['body'] ?? '', diff --git a/src/Appwrite/Platform/Workers/Deletes.php b/src/Appwrite/Platform/Workers/Deletes.php index 38b7958a0e..005c6f0bea 100644 --- a/src/Appwrite/Platform/Workers/Deletes.php +++ b/src/Appwrite/Platform/Workers/Deletes.php @@ -23,6 +23,7 @@ use Utopia\Database\DateTime; use Utopia\Database\Document; use Utopia\Database\Exception as DatabaseException; use Utopia\Database\Exception\Conflict; +use Utopia\Database\Exception\NotFound as NotFoundException; use Utopia\Database\Exception\Restricted; use Utopia\Database\Exception\Structure; use Utopia\Database\Query; @@ -1076,14 +1077,18 @@ class Deletes extends Action /* delete log for a given $resourceInternalId */ $delete = function (Database $dbForProject, string $resourceInternalId, string $resourceType) use ($executionsRetentionCount) { // get the execution at position `N+1` - $execution = $dbForProject->findOne('executions', [ - Query::select(['$createdAt']), - Query::equal('resourceInternalId', [$resourceInternalId]), - Query::equal('resourceType', [$resourceType]), - Query::orderDesc('$createdAt'), - Query::orderDesc(), - Query::offset($executionsRetentionCount), - ]); + try { + $execution = $dbForProject->findOne('executions', [ + Query::select(['$createdAt']), + Query::equal('resourceInternalId', [$resourceInternalId]), + Query::equal('resourceType', [$resourceType]), + Query::orderDesc('$createdAt'), + Query::orderDesc(), + Query::offset($executionsRetentionCount), + ]); + } catch (NotFoundException) { + return; + } if (!$execution->isEmpty()) { // delete everything older diff --git a/tests/e2e/Services/Functions/FunctionsCustomClientTest.php b/tests/e2e/Services/Functions/FunctionsCustomClientTest.php index a4b9797377..077dc71f4b 100644 --- a/tests/e2e/Services/Functions/FunctionsCustomClientTest.php +++ b/tests/e2e/Services/Functions/FunctionsCustomClientTest.php @@ -219,14 +219,8 @@ final class FunctionsCustomClientTest extends Scope 'body' => 'foobar', 'async' => true ]); - $executionId = $execution['body']['$id']; $this->assertEquals(202, $execution['headers']['status-code']); - - $this->assertEventually(function () use ($functionId, $executionId) { - $execution = $this->getExecution($functionId, $executionId); - $this->assertEquals('completed', $execution['body']['status']); - $this->assertEquals(200, $execution['body']['responseStatusCode']); - }, 10000, 500); + $this->assertNotEmpty($execution['body']['$id']); return [ 'functionId' => $functionId @@ -592,20 +586,6 @@ final class FunctionsCustomClientTest extends Scope 'data' => ['name' => 'Test Document'], ]); $this->assertEquals(201, $document['headers']['status-code']); - $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->assertGreaterThan(0, count($executions['body']['executions']), 'Function should have been triggered by document creation'); - - $lastExecution = $executions['body']['executions'][0]; - $this->assertEquals('completed', $lastExecution['status']); - $this->assertEquals(204, $lastExecution['responseStatusCode']); - }, 20000, 500); $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId, [ 'content-type' => 'application/json', diff --git a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php index 999037a8d6..94b322a117 100644 --- a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php +++ b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php @@ -750,18 +750,6 @@ final class FunctionsCustomServerTest extends Scope $this->assertNotEmpty($execution['body']['$id']); $this->assertEquals('waiting', $execution['body']['status']); - $executionId = $execution['body']['$id'] ?? ''; - - $this->assertEventually(function () use ($functionId, $executionId) { - $execution = $this->getExecution($functionId, $executionId); - - $this->assertEquals(200, $execution['headers']['status-code']); - $this->assertEquals(200, $execution['body']['responseStatusCode']); - $this->assertEquals('completed', $execution['body']['status']); - $this->assertEmpty($execution['body']['responseBody']); - $this->assertEmpty($execution['body']['errors']); - }, 10000, 500); - $deployment = $this->getDeployment($functionId, $deployment['body']['$id']); $this->assertEquals(200, $deployment['headers']['status-code']); $this->assertGreaterThan(0, $deployment['body']['sourceSize']); @@ -1618,12 +1606,12 @@ final class FunctionsCustomServerTest extends Scope $this->assertIsArray($execution['body']['responseHeaders']); $this->assertEmpty($execution['body']['responseBody']); // For HEAD requests, response body is empty - /** Delete execution */ + /** Executions are not persisted, so deletes return 404 */ $execution = $this->client->call(Client::METHOD_DELETE, '/functions/' . $data['functionId'] . '/executions/' . $execution['body']['$id'], array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), []); - $this->assertEquals(204, $execution['headers']['status-code']); + $this->assertEquals(404, $execution['headers']['status-code']); /** Test create execution with 400 status code */ $execution = $this->createExecution($data['functionId'], [ @@ -1635,12 +1623,12 @@ final class FunctionsCustomServerTest extends Scope $this->assertEquals('completed', $execution['body']['status']); $this->assertEquals(400, $execution['body']['responseStatusCode']); - /** Delete execution */ + /** Executions are not persisted, so deletes return 404 */ $execution = $this->client->call(Client::METHOD_DELETE, '/functions/' . $data['functionId'] . '/executions/' . $execution['body']['$id'], array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), []); - $this->assertEquals(204, $execution['headers']['status-code']); + $this->assertEquals(404, $execution['headers']['status-code']); } public function testListExecutions(): void @@ -1648,29 +1636,21 @@ final class FunctionsCustomServerTest extends Scope $data = $this->setupTestExecution(); /** - * Test for SUCCESS + * Executions are not persisted, so lists are always empty */ $executions = $this->listExecutions($data['functionId']); $this->assertEquals(200, $executions['headers']['status-code']); - $this->assertGreaterThanOrEqual(1, $executions['body']['total']); + $this->assertEquals(0, $executions['body']['total']); $this->assertIsArray($executions['body']['executions']); - $this->assertGreaterThanOrEqual(1, count($executions['body']['executions'])); - // Verify our execution is in the list (don't assume position) - $deploymentIds = array_column($executions['body']['executions'], 'deploymentId'); - $this->assertContains($data['deploymentId'], $deploymentIds); + $this->assertCount(0, $executions['body']['executions']); - /** - * Test for SUCCESS with total=false - */ $executionsWithIncludeTotalFalse = $this->listExecutions($data['functionId'], ['total' => false]); $this->assertEquals(200, $executionsWithIncludeTotalFalse['headers']['status-code']); - $this->assertIsArray($executionsWithIncludeTotalFalse['body']); $this->assertIsArray($executionsWithIncludeTotalFalse['body']['executions']); - $this->assertIsInt($executionsWithIncludeTotalFalse['body']['total']); $this->assertSame(0, $executionsWithIncludeTotalFalse['body']['total']); - $this->assertGreaterThan(0, count($executionsWithIncludeTotalFalse['body']['executions'])); + $this->assertCount(0, $executionsWithIncludeTotalFalse['body']['executions']); $executions = $this->listExecutions($data['functionId'], [ 'queries' => [ @@ -1678,75 +1658,17 @@ final class FunctionsCustomServerTest extends Scope ], ]); - $this->assertEquals(200, $executions['headers']['status-code']); - $this->assertGreaterThanOrEqual(1, $executions['body']['total']); - $this->assertIsArray($executions['body']['executions']); - $this->assertGreaterThanOrEqual(1, count($executions['body']['executions'])); - - $executions = $this->listExecutions($data['functionId'], [ - 'queries' => [ - Query::equal('deploymentId', ['some-random-id'])->toString(), - ], - ]); - $this->assertEquals(200, $executions['headers']['status-code']); $this->assertEquals(0, $executions['body']['total']); - $this->assertIsArray($executions['body']['executions']); $this->assertCount(0, $executions['body']['executions']); - $executions = $this->listExecutions($data['functionId'], [ - 'queries' => [ - Query::limit(1)->toString(), - ], - ]); - - $this->assertEquals(200, $executions['headers']['status-code']); - $this->assertCount(1, $executions['body']['executions']); - - $executions = $this->listExecutions($data['functionId'], [ - 'queries' => [ - Query::offset(0)->toString(), - ], - ]); - - $this->assertEquals(200, $executions['headers']['status-code']); - $this->assertGreaterThanOrEqual(1, count($executions['body']['executions'])); - - $executions = $this->listExecutions($data['functionId'], [ - 'queries' => [ - Query::equal('trigger', ['http'])->toString(), - ], - ]); - - $this->assertEquals(200, $executions['headers']['status-code']); - $this->assertGreaterThanOrEqual(1, count($executions['body']['executions'])); - - /** - * Test search queries - */ $executions = $this->listExecutions($data['functionId'], [ 'search' => $data['executionId'], ]); $this->assertEquals(200, $executions['headers']['status-code']); - $this->assertGreaterThanOrEqual(1, $executions['body']['total']); - $this->assertIsInt($executions['body']['total']); - $this->assertGreaterThanOrEqual(1, count($executions['body']['executions'])); - // Verify our function's execution is in the results - $functionIds = array_column($executions['body']['executions'], 'functionId'); - $this->assertContains($data['functionId'], $functionIds); - - $executions = $this->listExecutions($data['functionId'], [ - 'search' => $data['functionId'], - ]); - - $this->assertEquals(200, $executions['headers']['status-code']); - $this->assertGreaterThanOrEqual(1, $executions['body']['total']); - $this->assertIsInt($executions['body']['total']); - $this->assertGreaterThanOrEqual(1, count($executions['body']['executions'])); - // Verify our execution is in the results - $executionIds = array_column($executions['body']['executions'], '$id'); - $this->assertContains($data['executionId'], $executionIds); + $this->assertEquals(0, $executions['body']['total']); + $this->assertCount(0, $executions['body']['executions']); } public function testSyncCreateExecution(): void @@ -1776,17 +1698,12 @@ final class FunctionsCustomServerTest extends Scope $data = $this->setupTestExecution(); /** - * Test for SUCCESS + * Executions are not persisted, so gets return 404 */ $execution = $this->getExecution($data['functionId'], $data['executionId']); - $this->assertEquals(200, $execution['headers']['status-code']); - $this->assertEquals($data['executionId'], $execution['body']['$id']); - $this->assertEquals($data['deploymentId'], $execution['body']['deploymentId']); + $this->assertEquals(404, $execution['headers']['status-code']); - /** - * Test for FAILURE - */ $function = $this->getExecution($data['functionId'], 'x'); $this->assertEquals(404, $function['headers']['status-code']); @@ -1806,27 +1723,16 @@ final class FunctionsCustomServerTest extends Scope $executionId = $execution['body']['$id'] ?? ''; /** - * Test for SUCCESS + * Executions are not persisted, so deletes return 404 */ $execution = $this->client->call(Client::METHOD_DELETE, '/functions/' . $data['functionId'] . '/executions/' . $executionId, array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders())); - $this->assertEquals(204, $execution['headers']['status-code']); - $this->assertEmpty($execution['body']); - - $execution = $this->client->call(Client::METHOD_DELETE, '/functions/' . $data['functionId'] . '/executions/' . $executionId, array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders())); - $this->assertEquals(404, $execution['headers']['status-code']); $this->assertStringContainsString('Execution with the requested ID could not be found', (string) $execution['body']['message']); - /** - * Test for FAILURE - */ $execution = $this->client->call(Client::METHOD_POST, '/functions/' . $data['functionId'] . '/executions', array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], @@ -1843,9 +1749,7 @@ final class FunctionsCustomServerTest extends Scope 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders())); - $this->assertEquals(400, $execution['headers']['status-code']); - $this->assertStringContainsString('execution_in_progress', (string) $execution['body']['type']); - $this->assertStringContainsString('Can\'t delete ongoing execution.', (string) $execution['body']['message']); + $this->assertEquals(404, $execution['headers']['status-code']); } @@ -2116,20 +2020,8 @@ final class FunctionsCustomServerTest extends Scope $this->assertEquals(202, $execution['headers']['status-code']); - $executionId = $execution['body']['$id'] ?? ''; - - \sleep(5); // Wait for the function to timeout - - $this->assertEventually(function () use ($functionId, $executionId) { - $execution = $this->getExecution($functionId, $executionId); - - $this->assertEquals(200, $execution['headers']['status-code']); - $this->assertEquals('failed', $execution['body']['status']); - $this->assertEquals(500, $execution['body']['responseStatusCode']); - $this->assertGreaterThan(2, $execution['body']['duration']); - $this->assertLessThan(20, $execution['body']['duration']); - $this->assertEquals('', $execution['body']['responseBody']); - }, 10000, 500); + $this->assertNotEmpty($execution['body']['$id']); + $this->assertEquals('waiting', $execution['body']['status']); $this->cleanupFunction($functionId); } @@ -2179,18 +2071,8 @@ final class FunctionsCustomServerTest extends Scope $this->assertEmpty($execution['body']['logs']); $this->assertEmpty($execution['body']['errors']); - $executionId = $execution['body']['$id'] ?? ''; - - $executions = $this->listExecutions($functionId); - - $this->assertEquals(200, $executions['headers']['status-code']); - $this->assertEquals(1, $executions['body']['total']); - $this->assertIsArray($executions['body']['executions']); - $this->assertCount(1, $executions['body']['executions']); - $this->assertEquals($executions['body']['executions'][0]['$id'], $executionId); - $this->assertEquals('http', $executions['body']['executions'][0]['trigger']); - $this->assertEquals(200, $executions['body']['executions'][0]['responseStatusCode']); - $this->assertEmpty($executions['body']['executions'][0]['responseBody']); + $this->assertNotEmpty($execution['body']['$id']); + $this->assertEquals('http', $execution['body']['trigger']); $this->cleanupFunction($functionId); } @@ -2345,21 +2227,6 @@ final class FunctionsCustomServerTest extends Scope $userId = $user['body']['$id'] ?? ''; - $this->assertEventually(function () use ($functionId) { - $executions = $this->listExecutions($functionId); - - $this->assertEquals(200, $executions['headers']['status-code']); - $executionsList = $executions['body']['executions'] ?? []; - $this->assertNotEmpty($executionsList); - $lastExecution = $executionsList[0]; - - $this->assertEquals('completed', $lastExecution['status']); - $this->assertEquals(204, $lastExecution['responseStatusCode']); - $this->assertNotEmpty($lastExecution['$id']); - $headers = array_column($lastExecution['requestHeaders'] ?? [], 'value', 'name'); - $this->assertEmpty($headers['x-appwrite-client-ip'] ?? ''); - }, 20_000, 500); - $this->cleanupFunction($functionId); // Cleanup user @@ -2413,17 +2280,6 @@ final class FunctionsCustomServerTest extends Scope $this->assertEquals(202, $execution['headers']['status-code']); $this->assertNotEmpty($execution['body']['$id']); - $executionId = $execution['body']['$id'] ?? ''; - - $this->assertEventually(function () use ($functionId, $executionId) { - $execution = $this->getExecution($functionId, $executionId); - - $this->assertEquals(200, $execution['headers']['status-code']); - $this->assertEquals('completed', $execution['body']['status']); - $this->assertEquals(200, $execution['body']['responseStatusCode']); - $this->assertGreaterThan(0, $execution['body']['duration']); - }, 10000, 500); - $this->cleanupFunction($functionId); } @@ -2503,17 +2359,6 @@ final class FunctionsCustomServerTest extends Scope $this->assertArrayHasKey('x-appwrite-execution-id', $response['headers']); $this->assertNotEmpty($response['headers']['x-appwrite-execution-id']); - // Async execution document creation - $this->assertEventually(function () use ($functionId) { - $executions = $this->client->call(Client::METHOD_GET, '/functions/' . $functionId . '/executions', array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders()), []); - - $this->assertEquals(200, $executions['headers']['status-code']); - $this->assertCount(1, $executions['body']['executions']); - }); - $this->cleanupFunction($functionId); } @@ -2725,17 +2570,6 @@ final class FunctionsCustomServerTest extends Scope $this->assertEmpty($execution['body']['errors']); $this->assertNotEmpty($execution['body']['$id']); - $executionId = $execution['body']['$id'] ?? ''; - - $this->assertEventually(function () use ($functionId, $executionId) { - $execution = $this->getExecution($functionId, $executionId); - - $this->assertEquals(200, $execution['headers']['status-code']); - $this->assertEquals('completed', $execution['body']['status']); - $this->assertEmpty($execution['body']['logs']); - $this->assertEmpty($execution['body']['errors']); - }, 10000, 500); - // Domain Executions test $domain = $this->getFunctionDomain($functionId); @@ -2749,30 +2583,6 @@ final class FunctionsCustomServerTest extends Scope $this->assertEquals(200, $response['headers']['status-code']); - $executions = $this->listExecutions($functionId, [ - 'queries' => [ - Query::limit(1)->toString(), - Query::orderDesc('$id')->toString(), - ] - ]); - - $this->assertEquals(200, $executions['headers']['status-code']); - $this->assertCount(1, $executions['body']['executions']); - $this->assertEmpty($executions['body']['executions'][0]['logs']); - $this->assertEmpty($executions['body']['executions'][0]['errors']); - - $this->assertEventually(function () use ($functionId) { - $executions = $this->listExecutions($functionId); - - $this->assertEquals(200, $executions['headers']['status-code']); - $this->assertCount(3, $executions['body']['executions']); - - foreach ($executions['body']['executions'] as $execution) { - $this->assertEmpty($execution['logs']); - $this->assertEmpty($execution['errors']); - } - }, 10000, 500); - $this->cleanupFunction($functionId); } diff --git a/tests/e2e/Services/FunctionsSchedule/FunctionsScheduleTest.php b/tests/e2e/Services/FunctionsSchedule/FunctionsScheduleTest.php index 0e45a46f64..5b49318752 100644 --- a/tests/e2e/Services/FunctionsSchedule/FunctionsScheduleTest.php +++ b/tests/e2e/Services/FunctionsSchedule/FunctionsScheduleTest.php @@ -42,30 +42,6 @@ final class FunctionsScheduleTest extends Scope 'activate' => true ]); - // Wait for scheduled execution (schedule runs every minute) - // Give extra time in CI where deployment/scheduling may be slower - $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->assertGreaterThanOrEqual(1, count($executions['body']['executions']), 'Expected at least 1 scheduled execution'); - - $asyncExecution = $executions['body']['executions'][0]; - - $this->assertEquals('schedule', $asyncExecution['trigger']); - $this->assertEquals('completed', $asyncExecution['status']); - $this->assertEquals(200, $asyncExecution['responseStatusCode']); - $this->assertEquals('', $asyncExecution['responseBody']); - $this->assertGreaterThan(0, $asyncExecution['duration']); - $this->assertNotEmpty($asyncExecution['$id']); - $headers = array_column($asyncExecution['requestHeaders'] ?? [], 'value', 'name'); - $this->assertEmpty($headers['x-appwrite-client-ip'] ?? ''); - }, 180000, 500); // 3 minute timeout with 500ms polling for CI stability - $this->cleanupFunction($functionId); } @@ -124,17 +100,6 @@ final class FunctionsScheduleTest extends Scope $this->assertEquals('x-appwrite-client-ip', $execution['body']['requestHeaders'][0]['name']); $this->assertNotEmpty($execution['body']['requestHeaders'][0]['value']); - $this->assertEventually(function () use ($functionId, $executionId) { - $execution = $this->getExecution($functionId, $executionId); - - $this->assertEquals(200, $execution['headers']['status-code']); - $this->assertEquals(200, $execution['body']['responseStatusCode']); - $this->assertEquals('completed', $execution['body']['status']); - $this->assertEquals('/custom-path', $execution['body']['requestPath']); - $this->assertEquals('PATCH', $execution['body']['requestMethod']); - $this->assertGreaterThan(0, $execution['body']['duration']); - }, 120000, 500); - /* Test for FAILURE */ // Schedule synchronous execution $execution = $this->createExecution($functionId, [ diff --git a/tests/e2e/Services/GraphQL/FunctionsClientTest.php b/tests/e2e/Services/GraphQL/FunctionsClientTest.php index d649412685..ff365a7aeb 100644 --- a/tests/e2e/Services/GraphQL/FunctionsClientTest.php +++ b/tests/e2e/Services/GraphQL/FunctionsClientTest.php @@ -263,11 +263,10 @@ final class FunctionsClientTest extends Scope 'x-appwrite-project' => $projectId, ], $this->getHeaders()), $gqlPayload); - $this->assertIsArray($execution['body']['data']); - $this->assertArrayNotHasKey('errors', $execution['body']); - $execution = $execution['body']['data']['functionsGetExecution']; - $this->assertIsArray($execution); + // Executions are not persisted, so gets return not found + $this->assertArrayHasKey('errors', $execution['body']); + $this->assertStringContainsString('could not be found', (string) $execution['body']['errors'][0]['message']); - return $execution; + return []; } } diff --git a/tests/e2e/Services/GraphQL/FunctionsServerTest.php b/tests/e2e/Services/GraphQL/FunctionsServerTest.php index cc0f2e0cb3..010ee78bcd 100644 --- a/tests/e2e/Services/GraphQL/FunctionsServerTest.php +++ b/tests/e2e/Services/GraphQL/FunctionsServerTest.php @@ -425,12 +425,11 @@ final class FunctionsServerTest extends Scope 'x-appwrite-project' => $projectId, ], $this->getHeaders()), $gqlPayload); - $this->assertIsArray($execution['body']['data']); - $this->assertArrayNotHasKey('errors', $execution['body']); - $execution = $execution['body']['data']['functionsGetExecution']; - $this->assertIsArray($execution); + // Executions are not persisted, so gets return not found + $this->assertArrayHasKey('errors', $execution['body']); + $this->assertStringContainsString('could not be found', (string) $execution['body']['errors'][0]['message']); - return $execution; + return []; } /** From 0f23ffa1ec276ae2041c9b0533a071896b5c4de5 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Fri, 10 Jul 2026 12:32:25 +0530 Subject: [PATCH 06/15] (fix): validate execution queries without collection schema --- .../Database/Validator/Queries/Base.php | 26 ++++++++++++++++--- .../Database/Validator/Queries/Executions.php | 14 +++++++++- .../Database/Validator/Queries/Logs.php | 13 +++++++++- 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/src/Appwrite/Utopia/Database/Validator/Queries/Base.php b/src/Appwrite/Utopia/Database/Validator/Queries/Base.php index 9d9bbde00b..5e180b1d8b 100644 --- a/src/Appwrite/Utopia/Database/Validator/Queries/Base.php +++ b/src/Appwrite/Utopia/Database/Validator/Queries/Base.php @@ -22,7 +22,14 @@ class Base extends Queries * @param string[] $allowedAttributes * @throws \Exception */ - public function __construct(string $collection, array $allowedAttributes) + /** + * @param string $collection + * @param string[] $allowedAttributes + * @param array $fallbackTypes attribute => Database::VAR_* map used when + * the collection is not part of this platform's schema (e.g. executions on Server CE) + * @throws \Exception + */ + public function __construct(string $collection, array $allowedAttributes, array $fallbackTypes = []) { $config = Config::getParam('collections', []); @@ -34,7 +41,7 @@ class Base extends Queries $config['logs'] ); - $collection = $collections[$collection]; + $collection = $collections[$collection] ?? null; $allowedAttributesLookup = []; foreach ($allowedAttributes as $attribute) { @@ -43,7 +50,7 @@ class Base extends Queries $allAttributes = []; $attributes = []; - foreach ($collection['attributes'] as $attribute) { + foreach ($collection['attributes'] ?? [] as $attribute) { $key = $attribute['$id']; $attributeDocument = new Document([ @@ -59,6 +66,19 @@ class Base extends Queries } } + if ($collection === null) { + foreach ($allowedAttributes as $key) { + $attributeDocument = new Document([ + 'key' => $key, + 'type' => $fallbackTypes[$key] ?? Database::VAR_STRING, + 'array' => false, + ]); + + $allAttributes[] = $attributeDocument; + $attributes[] = $attributeDocument; + } + } + $internalAttributes = [ new Document([ 'key' => '$id', diff --git a/src/Appwrite/Utopia/Database/Validator/Queries/Executions.php b/src/Appwrite/Utopia/Database/Validator/Queries/Executions.php index 4c0a38b46f..ef5d36aede 100644 --- a/src/Appwrite/Utopia/Database/Validator/Queries/Executions.php +++ b/src/Appwrite/Utopia/Database/Validator/Queries/Executions.php @@ -2,6 +2,8 @@ namespace Appwrite\Utopia\Database\Validator\Queries; +use Utopia\Database\Database; + class Executions extends Base { public const ALLOWED_ATTRIBUTES = [ @@ -14,12 +16,22 @@ class Executions extends Base 'deploymentId' ]; + public const ATTRIBUTE_TYPES = [ + 'trigger' => Database::VAR_STRING, + 'status' => Database::VAR_STRING, + 'responseStatusCode' => Database::VAR_INTEGER, + 'duration' => Database::VAR_FLOAT, + 'requestMethod' => Database::VAR_STRING, + 'requestPath' => Database::VAR_STRING, + 'deploymentId' => Database::VAR_STRING, + ]; + /** * Expression constructor * */ public function __construct() { - parent::__construct('executions', self::ALLOWED_ATTRIBUTES); + parent::__construct('executions', self::ALLOWED_ATTRIBUTES, self::ATTRIBUTE_TYPES); } } diff --git a/src/Appwrite/Utopia/Database/Validator/Queries/Logs.php b/src/Appwrite/Utopia/Database/Validator/Queries/Logs.php index d07d3dd3d2..cf4c3ad7ad 100644 --- a/src/Appwrite/Utopia/Database/Validator/Queries/Logs.php +++ b/src/Appwrite/Utopia/Database/Validator/Queries/Logs.php @@ -2,6 +2,8 @@ namespace Appwrite\Utopia\Database\Validator\Queries; +use Utopia\Database\Database; + class Logs extends Base { public const ALLOWED_ATTRIBUTES = [ @@ -13,12 +15,21 @@ class Logs extends Base 'deploymentId' ]; + public const ATTRIBUTE_TYPES = [ + 'status' => Database::VAR_STRING, + 'responseStatusCode' => Database::VAR_INTEGER, + 'duration' => Database::VAR_FLOAT, + 'requestMethod' => Database::VAR_STRING, + 'requestPath' => Database::VAR_STRING, + 'deploymentId' => Database::VAR_STRING, + ]; + /** * Expression constructor * */ public function __construct() { - parent::__construct('executions', self::ALLOWED_ATTRIBUTES); //TODO: Update this later + parent::__construct('executions', self::ALLOWED_ATTRIBUTES, self::ATTRIBUTE_TYPES); //TODO: Update this later } } From aacc34550aef089186a6a23d746958059abf8ae2 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Fri, 10 Jul 2026 13:16:22 +0530 Subject: [PATCH 07/15] (fix): address review feedback on scheduled execution cancel and upgrades --- .../Functions/Http/Executions/Delete.php | 12 +++++++++++- src/Appwrite/Platform/Tasks/ScheduleBase.php | 19 ++++++++++++++++--- .../Platform/Tasks/ScheduleExecutions.php | 19 +++++++++++++++++-- 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Executions/Delete.php b/src/Appwrite/Platform/Modules/Functions/Http/Executions/Delete.php index 821d14684c..b0ef2f9a7c 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Executions/Delete.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Executions/Delete.php @@ -110,9 +110,19 @@ class Delete extends Base 'active' => false, ]))); + $execution = new Document([ + '$id' => $executionId, + 'functionId' => $function->getId(), + 'resourceId' => $function->getId(), + 'resourceType' => 'functions', + 'trigger' => 'schedule', + 'status' => 'scheduled', + ]); + $queueForEvents ->setParam('functionId', $function->getId()) - ->setParam('executionId', $executionId); + ->setParam('executionId', $executionId) + ->setPayload($response->output($execution, Response::MODEL_EXECUTION)); $response->noContent(); return; diff --git a/src/Appwrite/Platform/Tasks/ScheduleBase.php b/src/Appwrite/Platform/Tasks/ScheduleBase.php index 554dbf558b..0804b513a3 100644 --- a/src/Appwrite/Platform/Tasks/ScheduleBase.php +++ b/src/Appwrite/Platform/Tasks/ScheduleBase.php @@ -274,9 +274,22 @@ abstract class ScheduleBase extends Action $this->schedules[$sequence]['project'] = $project; if (!static::loadResource()) { - $this->schedules[$sequence]['resource'] = new Document([ - '$id' => $schedule['resourceId'], - ]); + // Best-effort load: schedules from before the executions + // collection was dropped can still resolve their document; + // newer ones carry what they need in the schedule data. + try { + $resource = $getProjectDB($project)->getDocument(static::getCollectionId(), $schedule['resourceId']); + } catch (\Throwable) { + $resource = new Document(); + } + + if ($resource->isEmpty()) { + $resource = new Document([ + '$id' => $schedule['resourceId'], + ]); + } + + $this->schedules[$sequence]['resource'] = $resource; continue; } diff --git a/src/Appwrite/Platform/Tasks/ScheduleExecutions.php b/src/Appwrite/Platform/Tasks/ScheduleExecutions.php index 51d6662250..feb49cf4a6 100644 --- a/src/Appwrite/Platform/Tasks/ScheduleExecutions.php +++ b/src/Appwrite/Platform/Tasks/ScheduleExecutions.php @@ -5,6 +5,7 @@ namespace Appwrite\Platform\Tasks; use Appwrite\Event\Message\Func as FunctionMessage; use Appwrite\Event\Publisher\Func as FunctionPublisher; use Swoole\Coroutine as Co; +use Utopia\Console; use Utopia\Database\Database; /** @@ -68,11 +69,25 @@ class ScheduleExecutions extends ScheduleBase $schedule['$id'], )->getAttribute('data', []); + $functionId = $data['functionId'] ?? $schedule['resource']->getAttribute('resourceId', ''); + + if (empty($functionId)) { + Console::error("Missing functionId for scheduled execution {$schedule['resourceId']}, skipping"); + + $dbForPlatform->deleteDocument( + 'schedules', + $schedule['$id'], + ); + + unset($this->schedules[$schedule['$sequence']]); + continue; + } + $delay = $scheduledAt->getTimestamp() - (new \DateTime())->getTimestamp(); $this->updateProjectAccess($schedule['project'], $dbForPlatform); - \go(function () use ($publisherForFunctions, $schedule, $scheduledAt, $delay, $data, $dbForPlatform) { + \go(function () use ($publisherForFunctions, $schedule, $scheduledAt, $delay, $data, $functionId, $dbForPlatform) { if ($delay > 0) { Co::sleep($delay); } @@ -80,7 +95,7 @@ class ScheduleExecutions extends ScheduleBase $publisherForFunctions->enqueue(new FunctionMessage( project: $schedule['project'], userId: $data['userId'] ?? '', - functionId: $data['functionId'] ?? '', + functionId: $functionId, execution: $schedule['resource'], type: 'schedule', body: $data['body'] ?? '', From 90d49d753bd168a105cc6e5aa39434f32123f0fb Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Fri, 10 Jul 2026 13:26:39 +0530 Subject: [PATCH 08/15] (fix): include full execution payload in scheduled cancel event --- .../Modules/Functions/Http/Executions/Delete.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Executions/Delete.php b/src/Appwrite/Platform/Modules/Functions/Http/Executions/Delete.php index b0ef2f9a7c..8145a41b0f 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Executions/Delete.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Executions/Delete.php @@ -112,11 +112,24 @@ class Delete extends Base $execution = new Document([ '$id' => $executionId, + '$createdAt' => DateTime::now(), + '$updatedAt' => DateTime::now(), + '$permissions' => [], 'functionId' => $function->getId(), 'resourceId' => $function->getId(), 'resourceType' => 'functions', + 'deploymentId' => '', 'trigger' => 'schedule', 'status' => 'scheduled', + 'requestMethod' => '', + 'requestPath' => '', + 'requestHeaders' => [], + 'responseStatusCode' => 0, + 'responseBody' => '', + 'responseHeaders' => [], + 'logs' => '', + 'errors' => '', + 'duration' => 0.0, ]); $queueForEvents From 70ea0acd897740a79962b948d7debcc0c5b2b139 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Fri, 10 Jul 2026 13:29:31 +0530 Subject: [PATCH 09/15] (refactor): drop no-op router change --- app/controllers/general.php | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/app/controllers/general.php b/app/controllers/general.php index bbc75930fa..914acf6d2d 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -125,7 +125,7 @@ function router(Http $utopia, Database $dbForPlatform, callable $getProjectDB, S if (\str_ends_with($host, $denyDomain)) { $exception = new AppwriteException(AppwriteException::RULE_NOT_FOUND, 'This domain is not connected to any Appwrite resources. Visit domains tab under function/site settings to configure it.', view: $errorView); - $exception->addCTA('Start with this domain', $url . '/console'); + $exception->addCTA('Start with this domain', $url); throw $exception; } } @@ -200,7 +200,7 @@ function router(Http $utopia, Database $dbForPlatform, callable $getProjectDB, S $resourceId = $rule->getAttribute('deploymentResourceId', ''); $type = ($resourceType === 'site') ? 'sites' : 'functions'; $exception = new AppwriteException(AppwriteException::DEPLOYMENT_NOT_FOUND, view: $errorView); - $exception->addCTA('View deployments', $url . '/console/project-' . $project->getAttribute('region', 'default') . '-' . $projectId . '/' . $type . '/' . $resourceType . '-' . $resourceId); + $exception->addCTA('View deployments', $url . '/projects/' . $projectId . '/' . $type . '/' . $resourceId); throw $exception; } @@ -289,7 +289,7 @@ function router(Http $utopia, Database $dbForPlatform, callable $getProjectDB, S $response ->addHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0') ->addHeader('Pragma', 'no-cache') - ->redirect($url . '/console/auth/preview?' + ->redirect($url . '/auth/preview?' . \http_build_query([ 'projectId' => $projectId, 'origin' => $protocol . '://' . $host, @@ -346,22 +346,21 @@ function router(Http $utopia, Database $dbForPlatform, callable $getProjectDB, S $allowAnyStatus = !\is_null($apiKey) && $apiKey->isDeploymentStatusIgnored(); if (!$allowAnyStatus && $deployment->getAttribute('status') !== 'ready') { $status = $deployment->getAttribute('status'); - $region = $project->getAttribute('region', 'default'); switch ($status) { case 'failed': $exception = new AppwriteException(AppwriteException::BUILD_FAILED, view: $errorView); - $ctaUrl = '/console/project-' . $region . '-' . $project->getId() . '/sites/site-' . $resource->getId() . '/deployments/deployment-' . $deployment->getId(); + $ctaUrl = '/projects/' . $project->getId() . '/sites/' . $resource->getId() . '/deployments/' . $deployment->getId(); $exception->addCTA('View logs', $url . $ctaUrl); break; case 'canceled': $exception = new AppwriteException(AppwriteException::BUILD_CANCELED, view: $errorView); - $ctaUrl = '/console/project-' . $region . '-' . $project->getId() . '/sites/site-' . $resource->getId() . '/deployments'; + $ctaUrl = '/projects/' . $project->getId() . '/sites/' . $resource->getId() . '/deployments'; $exception->addCTA('View deployments', $url . $ctaUrl); break; default: $exception = new AppwriteException(AppwriteException::BUILD_NOT_READY, view: $errorView); - $ctaUrl = '/console/project-' . $region . '-' . $project->getId() . '/sites/site-' . $resource->getId() . '/deployments/deployment-' . $deployment->getId(); + $ctaUrl = '/projects/' . $project->getId() . '/sites/' . $resource->getId() . '/deployments/' . $deployment->getId(); $exception->addCTA('Reload', '/'); $exception->addCTA('View logs', $url . $ctaUrl); break; @@ -373,7 +372,7 @@ function router(Http $utopia, Database $dbForPlatform, callable $getProjectDB, S $permissions = $resource->getAttribute('execute'); if (!(\in_array('any', $permissions)) && !(\in_array('guests', $permissions))) { $exception = new AppwriteException(AppwriteException::FUNCTION_EXECUTE_PERMISSION_MISSING, view: $errorView); - $exception->addCTA('View settings', $url . '/console/project-' . $project->getAttribute('region', 'default') . '-' . $project->getId() . '/functions/function-' . $resource->getId() . '/settings'); + $exception->addCTA('View settings', $url . '/projects/' . $project->getId() . '/functions/' . $resource->getId() . '/settings'); throw $exception; } } @@ -722,11 +721,11 @@ function router(Http $utopia, Database $dbForPlatform, callable $getProjectDB, S spec: $spec, resource: $resource->getArrayCopy(), )); - - $execution->setAttribute('logs', ''); - $execution->setAttribute('errors', ''); } + $execution->setAttribute('logs', ''); + $execution->setAttribute('errors', ''); + $headers = []; foreach (($executionResponse['headers'] ?? []) as $key => $value) { $headers[] = ['name' => $key, 'value' => \is_array($value) ? \implode(', ', $value) : $value]; From e03c53bddd2251b7300ab07da081d96f2460251e Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Fri, 10 Jul 2026 13:44:31 +0530 Subject: [PATCH 10/15] (test): tag CE-only execution persistence tests --- .../Services/Functions/FunctionsCustomServerTest.php | 12 ++++++++---- tests/e2e/Services/GraphQL/FunctionsClientTest.php | 2 ++ tests/e2e/Services/GraphQL/FunctionsServerTest.php | 2 ++ 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php index 94b322a117..832a74be0e 100644 --- a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php +++ b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php @@ -7,6 +7,7 @@ namespace Tests\E2E\Services\Functions; use Appwrite\Platform\Modules\Compute\Specification; use Appwrite\Tests\Retry; use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\Group; use Tests\E2E\Client; use Tests\E2E\Scopes\ProjectCustom; use Tests\E2E\Scopes\Scope; @@ -1606,12 +1607,12 @@ final class FunctionsCustomServerTest extends Scope $this->assertIsArray($execution['body']['responseHeaders']); $this->assertEmpty($execution['body']['responseBody']); // For HEAD requests, response body is empty - /** Executions are not persisted, so deletes return 404 */ + /** 404 on Server CE (executions are not persisted), 204 on platforms that persist them */ $execution = $this->client->call(Client::METHOD_DELETE, '/functions/' . $data['functionId'] . '/executions/' . $execution['body']['$id'], array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), []); - $this->assertEquals(404, $execution['headers']['status-code']); + $this->assertContains($execution['headers']['status-code'], [204, 404]); /** Test create execution with 400 status code */ $execution = $this->createExecution($data['functionId'], [ @@ -1623,14 +1624,15 @@ final class FunctionsCustomServerTest extends Scope $this->assertEquals('completed', $execution['body']['status']); $this->assertEquals(400, $execution['body']['responseStatusCode']); - /** Executions are not persisted, so deletes return 404 */ + /** 404 on Server CE (executions are not persisted), 204 on platforms that persist them */ $execution = $this->client->call(Client::METHOD_DELETE, '/functions/' . $data['functionId'] . '/executions/' . $execution['body']['$id'], array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), []); - $this->assertEquals(404, $execution['headers']['status-code']); + $this->assertContains($execution['headers']['status-code'], [204, 404]); } + #[Group('ceOnly')] public function testListExecutions(): void { $data = $this->setupTestExecution(); @@ -1693,6 +1695,7 @@ final class FunctionsCustomServerTest extends Scope $this->assertLessThan(1.500, $execution['body']['duration']); } + #[Group('ceOnly')] public function testGetExecution(): void { $data = $this->setupTestExecution(); @@ -1710,6 +1713,7 @@ final class FunctionsCustomServerTest extends Scope } + #[Group('ceOnly')] public function testDeleteExecution(): void { // Create fresh execution for this test since we delete it diff --git a/tests/e2e/Services/GraphQL/FunctionsClientTest.php b/tests/e2e/Services/GraphQL/FunctionsClientTest.php index ff365a7aeb..057e448aae 100644 --- a/tests/e2e/Services/GraphQL/FunctionsClientTest.php +++ b/tests/e2e/Services/GraphQL/FunctionsClientTest.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Tests\E2E\Services\GraphQL; use Appwrite\Tests\Async; +use PHPUnit\Framework\Attributes\Group; use Tests\E2E\Client; use Tests\E2E\Scopes\ProjectCustom; use Tests\E2E\Scopes\Scope; @@ -243,6 +244,7 @@ final class FunctionsClientTest extends Scope * @return array * @throws \Exception */ + #[Group('ceOnly')] public function testGetExecution(): array { $function = $this->setupFunction(); diff --git a/tests/e2e/Services/GraphQL/FunctionsServerTest.php b/tests/e2e/Services/GraphQL/FunctionsServerTest.php index 010ee78bcd..96ad222005 100644 --- a/tests/e2e/Services/GraphQL/FunctionsServerTest.php +++ b/tests/e2e/Services/GraphQL/FunctionsServerTest.php @@ -6,6 +6,7 @@ namespace Tests\E2E\Services\GraphQL; use Appwrite\Tests\Async; use Appwrite\Tests\Async\Exceptions\Critical; +use PHPUnit\Framework\Attributes\Group; use Tests\E2E\Client; use Tests\E2E\Scopes\ProjectCustom; use Tests\E2E\Scopes\Scope; @@ -406,6 +407,7 @@ final class FunctionsServerTest extends Scope * @return array * @throws \Exception */ + #[Group('ceOnly')] public function testGetExecution(): array { $execution = $this->setupExecution(); From 785d53714e10e2b1dba3adab7167c985fcf3e20d Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Fri, 10 Jul 2026 14:32:29 +0530 Subject: [PATCH 11/15] (feat): move execution and site log read endpoints to cloud --- .../Functions/Http/Executions/Delete.php | 192 ------------------ .../Modules/Functions/Http/Executions/Get.php | 106 ---------- .../Functions/Http/Executions/XList.php | 182 ----------------- .../Modules/Functions/Services/Http.php | 6 - .../Modules/Sites/Http/Logs/Delete.php | 96 --------- .../Platform/Modules/Sites/Http/Logs/Get.php | 92 --------- .../Modules/Sites/Http/Logs/XList.php | 168 --------------- .../Platform/Modules/Sites/Services/Http.php | 8 - .../Database/Validator/Queries/Base.php | 26 +-- .../Database/Validator/Queries/Executions.php | 14 +- .../Database/Validator/Queries/Logs.php | 13 +- .../Functions/FunctionsCustomClientTest.php | 44 ---- .../Functions/FunctionsCustomServerTest.php | 132 ------------ .../FunctionsScheduleTest.php | 38 ---- .../Services/GraphQL/FunctionsClientTest.php | 62 ------ .../Services/GraphQL/FunctionsServerTest.php | 62 ------ 16 files changed, 5 insertions(+), 1236 deletions(-) delete mode 100644 src/Appwrite/Platform/Modules/Functions/Http/Executions/Delete.php delete mode 100644 src/Appwrite/Platform/Modules/Functions/Http/Executions/Get.php delete mode 100644 src/Appwrite/Platform/Modules/Functions/Http/Executions/XList.php delete mode 100644 src/Appwrite/Platform/Modules/Sites/Http/Logs/Delete.php delete mode 100644 src/Appwrite/Platform/Modules/Sites/Http/Logs/Get.php delete mode 100644 src/Appwrite/Platform/Modules/Sites/Http/Logs/XList.php diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Executions/Delete.php b/src/Appwrite/Platform/Modules/Functions/Http/Executions/Delete.php deleted file mode 100644 index 8145a41b0f..0000000000 --- a/src/Appwrite/Platform/Modules/Functions/Http/Executions/Delete.php +++ /dev/null @@ -1,192 +0,0 @@ -setHttpMethod(Action::HTTP_REQUEST_METHOD_DELETE) - ->setHttpPath('/v1/functions/:functionId/executions/:executionId') - ->desc('Delete execution') - ->groups(['api', 'functions']) - ->label('scope', ['executions.write', 'execution.write']) - ->label('resourceType', RESOURCE_TYPE_FUNCTIONS) - ->label('event', 'functions.[functionId].executions.[executionId].delete') - ->label('audits.event', 'executions.delete') - ->label('audits.resource', 'function/{request.functionId}') - ->label('usage.resource', 'function/{request.functionId}') - ->label('sdk', new Method( - namespace: 'functions', - group: 'executions', - name: 'deleteExecution', - description: <<param('functionId', '', fn (Database $dbForProject) => new UID($dbForProject->getAdapter()->getMaxUIDLength()), 'Function ID.', false, ['dbForProject']) - ->param('executionId', '', fn (Database $dbForProject) => new UID($dbForProject->getAdapter()->getMaxUIDLength()), 'Execution ID.', false, ['dbForProject']) - ->inject('response') - ->inject('project') - ->inject('dbForProject') - ->inject('dbForPlatform') - ->inject('queueForEvents') - ->inject('authorization') - ->callback($this->action(...)); - } - - public function action( - string $functionId, - string $executionId, - Response $response, - Document $project, - Database $dbForProject, - Database $dbForPlatform, - Event $queueForEvents, - Authorization $authorization - ) { - $function = $dbForProject->getDocument('functions', $functionId); - - if ($function->isEmpty()) { - throw new Exception(Exception::FUNCTION_NOT_FOUND); - } - - try { - $execution = $dbForProject->getDocument('executions', $executionId); - } catch (NotFoundException) { - $execution = new Document(); - } - - if ($execution->isEmpty()) { - // Executions are not persisted, but a pending scheduled execution - // can still be cancelled through its schedule. - $schedule = $authorization->skip(fn () => $dbForPlatform->findOne('schedules', [ - Query::equal('resourceId', [$executionId]), - Query::equal('resourceType', [SCHEDULE_RESOURCE_TYPE_EXECUTION]), - Query::equal('projectInternalId', [$project->getSequence()]), - Query::equal('active', [true]), - ])); - - if ($schedule->isEmpty()) { - throw new Exception(Exception::EXECUTION_NOT_FOUND); - } - - $authorization->skip(fn () => $dbForPlatform->updateDocument('schedules', $schedule->getId(), new Document([ - 'resourceUpdatedAt' => DateTime::now(), - 'active' => false, - ]))); - - $execution = new Document([ - '$id' => $executionId, - '$createdAt' => DateTime::now(), - '$updatedAt' => DateTime::now(), - '$permissions' => [], - 'functionId' => $function->getId(), - 'resourceId' => $function->getId(), - 'resourceType' => 'functions', - 'deploymentId' => '', - 'trigger' => 'schedule', - 'status' => 'scheduled', - 'requestMethod' => '', - 'requestPath' => '', - 'requestHeaders' => [], - 'responseStatusCode' => 0, - 'responseBody' => '', - 'responseHeaders' => [], - 'logs' => '', - 'errors' => '', - 'duration' => 0.0, - ]); - - $queueForEvents - ->setParam('functionId', $function->getId()) - ->setParam('executionId', $executionId) - ->setPayload($response->output($execution, Response::MODEL_EXECUTION)); - - $response->noContent(); - return; - } - - if ($execution->getAttribute('resourceType') !== 'functions' && $execution->getAttribute('resourceInternalId') !== $function->getSequence()) { - throw new Exception(Exception::EXECUTION_NOT_FOUND); - } - $status = $execution->getAttribute('status'); - - // Treat timed-out executions as failed so they can be deleted. - if ($status === 'waiting' || $status === 'processing') { - $timeout = $function->getAttribute('timeout', 900); - $elapsed = \time() - \strtotime($execution->getCreatedAt()); - if ($elapsed >= $timeout) { - $status = 'failed'; - } - } - - if (!in_array($status, ['completed', 'failed', 'scheduled'])) { - throw new Exception(Exception::EXECUTION_IN_PROGRESS); - } - - if (!$dbForProject->deleteDocument('executions', $execution->getId())) { - throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Failed to remove execution from DB'); - } - - if ($status === 'scheduled') { - $schedule = $dbForPlatform->findOne('schedules', [ - Query::equal('resourceId', [$execution->getId()]), - Query::equal('resourceType', [SCHEDULE_RESOURCE_TYPE_EXECUTION]), - Query::equal('active', [true]), - ]); - - if (!$schedule->isEmpty()) { - $schedule - ->setAttribute('resourceUpdatedAt', DateTime::now()) - ->setAttribute('active', false); - - $authorization->skip(fn () => $dbForPlatform->updateDocument('schedules', $schedule->getId(), new Document([ - 'resourceUpdatedAt' => $schedule->getAttribute('resourceUpdatedAt'), - 'active' => $schedule->getAttribute('active'), - ]))); - } - } - - $queueForEvents - ->setParam('functionId', $function->getId()) - ->setParam('executionId', $execution->getId()) - ->setPayload($response->output($execution, Response::MODEL_EXECUTION)); - - $response->noContent(); - } -} diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Executions/Get.php b/src/Appwrite/Platform/Modules/Functions/Http/Executions/Get.php deleted file mode 100644 index 4c351712d8..0000000000 --- a/src/Appwrite/Platform/Modules/Functions/Http/Executions/Get.php +++ /dev/null @@ -1,106 +0,0 @@ -setHttpMethod(Action::HTTP_REQUEST_METHOD_GET) - ->setHttpPath('/v1/functions/:functionId/executions/:executionId') - ->desc('Get execution') - ->groups(['api', 'functions']) - ->label('scope', ['executions.read', 'execution.read']) - ->label('usage.resource', 'function/{request.functionId}') - ->label('resourceType', RESOURCE_TYPE_FUNCTIONS) - ->label('sdk', new Method( - namespace: 'functions', - group: 'executions', - name: 'getExecution', - description: <<param('functionId', '', fn (Database $dbForProject) => new UID($dbForProject->getAdapter()->getMaxUIDLength()), 'Function ID.', false, ['dbForProject']) - ->param('executionId', '', fn (Database $dbForProject) => new UID($dbForProject->getAdapter()->getMaxUIDLength()), 'Execution ID.', false, ['dbForProject']) - ->inject('response') - ->inject('dbForProject') - ->inject('authorization') - ->inject('user') - ->callback($this->action(...)); - } - - public function action( - string $functionId, - string $executionId, - Response $response, - Database $dbForProject, - Authorization $authorization, - User $user - ) { - $function = $authorization->skip(fn () => $dbForProject->getDocument('functions', $functionId)); - - $isAPIKey = $user->isKey($authorization->getRoles()); - $isPrivilegedUser = $user->isPrivileged($authorization->getRoles()); - - if ($function->isEmpty() || (!$function->getAttribute('enabled') && !$isAPIKey && !$isPrivilegedUser)) { - throw new Exception(Exception::FUNCTION_NOT_FOUND); - } - - try { - $execution = $dbForProject->getDocument('executions', $executionId); - } catch (NotFoundException) { - $execution = new Document(); - } - - if ($execution->getAttribute('resourceType') !== 'functions' || $execution->getAttribute('resourceInternalId') !== $function->getSequence()) { - throw new Exception(Exception::EXECUTION_NOT_FOUND); - } - - if ($execution->isEmpty()) { - throw new Exception(Exception::EXECUTION_NOT_FOUND); - } - - // Override status in response if the execution is stuck in waiting/processing beyond the function timeout. - $status = $execution->getAttribute('status', ''); - if ($status === 'waiting' || $status === 'processing') { - $timeout = $function->getAttribute('timeout', 900); - $elapsed = \time() - \strtotime($execution->getCreatedAt()); - if ($elapsed >= $timeout) { - $execution->setAttribute('status', 'failed'); - } - } - - $response->dynamic($execution, Response::MODEL_EXECUTION); - } -} diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Executions/XList.php b/src/Appwrite/Platform/Modules/Functions/Http/Executions/XList.php deleted file mode 100644 index 772634d512..0000000000 --- a/src/Appwrite/Platform/Modules/Functions/Http/Executions/XList.php +++ /dev/null @@ -1,182 +0,0 @@ -setHttpMethod(Action::HTTP_REQUEST_METHOD_GET) - ->setHttpPath('/v1/functions/:functionId/executions') - ->desc('List executions') - ->groups(['api', 'functions']) - ->label('scope', ['executions.read', 'execution.read']) - ->label('usage.resource', 'function/{request.functionId}') - ->label('resourceType', RESOURCE_TYPE_FUNCTIONS) - ->label('sdk', new Method( - namespace: 'functions', - group: 'executions', - name: 'listExecutions', - description: <<param('functionId', '', fn (Database $dbForProject) => new UID($dbForProject->getAdapter()->getMaxUIDLength()), 'Function ID.', false, ['dbForProject']) - ->param('queries', [], new Executions(), 'Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/queries). Maximum of ' . APP_LIMIT_ARRAY_PARAMS_SIZE . ' queries are allowed, each ' . APP_LIMIT_ARRAY_ELEMENT_SIZE . ' characters long. You may filter on the following attributes: ' . implode(', ', Executions::ALLOWED_ATTRIBUTES), true) - ->param('total', true, new Boolean(true), 'When set to false, the total count returned will be 0 and will not be calculated.', true) - ->inject('response') - ->inject('dbForProject') - ->inject('authorization') - ->inject('user') - ->callback($this->action(...)); - } - - public function action( - string $functionId, - array $queries, - bool $includeTotal, - Response $response, - Database $dbForProject, - Authorization $authorization, - User $user - ) { - $function = $authorization->skip(fn () => $dbForProject->getDocument('functions', $functionId)); - - $isAPIKey = $user->isKey($authorization->getRoles()); - $isPrivilegedUser = $user->isPrivileged($authorization->getRoles()); - - if ($function->isEmpty() || (!$function->getAttribute('enabled') && !$isAPIKey && !$isPrivilegedUser)) { - throw new Exception(Exception::FUNCTION_NOT_FOUND); - } - - try { - $queries = Query::parseQueries($queries); - } catch (QueryException $e) { - throw new Exception(Exception::GENERAL_QUERY_INVALID, $e->getMessage()); - } - - // Set internal queries - $queries[] = Query::equal('resourceInternalId', [$function->getSequence()]); - $queries[] = Query::equal('resourceType', ['functions']); - - $cursor = Query::getCursorQueries($queries, false); - $cursor = \reset($cursor); - - if ($cursor !== false) { - $validator = new Cursor(); - if (!$validator->isValid($cursor)) { - throw new Exception(Exception::GENERAL_QUERY_INVALID, $validator->getDescription()); - } - - $executionId = $cursor->getValue(); - try { - $cursorDocument = $dbForProject->getDocument('executions', $executionId); - } catch (NotFoundException) { - $cursorDocument = new Document(); - } - - if ($cursorDocument->isEmpty()) { - throw new Exception(Exception::GENERAL_CURSOR_NOT_FOUND, "Execution '{$executionId}' for the 'cursor' value not found."); - } - - $cursor->setValue($cursorDocument); - } - - // Calculate the cutoff datetime before which a waiting/processing execution is considered timed out. - $timeout = $function->getAttribute('timeout', 900); - $thresholdDate = new \DateTime("-{$timeout} seconds"); - $threshold = DateTime::format($thresholdDate); - - // Capture what statuses the caller explicitly requested, before we mutate the query. - $requestedStatuses = []; - foreach ($queries as $query) { - if ($query->getMethod() === Query::TYPE_EQUAL && $query->getAttribute() === 'status') { - $requestedStatuses = [...$requestedStatuses, ...$query->getValues()]; - } - } - - // If the caller is filtering by 'failed', expand the DB query to also return - // waiting/processing executions created before the timeout threshold, so timed-out - // executions that were never marked failed in the DB are included in the results. - foreach ($queries as $index => $query) { - if ($query->getMethod() === Query::TYPE_EQUAL && $query->getAttribute() === 'status' && \in_array('failed', $query->getValues())) { - $queries[$index] = Query::or([ - $query, - Query::and([ - Query::equal('status', ['waiting', 'processing']), - Query::createdBefore($threshold), - ]), - ]); - break; - } - } - - $filterQueries = Query::groupByType($queries)['filters']; - - try { - $results = $dbForProject->find('executions', $queries); - $total = $includeTotal ? $dbForProject->count('executions', $filterQueries, APP_LIMIT_COUNT) : 0; - } catch (NotFoundException) { - $results = []; - $total = 0; - } catch (OrderException $e) { - throw new Exception(Exception::DATABASE_QUERY_ORDER_NULL, "The order attribute '{$e->getAttribute()}' had a null value. Cursor pagination requires all documents order attribute values are non-null."); - } - - // Override status in response for timed-out executions, but only when the caller - // did not explicitly request a non-failed status (e.g. waiting/processing). - if (empty(\array_diff($requestedStatuses, ['failed']))) { - foreach ($results as $execution) { - $status = $execution->getAttribute('status', ''); - if ($status === 'waiting' || $status === 'processing') { - $elapsed = \time() - \strtotime($execution->getCreatedAt()); - if ($elapsed >= $timeout) { - $execution->setAttribute('status', 'failed'); - } - } - } - } - - $response->dynamic(new Document([ - 'executions' => $results, - 'total' => $total, - ]), Response::MODEL_EXECUTION_LIST); - } -} diff --git a/src/Appwrite/Platform/Modules/Functions/Services/Http.php b/src/Appwrite/Platform/Modules/Functions/Services/Http.php index 9848105e73..32b4ee5bc4 100644 --- a/src/Appwrite/Platform/Modules/Functions/Services/Http.php +++ b/src/Appwrite/Platform/Modules/Functions/Services/Http.php @@ -12,9 +12,6 @@ use Appwrite\Platform\Modules\Functions\Http\Deployments\Template\Create as Crea use Appwrite\Platform\Modules\Functions\Http\Deployments\Vcs\Create as CreateVcsDeployment; use Appwrite\Platform\Modules\Functions\Http\Deployments\XList as ListDeployments; use Appwrite\Platform\Modules\Functions\Http\Executions\Create as CreateExecution; -use Appwrite\Platform\Modules\Functions\Http\Executions\Delete as DeleteExecution; -use Appwrite\Platform\Modules\Functions\Http\Executions\Get as GetExecution; -use Appwrite\Platform\Modules\Functions\Http\Executions\XList as ListExecutions; use Appwrite\Platform\Modules\Functions\Http\Functions\Create as CreateFunction; use Appwrite\Platform\Modules\Functions\Http\Functions\Delete as DeleteFunction; use Appwrite\Platform\Modules\Functions\Http\Functions\Deployment\Update as UpdateFunctionDeployment; @@ -65,9 +62,6 @@ class Http extends Service // Executions $this->addAction(CreateExecution::getName(), new CreateExecution()); - $this->addAction(GetExecution::getName(), new GetExecution()); - $this->addAction(ListExecutions::getName(), new ListExecutions()); - $this->addAction(DeleteExecution::getName(), new DeleteExecution()); // Variables $this->addAction(CreateVariable::getName(), new CreateVariable()); diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Logs/Delete.php b/src/Appwrite/Platform/Modules/Sites/Http/Logs/Delete.php deleted file mode 100644 index bf0d43f977..0000000000 --- a/src/Appwrite/Platform/Modules/Sites/Http/Logs/Delete.php +++ /dev/null @@ -1,96 +0,0 @@ -setHttpMethod(Action::HTTP_REQUEST_METHOD_DELETE) - ->setHttpPath('/v1/sites/:siteId/logs/:logId') - ->desc('Delete log') - ->groups(['api', 'sites']) - ->label('scope', 'log.write') - ->label('resourceType', RESOURCE_TYPE_SITES) - ->label('event', 'sites.[siteId].logs.[logId].delete') - ->label('audits.event', 'logs.delete') - ->label('audits.resource', 'site/{request.siteId}') - ->label('usage.resource', 'site/{request.siteId}') - ->label('sdk', new Method( - namespace: 'sites', - group: 'logs', - name: 'deleteLog', - description: <<param('siteId', '', fn (Database $dbForProject) => new UID($dbForProject->getAdapter()->getMaxUIDLength()), 'Site ID.', false, ['dbForProject']) - ->param('logId', '', fn (Database $dbForProject) => new UID($dbForProject->getAdapter()->getMaxUIDLength()), 'Log ID.', false, ['dbForProject']) - ->inject('response') - ->inject('dbForProject') - ->inject('queueForEvents') - ->callback($this->action(...)); - } - - public function action(string $siteId, string $logId, Response $response, Database $dbForProject, Event $queueForEvents) - { - $site = $dbForProject->getDocument('sites', $siteId); - - if ($site->isEmpty()) { - throw new Exception(Exception::SITE_NOT_FOUND); - } - - try { - $log = $dbForProject->getDocument('executions', $logId); - } catch (NotFoundException) { - $log = new Document(); - } - if ($log->isEmpty()) { - throw new Exception(Exception::LOG_NOT_FOUND); - } - - if ($log->getAttribute('resourceType') !== 'sites' && $log->getAttribute('resourceInternalId') !== $site->getSequence()) { - throw new Exception(Exception::LOG_NOT_FOUND); - } - - if (!$dbForProject->deleteDocument('executions', $log->getId())) { - throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Failed to remove log from DB'); - } - - $queueForEvents - ->setParam('siteId', $site->getId()) - ->setParam('logId', $log->getId()) - ->setPayload($response->output($log, Response::MODEL_EXECUTION)); // TODO: Update model - - $response->noContent(); - } -} diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Logs/Get.php b/src/Appwrite/Platform/Modules/Sites/Http/Logs/Get.php deleted file mode 100644 index 0e00e23c38..0000000000 --- a/src/Appwrite/Platform/Modules/Sites/Http/Logs/Get.php +++ /dev/null @@ -1,92 +0,0 @@ -setHttpMethod(Action::HTTP_REQUEST_METHOD_GET) - ->setHttpPath('/v1/sites/:siteId/logs/:logId') - ->desc('Get log') - ->groups(['api', 'sites']) - ->label('scope', 'log.read') - ->label('resourceType', RESOURCE_TYPE_SITES) - ->label('sdk', new Method( - namespace: 'sites', - group: 'logs', - name: 'getLog', - description: <<param('siteId', '', fn (Database $dbForProject) => new UID($dbForProject->getAdapter()->getMaxUIDLength()), 'Site ID.', false, ['dbForProject']) - ->param('logId', '', fn (Database $dbForProject) => new UID($dbForProject->getAdapter()->getMaxUIDLength()), 'Log ID.', false, ['dbForProject']) - ->inject('response') - ->inject('dbForProject') - ->callback($this->action(...)); - } - - public function action(string $siteId, string $logId, Response $response, Database $dbForProject) - { - $site = $dbForProject->getDocument('sites', $siteId); - - if ($site->isEmpty() || !$site->getAttribute('enabled')) { - throw new Exception(Exception::SITE_NOT_FOUND); - } - - try { - $log = $dbForProject->getDocument('executions', $logId); - } catch (NotFoundException) { - $log = new Document(); - } - - if ($log->getAttribute('resourceType') !== 'sites' && $log->getAttribute('resourceInternalId') !== $site->getSequence()) { - throw new Exception(Exception::LOG_NOT_FOUND); - } - - if ($log->isEmpty()) { - throw new Exception(Exception::LOG_NOT_FOUND); - } - - // Override status in response if the log is stuck in waiting/processing beyond the site timeout. - $status = $log->getAttribute('status', ''); - if ($status === 'waiting' || $status === 'processing') { - $timeout = $site->getAttribute('timeout', 30); - $elapsed = \time() - \strtotime($log->getCreatedAt()); - if ($elapsed >= $timeout) { - $log->setAttribute('status', 'failed'); - } - } - - $response->dynamic($log, Response::MODEL_EXECUTION); //TODO: Change to model log, but model log already exists - decide what to do - } -} diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Logs/XList.php b/src/Appwrite/Platform/Modules/Sites/Http/Logs/XList.php deleted file mode 100644 index 66b86150db..0000000000 --- a/src/Appwrite/Platform/Modules/Sites/Http/Logs/XList.php +++ /dev/null @@ -1,168 +0,0 @@ -setHttpMethod(Action::HTTP_REQUEST_METHOD_GET) - ->setHttpPath('/v1/sites/:siteId/logs') - ->desc('List logs') - ->groups(['api', 'sites']) - ->label('scope', 'log.read') - ->label('resourceType', RESOURCE_TYPE_SITES) - ->label('sdk', new Method( - namespace: 'sites', - group: 'logs', - name: 'listLogs', - description: <<param('siteId', '', fn (Database $dbForProject) => new UID($dbForProject->getAdapter()->getMaxUIDLength()), 'Site ID.', false, ['dbForProject']) - ->param('queries', [], new Logs(), 'Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/queries). Maximum of ' . APP_LIMIT_ARRAY_PARAMS_SIZE . ' queries are allowed, each ' . APP_LIMIT_ARRAY_ELEMENT_SIZE . ' characters long. You may filter on the following attributes: ' . implode(', ', Executions::ALLOWED_ATTRIBUTES), true) - ->param('total', true, new Boolean(true), 'When set to false, the total count returned will be 0 and will not be calculated.', true) - ->inject('response') - ->inject('dbForProject') - ->callback($this->action(...)); - } - - public function action(string $siteId, array $queries, bool $includeTotal, Response $response, Database $dbForProject) - { - $site = $dbForProject->getDocument('sites', $siteId); - - if ($site->isEmpty() || !$site->getAttribute('enabled')) { - throw new Exception(Exception::SITE_NOT_FOUND); - } - - try { - $queries = Query::parseQueries($queries); - } catch (QueryException $e) { - throw new Exception(Exception::GENERAL_QUERY_INVALID, $e->getMessage()); - } - - // Set internal queries - $queries[] = Query::equal('resourceInternalId', [$site->getSequence()]); - $queries[] = Query::equal('resourceType', ['sites']); - - $cursor = Query::getCursorQueries($queries, false); - $cursor = \reset($cursor); - - if ($cursor !== false) { - $validator = new Cursor(); - if (!$validator->isValid($cursor)) { - throw new Exception(Exception::GENERAL_QUERY_INVALID, $validator->getDescription()); - } - - $logId = $cursor->getValue(); - try { - $cursorDocument = $dbForProject->getDocument('executions', $logId); - } catch (NotFoundException) { - $cursorDocument = new Document(); - } - - if ($cursorDocument->isEmpty() || $cursorDocument->getAttribute('resourceType') !== 'sites') { - throw new Exception(Exception::GENERAL_CURSOR_NOT_FOUND, "Log '{$logId}' for the 'cursor' value not found."); - } - - $cursor->setValue($cursorDocument); - } - - // Calculate the cutoff datetime before which a waiting/processing log is considered timed out. - $timeout = $site->getAttribute('timeout', 30); - $thresholdDate = new \DateTime("-{$timeout} seconds"); - $threshold = DateTime::format($thresholdDate); - - // Capture what statuses the caller explicitly requested, before we mutate the query. - $requestedStatuses = []; - foreach ($queries as $query) { - if ($query->getMethod() === Query::TYPE_EQUAL && $query->getAttribute() === 'status') { - $requestedStatuses = [...$requestedStatuses, ...$query->getValues()]; - } - } - - // If the caller is filtering by 'failed', expand the DB query to also return - // waiting/processing logs created before the timeout threshold, so timed-out - // logs that were never marked failed in the DB are included in the results. - foreach ($queries as $index => $query) { - if ($query->getMethod() === Query::TYPE_EQUAL && $query->getAttribute() === 'status' && \in_array('failed', $query->getValues())) { - $queries[$index] = Query::or([ - $query, - Query::and([ - Query::equal('status', ['waiting', 'processing']), - Query::createdBefore($threshold), - ]), - ]); - break; - } - } - - $filterQueries = Query::groupByType($queries)['filters']; - - try { - $results = $dbForProject->find('executions', $queries); - $total = $includeTotal ? $dbForProject->count('executions', $filterQueries, APP_LIMIT_COUNT) : 0; - } catch (NotFoundException) { - $results = []; - $total = 0; - } catch (OrderException $e) { - throw new Exception(Exception::DATABASE_QUERY_ORDER_NULL, "The order attribute '{$e->getAttribute()}' had a null value. Cursor pagination requires all documents order attribute values are non-null."); - } - - // Override status in response for timed-out logs, but only when the caller - // did not explicitly request a non-failed status (e.g. waiting/processing). - if (empty(\array_diff($requestedStatuses, ['failed']))) { - foreach ($results as $log) { - $status = $log->getAttribute('status', ''); - if ($status === 'waiting' || $status === 'processing') { - $elapsed = \time() - \strtotime($log->getCreatedAt()); - if ($elapsed >= $timeout) { - $log->setAttribute('status', 'failed'); - } - } - } - } - - $response->dynamic(new Document([ - 'executions' => $results, - 'total' => $total, - ]), Response::MODEL_EXECUTION_LIST); // TODO: Update response model - } -} diff --git a/src/Appwrite/Platform/Modules/Sites/Services/Http.php b/src/Appwrite/Platform/Modules/Sites/Services/Http.php index 89290736fc..177a8a1007 100644 --- a/src/Appwrite/Platform/Modules/Sites/Services/Http.php +++ b/src/Appwrite/Platform/Modules/Sites/Services/Http.php @@ -12,9 +12,6 @@ use Appwrite\Platform\Modules\Sites\Http\Deployments\Template\Create as CreateTe use Appwrite\Platform\Modules\Sites\Http\Deployments\Vcs\Create as CreateVcsDeployment; use Appwrite\Platform\Modules\Sites\Http\Deployments\XList as ListDeployments; use Appwrite\Platform\Modules\Sites\Http\Frameworks\XList as ListFrameworks; -use Appwrite\Platform\Modules\Sites\Http\Logs\Delete as DeleteLog; -use Appwrite\Platform\Modules\Sites\Http\Logs\Get as GetLog; -use Appwrite\Platform\Modules\Sites\Http\Logs\XList as ListLogs; use Appwrite\Platform\Modules\Sites\Http\Sites\Create as CreateSite; use Appwrite\Platform\Modules\Sites\Http\Sites\Delete as DeleteSite; use Appwrite\Platform\Modules\Sites\Http\Sites\Deployment\Update as UpdateSiteDeployment; @@ -59,11 +56,6 @@ class Http extends Service $this->addAction(CreateDuplicateDeployment::getName(), new CreateDuplicateDeployment()); $this->addAction(UpdateDeploymentStatus::getName(), new UpdateDeploymentStatus()); - // Logs - $this->addAction(GetLog::getName(), new GetLog()); - $this->addAction(ListLogs::getName(), new ListLogs()); - $this->addAction(DeleteLog::getName(), new DeleteLog()); - // Variables $this->addAction(CreateVariable::getName(), new CreateVariable()); $this->addAction(GetVariable::getName(), new GetVariable()); diff --git a/src/Appwrite/Utopia/Database/Validator/Queries/Base.php b/src/Appwrite/Utopia/Database/Validator/Queries/Base.php index 5e180b1d8b..9d9bbde00b 100644 --- a/src/Appwrite/Utopia/Database/Validator/Queries/Base.php +++ b/src/Appwrite/Utopia/Database/Validator/Queries/Base.php @@ -22,14 +22,7 @@ class Base extends Queries * @param string[] $allowedAttributes * @throws \Exception */ - /** - * @param string $collection - * @param string[] $allowedAttributes - * @param array $fallbackTypes attribute => Database::VAR_* map used when - * the collection is not part of this platform's schema (e.g. executions on Server CE) - * @throws \Exception - */ - public function __construct(string $collection, array $allowedAttributes, array $fallbackTypes = []) + public function __construct(string $collection, array $allowedAttributes) { $config = Config::getParam('collections', []); @@ -41,7 +34,7 @@ class Base extends Queries $config['logs'] ); - $collection = $collections[$collection] ?? null; + $collection = $collections[$collection]; $allowedAttributesLookup = []; foreach ($allowedAttributes as $attribute) { @@ -50,7 +43,7 @@ class Base extends Queries $allAttributes = []; $attributes = []; - foreach ($collection['attributes'] ?? [] as $attribute) { + foreach ($collection['attributes'] as $attribute) { $key = $attribute['$id']; $attributeDocument = new Document([ @@ -66,19 +59,6 @@ class Base extends Queries } } - if ($collection === null) { - foreach ($allowedAttributes as $key) { - $attributeDocument = new Document([ - 'key' => $key, - 'type' => $fallbackTypes[$key] ?? Database::VAR_STRING, - 'array' => false, - ]); - - $allAttributes[] = $attributeDocument; - $attributes[] = $attributeDocument; - } - } - $internalAttributes = [ new Document([ 'key' => '$id', diff --git a/src/Appwrite/Utopia/Database/Validator/Queries/Executions.php b/src/Appwrite/Utopia/Database/Validator/Queries/Executions.php index ef5d36aede..4c0a38b46f 100644 --- a/src/Appwrite/Utopia/Database/Validator/Queries/Executions.php +++ b/src/Appwrite/Utopia/Database/Validator/Queries/Executions.php @@ -2,8 +2,6 @@ namespace Appwrite\Utopia\Database\Validator\Queries; -use Utopia\Database\Database; - class Executions extends Base { public const ALLOWED_ATTRIBUTES = [ @@ -16,22 +14,12 @@ class Executions extends Base 'deploymentId' ]; - public const ATTRIBUTE_TYPES = [ - 'trigger' => Database::VAR_STRING, - 'status' => Database::VAR_STRING, - 'responseStatusCode' => Database::VAR_INTEGER, - 'duration' => Database::VAR_FLOAT, - 'requestMethod' => Database::VAR_STRING, - 'requestPath' => Database::VAR_STRING, - 'deploymentId' => Database::VAR_STRING, - ]; - /** * Expression constructor * */ public function __construct() { - parent::__construct('executions', self::ALLOWED_ATTRIBUTES, self::ATTRIBUTE_TYPES); + parent::__construct('executions', self::ALLOWED_ATTRIBUTES); } } diff --git a/src/Appwrite/Utopia/Database/Validator/Queries/Logs.php b/src/Appwrite/Utopia/Database/Validator/Queries/Logs.php index cf4c3ad7ad..d07d3dd3d2 100644 --- a/src/Appwrite/Utopia/Database/Validator/Queries/Logs.php +++ b/src/Appwrite/Utopia/Database/Validator/Queries/Logs.php @@ -2,8 +2,6 @@ namespace Appwrite\Utopia\Database\Validator\Queries; -use Utopia\Database\Database; - class Logs extends Base { public const ALLOWED_ATTRIBUTES = [ @@ -15,21 +13,12 @@ class Logs extends Base 'deploymentId' ]; - public const ATTRIBUTE_TYPES = [ - 'status' => Database::VAR_STRING, - 'responseStatusCode' => Database::VAR_INTEGER, - 'duration' => Database::VAR_FLOAT, - 'requestMethod' => Database::VAR_STRING, - 'requestPath' => Database::VAR_STRING, - 'deploymentId' => Database::VAR_STRING, - ]; - /** * Expression constructor * */ public function __construct() { - parent::__construct('executions', self::ALLOWED_ATTRIBUTES, self::ATTRIBUTE_TYPES); //TODO: Update this later + parent::__construct('executions', self::ALLOWED_ATTRIBUTES); //TODO: Update this later } } diff --git a/tests/e2e/Services/Functions/FunctionsCustomClientTest.php b/tests/e2e/Services/Functions/FunctionsCustomClientTest.php index 077dc71f4b..a4b459bd9f 100644 --- a/tests/e2e/Services/Functions/FunctionsCustomClientTest.php +++ b/tests/e2e/Services/Functions/FunctionsCustomClientTest.php @@ -112,50 +112,6 @@ final class FunctionsCustomClientTest extends Scope $this->cleanupFunction($functionId); } - public function testCreateHeadExecution() - { - /** - * Test for SUCCESS - */ - $functionId = $this->setupFunction([ - 'functionId' => ID::unique(), - 'name' => 'Test', - 'execute' => [Role::user($this->getUser()['$id'])->toString()], - 'runtime' => 'node-22', - 'entrypoint' => 'index.js', - 'events' => [ - 'users.*.create', - 'users.*.delete', - ], - 'timeout' => 10, - ]); - $this->setupDeployment($functionId, [ - 'code' => $this->packageFunction('basic'), - 'activate' => true - ]); - - // Deny create async execution as guest - $execution = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/executions', [ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - ], [ - 'async' => true, - ]); - $this->assertEquals(401, $execution['headers']['status-code']); - - // Allow create async execution as user - $execution = $this->client->call(Client::METHOD_HEAD, '/functions/' . $functionId . '/executions', array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders()), [ - 'async' => true, - ]); - $this->assertEquals(200, $execution['headers']['status-code']); - $this->assertEmpty($execution['body']); - - $this->cleanupFunction($functionId); - } - public function testCreateCustomExecution(): array { /** diff --git a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php index 832a74be0e..608ba28eb0 100644 --- a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php +++ b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php @@ -7,7 +7,6 @@ namespace Tests\E2E\Services\Functions; use Appwrite\Platform\Modules\Compute\Specification; use Appwrite\Tests\Retry; use PHPUnit\Framework\Attributes\DataProvider; -use PHPUnit\Framework\Attributes\Group; use Tests\E2E\Client; use Tests\E2E\Scopes\ProjectCustom; use Tests\E2E\Scopes\Scope; @@ -27,7 +26,6 @@ final class FunctionsCustomServerTest extends Scope protected static array $testFunctionCache = []; protected static array $testDeploymentCache = []; - protected static array $testExecutionCache = []; /** * Setup a test function with variables for independent tests (with static caching) @@ -173,32 +171,6 @@ final class FunctionsCustomServerTest extends Scope /** * Setup a test execution for independent tests (with static caching) */ - protected function setupTestExecution(): array - { - $cacheKey = $this->getProject()['$id'] ?? 'default'; - if (!empty(static::$testExecutionCache[$cacheKey])) { - return static::$testExecutionCache[$cacheKey]; - } - - $data = $this->setupTestDeployment(); - $functionId = $data['functionId']; - - $execution = $this->createExecution($functionId, [ - 'async' => 'false', - ]); - - $this->assertEquals(201, $execution['headers']['status-code']); - $this->assertEquals('completed', $execution['body']['status']); - - $executionId = $execution['body']['$id'] ?? ''; - - static::$testExecutionCache[$cacheKey] = array_merge($data, [ - 'executionId' => $executionId, - ]); - - return static::$testExecutionCache[$cacheKey]; - } - public function testListSpecs(): void { $specifications = $this->listSpecifications(['type' => 'runtimes']); @@ -1632,47 +1604,6 @@ final class FunctionsCustomServerTest extends Scope $this->assertContains($execution['headers']['status-code'], [204, 404]); } - #[Group('ceOnly')] - public function testListExecutions(): void - { - $data = $this->setupTestExecution(); - - /** - * Executions are not persisted, so lists are always empty - */ - $executions = $this->listExecutions($data['functionId']); - - $this->assertEquals(200, $executions['headers']['status-code']); - $this->assertEquals(0, $executions['body']['total']); - $this->assertIsArray($executions['body']['executions']); - $this->assertCount(0, $executions['body']['executions']); - - $executionsWithIncludeTotalFalse = $this->listExecutions($data['functionId'], ['total' => false]); - - $this->assertEquals(200, $executionsWithIncludeTotalFalse['headers']['status-code']); - $this->assertIsArray($executionsWithIncludeTotalFalse['body']['executions']); - $this->assertSame(0, $executionsWithIncludeTotalFalse['body']['total']); - $this->assertCount(0, $executionsWithIncludeTotalFalse['body']['executions']); - - $executions = $this->listExecutions($data['functionId'], [ - 'queries' => [ - Query::equal('deploymentId', [$data['deploymentId']])->toString(), - ], - ]); - - $this->assertEquals(200, $executions['headers']['status-code']); - $this->assertEquals(0, $executions['body']['total']); - $this->assertCount(0, $executions['body']['executions']); - - $executions = $this->listExecutions($data['functionId'], [ - 'search' => $data['executionId'], - ]); - - $this->assertEquals(200, $executions['headers']['status-code']); - $this->assertEquals(0, $executions['body']['total']); - $this->assertCount(0, $executions['body']['executions']); - } - public function testSyncCreateExecution(): void { $data = $this->setupTestDeployment(); @@ -1695,69 +1626,6 @@ final class FunctionsCustomServerTest extends Scope $this->assertLessThan(1.500, $execution['body']['duration']); } - #[Group('ceOnly')] - public function testGetExecution(): void - { - $data = $this->setupTestExecution(); - - /** - * Executions are not persisted, so gets return 404 - */ - $execution = $this->getExecution($data['functionId'], $data['executionId']); - - $this->assertEquals(404, $execution['headers']['status-code']); - - $function = $this->getExecution($data['functionId'], 'x'); - - $this->assertEquals(404, $function['headers']['status-code']); - } - - - #[Group('ceOnly')] - public function testDeleteExecution(): void - { - // Create fresh execution for this test since we delete it - $data = $this->setupTestDeployment(); - - $execution = $this->createExecution($data['functionId'], [ - 'async' => 'false', - ]); - - $this->assertEquals(201, $execution['headers']['status-code']); - $executionId = $execution['body']['$id'] ?? ''; - - /** - * Executions are not persisted, so deletes return 404 - */ - $execution = $this->client->call(Client::METHOD_DELETE, '/functions/' . $data['functionId'] . '/executions/' . $executionId, array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders())); - - $this->assertEquals(404, $execution['headers']['status-code']); - $this->assertStringContainsString('Execution with the requested ID could not be found', (string) $execution['body']['message']); - - $execution = $this->client->call(Client::METHOD_POST, '/functions/' . $data['functionId'] . '/executions', array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders()), [ - 'async' => true, - ]); - - $executionId = $execution['body']['$id'] ?? ''; - - $this->assertEquals(202, $execution['headers']['status-code']); - - $execution = $this->client->call(Client::METHOD_DELETE, '/functions/' . $data['functionId'] . '/executions/' . $executionId, array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders())); - - $this->assertEquals(404, $execution['headers']['status-code']); - } - - - public function testUpdateSpecs(): void { $data = $this->setupTestDeployment(); diff --git a/tests/e2e/Services/FunctionsSchedule/FunctionsScheduleTest.php b/tests/e2e/Services/FunctionsSchedule/FunctionsScheduleTest.php index 5b49318752..f864bbd61f 100644 --- a/tests/e2e/Services/FunctionsSchedule/FunctionsScheduleTest.php +++ b/tests/e2e/Services/FunctionsSchedule/FunctionsScheduleTest.php @@ -132,42 +132,4 @@ final class FunctionsScheduleTest extends Scope $this->cleanupFunction($functionId); } - public function testDeleteScheduledExecution() - { - $functionId = $this->setupFunction([ - 'functionId' => ID::unique(), - 'name' => 'Test', - 'execute' => [Role::user($this->getUser()['$id'])->toString()], - 'runtime' => 'node-22', - 'entrypoint' => 'index.js', - 'timeout' => 10, - 'logging' => true, - ]); - - $this->setupDeployment($functionId, [ - 'code' => $this->packageFunction('basic'), - 'activate' => true - ]); - - $futureTime = (new \DateTime())->add(new \DateInterval('PT10H')); - $futureTime->setTime((int) $futureTime->format('H'), (int) $futureTime->format('i'), 0, 0); - - $execution = $this->createExecution($functionId, [ - 'async' => true, - 'scheduledAt' => $futureTime->format('Y-m-d H:i:s'), - ]); - - $this->assertEquals(202, $execution['headers']['status-code']); - - $executionId = $execution['body']['$id'] ?? ''; - - $execution = $this->client->call(Client::METHOD_DELETE, '/functions/' . $functionId . '/executions/' . $executionId, array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders())); - - $this->assertEquals(204, $execution['headers']['status-code']); - - $this->cleanupFunction($functionId); - } } diff --git a/tests/e2e/Services/GraphQL/FunctionsClientTest.php b/tests/e2e/Services/GraphQL/FunctionsClientTest.php index 057e448aae..21153ec5cc 100644 --- a/tests/e2e/Services/GraphQL/FunctionsClientTest.php +++ b/tests/e2e/Services/GraphQL/FunctionsClientTest.php @@ -5,7 +5,6 @@ declare(strict_types=1); namespace Tests\E2E\Services\GraphQL; use Appwrite\Tests\Async; -use PHPUnit\Framework\Attributes\Group; use Tests\E2E\Client; use Tests\E2E\Scopes\ProjectCustom; use Tests\E2E\Scopes\Scope; @@ -210,65 +209,4 @@ final class FunctionsClientTest extends Scope $this->assertNotEmpty($execution); } - /** - * @return array - * @throws \Exception - */ - public function testGetExecutions(): array - { - $function = $this->setupFunction(); - - $projectId = $this->getProject()['$id']; - $query = $this->getQuery(self::GET_EXECUTIONS); - $gqlPayload = [ - 'query' => $query, - 'variables' => [ - 'functionId' => $function['_id'], - ] - ]; - - $executions = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $projectId, - ], $this->getHeaders()), $gqlPayload); - - $this->assertIsArray($executions['body']['data']); - $this->assertArrayNotHasKey('errors', $executions['body']); - $executions = $executions['body']['data']['functionsListExecutions']; - $this->assertIsArray($executions); - - return $executions; - } - - /** - * @return array - * @throws \Exception - */ - #[Group('ceOnly')] - public function testGetExecution(): array - { - $function = $this->setupFunction(); - $execution = $this->setupExecution(); - - $projectId = $this->getProject()['$id']; - $query = $this->getQuery(self::GET_EXECUTION); - $gqlPayload = [ - 'query' => $query, - 'variables' => [ - 'functionId' => $function['_id'], - 'executionId' => $execution['_id'], - ] - ]; - - $execution = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $projectId, - ], $this->getHeaders()), $gqlPayload); - - // Executions are not persisted, so gets return not found - $this->assertArrayHasKey('errors', $execution['body']); - $this->assertStringContainsString('could not be found', (string) $execution['body']['errors'][0]['message']); - - return []; - } } diff --git a/tests/e2e/Services/GraphQL/FunctionsServerTest.php b/tests/e2e/Services/GraphQL/FunctionsServerTest.php index 96ad222005..34440578eb 100644 --- a/tests/e2e/Services/GraphQL/FunctionsServerTest.php +++ b/tests/e2e/Services/GraphQL/FunctionsServerTest.php @@ -6,7 +6,6 @@ namespace Tests\E2E\Services\GraphQL; use Appwrite\Tests\Async; use Appwrite\Tests\Async\Exceptions\Critical; -use PHPUnit\Framework\Attributes\Group; use Tests\E2E\Client; use Tests\E2E\Scopes\ProjectCustom; use Tests\E2E\Scopes\Scope; @@ -373,67 +372,6 @@ final class FunctionsServerTest extends Scope return $deployment; } - /** - * @return array - * @throws \Exception - */ - public function testGetExecutions(): array - { - $function = $this->setupFunction(); - - $projectId = $this->getProject()['$id']; - $query = $this->getQuery(self::GET_EXECUTIONS); - $gqlPayload = [ - 'query' => $query, - 'variables' => [ - 'functionId' => $function['_id'], - ] - ]; - - $executions = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $projectId, - ], $this->getHeaders()), $gqlPayload); - - $this->assertIsArray($executions['body']['data']); - $this->assertArrayNotHasKey('errors', $executions['body']); - $executions = $executions['body']['data']['functionsListExecutions']; - $this->assertIsArray($executions); - - return $executions; - } - - /** - * @return array - * @throws \Exception - */ - #[Group('ceOnly')] - public function testGetExecution(): array - { - $execution = $this->setupExecution(); - - $projectId = $this->getProject()['$id']; - $query = $this->getQuery(self::GET_EXECUTION); - $gqlPayload = [ - 'query' => $query, - 'variables' => [ - 'functionId' => $execution['functionId'], - 'executionId' => $execution['_id'], - ] - ]; - - $execution = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $projectId, - ], $this->getHeaders()), $gqlPayload); - - // Executions are not persisted, so gets return not found - $this->assertArrayHasKey('errors', $execution['body']); - $this->assertStringContainsString('could not be found', (string) $execution['body']['errors'][0]['message']); - - return []; - } - /** * @return array * @throws \Exception From 1318017be4b3a16da4b82705f9dfc53cef278d63 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Fri, 10 Jul 2026 16:24:58 +0530 Subject: [PATCH 12/15] (feat): cancel scheduled executions via ExecutionCancelled event --- .../Bus/Events/ExecutionCancelled.php | 18 +++ .../Functions/Http/Executions/Delete.php | 143 ++++++++++++++++++ .../Modules/Functions/Services/Http.php | 2 + .../FunctionsScheduleTest.php | 49 ++++++ 4 files changed, 212 insertions(+) create mode 100644 src/Appwrite/Bus/Events/ExecutionCancelled.php create mode 100644 src/Appwrite/Platform/Modules/Functions/Http/Executions/Delete.php diff --git a/src/Appwrite/Bus/Events/ExecutionCancelled.php b/src/Appwrite/Bus/Events/ExecutionCancelled.php new file mode 100644 index 0000000000..f96d9fec07 --- /dev/null +++ b/src/Appwrite/Bus/Events/ExecutionCancelled.php @@ -0,0 +1,18 @@ + $execution + * @param array $project + */ + public function __construct( + public readonly array $execution, + public readonly array $project, + ) { + } +} diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Executions/Delete.php b/src/Appwrite/Platform/Modules/Functions/Http/Executions/Delete.php new file mode 100644 index 0000000000..4a51e642c4 --- /dev/null +++ b/src/Appwrite/Platform/Modules/Functions/Http/Executions/Delete.php @@ -0,0 +1,143 @@ +setHttpMethod(Action::HTTP_REQUEST_METHOD_DELETE) + ->setHttpPath('/v1/functions/:functionId/executions/:executionId') + ->desc('Delete execution') + ->groups(['api', 'functions']) + ->label('scope', ['executions.write', 'execution.write']) + ->label('resourceType', RESOURCE_TYPE_FUNCTIONS) + ->label('event', 'functions.[functionId].executions.[executionId].delete') + ->label('audits.event', 'executions.delete') + ->label('audits.resource', 'function/{request.functionId}') + ->label('usage.resource', 'function/{request.functionId}') + ->label('sdk', new Method( + namespace: 'functions', + group: 'executions', + name: 'deleteExecution', + description: <<param('functionId', '', fn (Database $dbForProject) => new UID($dbForProject->getAdapter()->getMaxUIDLength()), 'Function ID.', false, ['dbForProject']) + ->param('executionId', '', fn (Database $dbForProject) => new UID($dbForProject->getAdapter()->getMaxUIDLength()), 'Execution ID.', false, ['dbForProject']) + ->inject('response') + ->inject('project') + ->inject('dbForProject') + ->inject('dbForPlatform') + ->inject('queueForEvents') + ->inject('authorization') + ->inject('bus') + ->callback($this->action(...)); + } + + public function action( + string $functionId, + string $executionId, + Response $response, + Document $project, + Database $dbForProject, + Database $dbForPlatform, + Event $queueForEvents, + Authorization $authorization, + Bus $bus, + ) { + $function = $dbForProject->getDocument('functions', $functionId); + + if ($function->isEmpty()) { + throw new Exception(Exception::FUNCTION_NOT_FOUND); + } + + // Executions are not persisted by Server CE; only a pending scheduled + // execution can be cancelled, through its schedule. + $schedule = $authorization->skip(fn () => $dbForPlatform->findOne('schedules', [ + Query::equal('resourceId', [$executionId]), + Query::equal('resourceType', [SCHEDULE_RESOURCE_TYPE_EXECUTION]), + Query::equal('projectInternalId', [$project->getSequence()]), + Query::equal('active', [true]), + ])); + + if ($schedule->isEmpty()) { + throw new Exception(Exception::EXECUTION_NOT_FOUND); + } + + $authorization->skip(fn () => $dbForPlatform->updateDocument('schedules', $schedule->getId(), new Document([ + 'resourceUpdatedAt' => DateTime::now(), + 'active' => false, + ]))); + + $execution = new Document([ + '$id' => $executionId, + '$createdAt' => DateTime::now(), + '$updatedAt' => DateTime::now(), + '$permissions' => [], + 'functionId' => $function->getId(), + 'resourceId' => $function->getId(), + 'resourceType' => 'functions', + 'deploymentId' => '', + 'trigger' => 'schedule', + 'status' => 'scheduled', + 'requestMethod' => '', + 'requestPath' => '', + 'requestHeaders' => [], + 'responseStatusCode' => 0, + 'responseBody' => '', + 'responseHeaders' => [], + 'logs' => '', + 'errors' => '', + 'duration' => 0.0, + ]); + + $bus->dispatch(new ExecutionCancelled( + execution: $execution->getArrayCopy(), + project: $project->getArrayCopy(), + )); + + $queueForEvents + ->setParam('functionId', $function->getId()) + ->setParam('executionId', $executionId) + ->setPayload($response->output($execution, Response::MODEL_EXECUTION)); + + $response->noContent(); + } +} diff --git a/src/Appwrite/Platform/Modules/Functions/Services/Http.php b/src/Appwrite/Platform/Modules/Functions/Services/Http.php index 32b4ee5bc4..cdefa45cac 100644 --- a/src/Appwrite/Platform/Modules/Functions/Services/Http.php +++ b/src/Appwrite/Platform/Modules/Functions/Services/Http.php @@ -12,6 +12,7 @@ use Appwrite\Platform\Modules\Functions\Http\Deployments\Template\Create as Crea use Appwrite\Platform\Modules\Functions\Http\Deployments\Vcs\Create as CreateVcsDeployment; use Appwrite\Platform\Modules\Functions\Http\Deployments\XList as ListDeployments; use Appwrite\Platform\Modules\Functions\Http\Executions\Create as CreateExecution; +use Appwrite\Platform\Modules\Functions\Http\Executions\Delete as DeleteExecution; use Appwrite\Platform\Modules\Functions\Http\Functions\Create as CreateFunction; use Appwrite\Platform\Modules\Functions\Http\Functions\Delete as DeleteFunction; use Appwrite\Platform\Modules\Functions\Http\Functions\Deployment\Update as UpdateFunctionDeployment; @@ -62,6 +63,7 @@ class Http extends Service // Executions $this->addAction(CreateExecution::getName(), new CreateExecution()); + $this->addAction(DeleteExecution::getName(), new DeleteExecution()); // Variables $this->addAction(CreateVariable::getName(), new CreateVariable()); diff --git a/tests/e2e/Services/FunctionsSchedule/FunctionsScheduleTest.php b/tests/e2e/Services/FunctionsSchedule/FunctionsScheduleTest.php index f864bbd61f..1d7b487c30 100644 --- a/tests/e2e/Services/FunctionsSchedule/FunctionsScheduleTest.php +++ b/tests/e2e/Services/FunctionsSchedule/FunctionsScheduleTest.php @@ -132,4 +132,53 @@ final class FunctionsScheduleTest extends Scope $this->cleanupFunction($functionId); } + + public function testDeleteScheduledExecution() + { + $functionId = $this->setupFunction([ + 'functionId' => ID::unique(), + 'name' => 'Test', + 'execute' => [Role::user($this->getUser()['$id'])->toString()], + 'runtime' => 'node-22', + 'entrypoint' => 'index.js', + 'timeout' => 10, + 'logging' => true, + ]); + + $this->setupDeployment($functionId, [ + 'code' => $this->packageFunction('basic'), + 'activate' => true + ]); + + $futureTime = (new \DateTime())->add(new \DateInterval('PT10H')); + $futureTime->setTime((int) $futureTime->format('H'), (int) $futureTime->format('i'), 0, 0); + + $execution = $this->createExecution($functionId, [ + 'async' => true, + 'scheduledAt' => $futureTime->format('Y-m-d H:i:s'), + ]); + + $this->assertEquals(202, $execution['headers']['status-code']); + + $executionId = $execution['body']['$id'] ?? ''; + + $execution = $this->client->call(Client::METHOD_DELETE, '/functions/' . $functionId . '/executions/' . $executionId, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + + $this->assertEquals(204, $execution['headers']['status-code']); + + /* Cancelling again eventually finds nothing to cancel */ + $this->assertEventually(function () use ($functionId, $executionId) { + $execution = $this->client->call(Client::METHOD_DELETE, '/functions/' . $functionId . '/executions/' . $executionId, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + + $this->assertEquals(404, $execution['headers']['status-code']); + }, 15000, 500); + + $this->cleanupFunction($functionId); + } } From c327a94cf57be754ac88663f682f71d9ec41e054 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Fri, 10 Jul 2026 16:54:18 +0530 Subject: [PATCH 13/15] (fix): verify function ownership when cancelling scheduled execution --- .../Platform/Modules/Functions/Http/Executions/Delete.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Executions/Delete.php b/src/Appwrite/Platform/Modules/Functions/Http/Executions/Delete.php index 4a51e642c4..d85995e7a1 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Executions/Delete.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Executions/Delete.php @@ -101,6 +101,10 @@ class Delete extends Base throw new Exception(Exception::EXECUTION_NOT_FOUND); } + if (($schedule->getAttribute('data')['functionId'] ?? null) !== $function->getId()) { + throw new Exception(Exception::EXECUTION_NOT_FOUND); + } + $authorization->skip(fn () => $dbForPlatform->updateDocument('schedules', $schedule->getId(), new Document([ 'resourceUpdatedAt' => DateTime::now(), 'active' => false, From 0413f08131f4981ced20ea5dc7ab5c140acf0922 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Fri, 10 Jul 2026 17:38:47 +0530 Subject: [PATCH 14/15] (refactor): make schedule resource loading polymorphic --- src/Appwrite/Platform/Tasks/ScheduleBase.php | 26 +++---------------- .../Platform/Tasks/ScheduleExecutions.php | 16 ++++++++++-- 2 files changed, 17 insertions(+), 25 deletions(-) diff --git a/src/Appwrite/Platform/Tasks/ScheduleBase.php b/src/Appwrite/Platform/Tasks/ScheduleBase.php index d49d483ae2..56250f8ea5 100644 --- a/src/Appwrite/Platform/Tasks/ScheduleBase.php +++ b/src/Appwrite/Platform/Tasks/ScheduleBase.php @@ -36,9 +36,9 @@ abstract class ScheduleBase extends Action abstract public static function getSupportedResource(): string; abstract public static function getCollectionId(): string; - public static function loadResource(): bool + protected function loadResource(Document $project, callable $getProjectDB, array $schedule): Document { - return true; + return $getProjectDB($project)->getDocument(static::getCollectionId(), $schedule['resourceId']); } abstract protected function enqueueResources(Database $dbForPlatform, callable $getProjectDB): void; @@ -273,29 +273,9 @@ abstract class ScheduleBase extends Action $this->schedules[$sequence]['project'] = $project; - if (!static::loadResource()) { - // Best-effort load: schedules from before the executions - // collection was dropped can still resolve their document; - // newer ones carry what they need in the schedule data. - try { - $resource = $getProjectDB($project)->getDocument(static::getCollectionId(), $schedule['resourceId']); - } catch (\Throwable) { - $resource = new Document(); - } - - if ($resource->isEmpty()) { - $resource = new Document([ - '$id' => $schedule['resourceId'], - ]); - } - - $this->schedules[$sequence]['resource'] = $resource; - continue; - } - // In case the resource is not found (project deleted). try { - $resource = $getProjectDB($project)->getDocument(static::getCollectionId(), $schedule['resourceId']); + $resource = $this->loadResource($project, $getProjectDB, $schedule); } catch (\Throwable $th) { Console::error("Failed to load resource: projectId::{$schedule['projectId']} resourceId::{$schedule['resourceId']}"); Console::error($th->getMessage()); diff --git a/src/Appwrite/Platform/Tasks/ScheduleExecutions.php b/src/Appwrite/Platform/Tasks/ScheduleExecutions.php index feb49cf4a6..2d54811ab8 100644 --- a/src/Appwrite/Platform/Tasks/ScheduleExecutions.php +++ b/src/Appwrite/Platform/Tasks/ScheduleExecutions.php @@ -7,6 +7,7 @@ use Appwrite\Event\Publisher\Func as FunctionPublisher; use Swoole\Coroutine as Co; use Utopia\Console; use Utopia\Database\Database; +use Utopia\Database\Document; /** * ScheduleExecutions @@ -34,9 +35,20 @@ class ScheduleExecutions extends ScheduleBase return RESOURCE_TYPE_EXECUTIONS; } - public static function loadResource(): bool + protected function loadResource(Document $project, callable $getProjectDB, array $schedule): Document { - return false; + // Executions are not persisted; the schedule carries what the worker + // needs. Schedules from before the executions collection was dropped + // can still resolve their document for the functionId their data lacks. + try { + $resource = parent::loadResource($project, $getProjectDB, $schedule); + } catch (\Throwable) { + $resource = new Document(); + } + + return $resource->isEmpty() + ? new Document(['$id' => $schedule['resourceId']]) + : $resource; } protected function enqueueResources(Database $dbForPlatform, callable $getProjectDB): void From 695fb6d9cc7b888eb1620baf43a54751cd087c2f Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Mon, 13 Jul 2026 10:52:15 +0530 Subject: [PATCH 15/15] Update Vibes to 0.3.16 --- docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index 6bf8768eff..fd9dee1235 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -218,7 +218,7 @@ services: max-file: "5" max-size: 10m container_name: appwrite-console - image: appwrite/vibes:0.3.5 + image: appwrite/vibes:0.3.16 restart: unless-stopped networks: - appwrite