diff --git a/Dockerfile b/Dockerfile index 1f042466f5..69de236a52 100755 --- a/Dockerfile +++ b/Dockerfile @@ -95,7 +95,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/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/init/resources.php b/app/init/resources.php index c41aa4489f..9dfa88c6d5 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\Jobs as JobsPublisher; use Appwrite\Event\Publisher\Mail as MailPublisher; @@ -135,11 +134,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/Events/ExecutionScheduled.php b/src/Appwrite/Bus/Events/ExecutionScheduled.php new file mode 100644 index 0000000000..ff4091a90e --- /dev/null +++ b/src/Appwrite/Bus/Events/ExecutionScheduled.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/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..44f2b7b362 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Executions/Create.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Executions/Create.php @@ -3,6 +3,8 @@ namespace Appwrite\Platform\Modules\Functions\Http\Executions; use Ahc\Jwt\JWT; +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; @@ -16,13 +18,13 @@ 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; 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; @@ -96,7 +98,6 @@ class Create extends Base ->inject('dbForPlatform') ->inject('user') ->inject('queueForEvents') - ->inject('usage') ->inject('publisherForFunctions') ->inject('geoRecord') ->inject('store') @@ -106,6 +107,7 @@ class Create extends Base ->inject('authorization') ->inject('publisherForDeletes') ->inject('executionsRetentionCount') + ->inject('bus') ->callback($this->action(...)); } @@ -124,7 +126,6 @@ class Create extends Base Database $dbForPlatform, User $user, Event $queueForEvents, - Context $usage, FunctionPublisher $publisherForFunctions, GeoRecord $geoRecord, Store $store, @@ -134,6 +135,7 @@ class Create extends Base Authorization $authorization, DeletePublisher $publisherForDeletes, int $executionsRetentionCount, + Bus $bus, ) { $async = \strval($async) === 'true' || \strval($async) === '1'; @@ -263,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', @@ -287,7 +291,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, @@ -307,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([ @@ -328,7 +332,10 @@ 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(), + )); } if ($executionsRetentionCount > 0 && ENABLE_EXECUTIONS_LIMIT_ON_ROUTE) { @@ -486,19 +493,12 @@ 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 ExecutionCompleted( + execution: $execution->getArrayCopy(), + project: $project->getArrayCopy(), + spec: $spec, + resource: $function->getArrayCopy(), + )); } $executionResponse['headers']['x-appwrite-execution-id'] = $execution->getId(); diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Executions/Delete.php b/src/Appwrite/Platform/Modules/Functions/Http/Executions/Delete.php index 227793f264..d85995e7a1 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Executions/Delete.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Executions/Delete.php @@ -2,6 +2,7 @@ namespace Appwrite\Platform\Modules\Functions\Http\Executions; +use Appwrite\Bus\Events\ExecutionCancelled; use Appwrite\Event\Event; use Appwrite\Extend\Exception; use Appwrite\Platform\Modules\Compute\Base; @@ -10,6 +11,7 @@ use Appwrite\SDK\ContentType; use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; +use Utopia\Bus\Bus; use Utopia\Database\Database; use Utopia\Database\DateTime; use Utopia\Database\Document; @@ -60,10 +62,12 @@ 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') ->inject('authorization') + ->inject('bus') ->callback($this->action(...)); } @@ -71,10 +75,12 @@ class Delete extends Base string $functionId, string $executionId, Response $response, + Document $project, Database $dbForProject, Database $dbForPlatform, Event $queueForEvents, - Authorization $authorization + Authorization $authorization, + Bus $bus, ) { $function = $dbForProject->getDocument('functions', $functionId); @@ -82,55 +88,58 @@ class Delete extends Base throw new Exception(Exception::FUNCTION_NOT_FOUND); } - $execution = $dbForProject->getDocument('executions', $executionId); - if ($execution->isEmpty()) { + // 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); } - if ($execution->getAttribute('resourceType') !== 'functions' && $execution->getAttribute('resourceInternalId') !== $function->getSequence()) { + if (($schedule->getAttribute('data')['functionId'] ?? null) !== $function->getId()) { 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'; - } - } + $authorization->skip(fn () => $dbForPlatform->updateDocument('schedules', $schedule->getId(), new Document([ + 'resourceUpdatedAt' => DateTime::now(), + 'active' => false, + ]))); - if (!in_array($status, ['completed', 'failed', 'scheduled'])) { - throw new Exception(Exception::EXECUTION_IN_PROGRESS); - } + $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, + ]); - 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'), - ]))); - } - } + $bus->dispatch(new ExecutionCancelled( + execution: $execution->getArrayCopy(), + project: $project->getArrayCopy(), + )); $queueForEvents ->setParam('functionId', $function->getId()) - ->setParam('executionId', $execution->getId()) + ->setParam('executionId', $executionId) ->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 027ff51166..0000000000 --- a/src/Appwrite/Platform/Modules/Functions/Http/Executions/Get.php +++ /dev/null @@ -1,100 +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); - } - - $execution = $dbForProject->getDocument('executions', $executionId); - - 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 29bed70cd0..0000000000 --- a/src/Appwrite/Platform/Modules/Functions/Http/Executions/XList.php +++ /dev/null @@ -1,174 +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(); - $cursorDocument = $dbForProject->getDocument('executions', $executionId); - - 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 (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 d9698c5915..8e36014207 100644 --- a/src/Appwrite/Platform/Modules/Functions/Services/Http.php +++ b/src/Appwrite/Platform/Modules/Functions/Services/Http.php @@ -13,8 +13,6 @@ use Appwrite\Platform\Modules\Functions\Http\Deployments\Vcs\Create as CreateVcs 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; @@ -69,8 +67,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 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 88927b9fb4..0000000000 --- a/src/Appwrite/Platform/Modules/Sites/Http/Logs/Delete.php +++ /dev/null @@ -1,90 +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); - } - - $log = $dbForProject->getDocument('executions', $logId); - 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 c482add2da..0000000000 --- a/src/Appwrite/Platform/Modules/Sites/Http/Logs/Get.php +++ /dev/null @@ -1,86 +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); - } - - $log = $dbForProject->getDocument('executions', $logId); - - 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 89fb3ee2e6..0000000000 --- a/src/Appwrite/Platform/Modules/Sites/Http/Logs/XList.php +++ /dev/null @@ -1,160 +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(); - $cursorDocument = $dbForProject->getDocument('executions', $logId); - - 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 (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/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/Tasks/ScheduleBase.php b/src/Appwrite/Platform/Tasks/ScheduleBase.php index 5842bd7f1a..56250f8ea5 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; + + protected function loadResource(Document $project, callable $getProjectDB, array $schedule): Document + { + return $getProjectDB($project)->getDocument(static::getCollectionId(), $schedule['resourceId']); + } abstract protected function enqueueResources(Database $dbForPlatform, callable $getProjectDB): void; public function __construct() @@ -270,7 +275,7 @@ abstract class ScheduleBase extends Action // 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 49dd851b6d..2d54811ab8 100644 --- a/src/Appwrite/Platform/Tasks/ScheduleExecutions.php +++ b/src/Appwrite/Platform/Tasks/ScheduleExecutions.php @@ -5,7 +5,9 @@ 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; +use Utopia\Database\Document; /** * ScheduleExecutions @@ -33,6 +35,22 @@ class ScheduleExecutions extends ScheduleBase return RESOURCE_TYPE_EXECUTIONS; } + protected function loadResource(Document $project, callable $getProjectDB, array $schedule): Document + { + // 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 { $intervalEnd = (new \DateTime())->modify('+' . self::ENQUEUE_TIMER . ' seconds'); @@ -63,11 +81,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); } @@ -75,7 +107,7 @@ class ScheduleExecutions extends ScheduleBase $publisherForFunctions->enqueue(new FunctionMessage( project: $schedule['project'], userId: $data['userId'] ?? '', - functionId: $schedule['resource']['resourceId'], + functionId: $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/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 f724774eaf..c09b95b6f0 100644 --- a/src/Appwrite/Platform/Workers/Functions.php +++ b/src/Appwrite/Platform/Workers/Functions.php @@ -313,11 +313,14 @@ 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', '')); @@ -325,7 +328,7 @@ class Functions extends Action Span::add('execution.status', $execution->getAttribute('status', '')); $bus->dispatch(new ExecutionCompleted( - execution: $execution->getArrayCopy(), + execution: $executionForEvent->getArrayCopy(), project: $project->getArrayCopy(), resource: $function->getArrayCopy(), )); diff --git a/tests/e2e/Services/Functions/FunctionsCustomClientTest.php b/tests/e2e/Services/Functions/FunctionsCustomClientTest.php index c04631bf60..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 { /** @@ -219,14 +175,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,23 +542,6 @@ 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) { - $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']); - $this->assertStringContainsString($documentId, (string) $lastExecution['logs']); - }, 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 b5ff53828f..608ba28eb0 100644 --- a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php +++ b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php @@ -26,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) @@ -172,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']); @@ -750,19 +723,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, $totalUsers) { - $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']); $this->assertEquals(200, $deployment['headers']['status-code']); $this->assertGreaterThan(0, $deployment['body']['sourceSize']); @@ -1619,12 +1579,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 */ + /** 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(204, $execution['headers']['status-code']); + $this->assertContains($execution['headers']['status-code'], [204, 404]); /** Test create execution with 400 status code */ $execution = $this->createExecution($data['functionId'], [ @@ -1636,118 +1596,12 @@ final class FunctionsCustomServerTest extends Scope $this->assertEquals('completed', $execution['body']['status']); $this->assertEquals(400, $execution['body']['responseStatusCode']); - /** Delete execution */ + /** 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(204, $execution['headers']['status-code']); - } - - public function testListExecutions(): void - { - $data = $this->setupTestExecution(); - - /** - * Test for SUCCESS - */ - $executions = $this->listExecutions($data['functionId']); - - $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'])); - // Verify our execution is in the list (don't assume position) - $deploymentIds = array_column($executions['body']['executions'], 'deploymentId'); - $this->assertContains($data['deploymentId'], $deploymentIds); - - /** - * 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'])); - - $executions = $this->listExecutions($data['functionId'], [ - 'queries' => [ - Query::equal('deploymentId', [$data['deploymentId']])->toString(), - ], - ]); - - $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->assertContains($execution['headers']['status-code'], [204, 404]); } public function testSyncCreateExecution(): void @@ -1772,85 +1626,6 @@ final class FunctionsCustomServerTest extends Scope $this->assertLessThan(1.500, $execution['body']['duration']); } - public function testGetExecution(): void - { - $data = $this->setupTestExecution(); - - /** - * Test for SUCCESS - */ - $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']); - - /** - * Test for FAILURE - */ - $function = $this->getExecution($data['functionId'], 'x'); - - $this->assertEquals(404, $function['headers']['status-code']); - } - - - 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'] ?? ''; - - /** - * Test for SUCCESS - */ - $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'], - ], $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(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']); - } - - - public function testUpdateSpecs(): void { $data = $this->setupTestDeployment(); @@ -2117,22 +1892,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']); - $this->assertEquals('', $execution['body']['logs']); - $this->assertStringContainsString('timed out', (string) $execution['body']['errors']); - }, 10000, 500); + $this->assertNotEmpty($execution['body']['$id']); + $this->assertEquals('waiting', $execution['body']['status']); $this->cleanupFunction($functionId); } @@ -2182,18 +1943,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); } @@ -2348,23 +2099,6 @@ final class FunctionsCustomServerTest extends Scope $userId = $user['body']['$id'] ?? ''; - $this->assertEventually(function () use ($functionId, $userId) { - $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->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'] ?? ''); - }, 20_000, 500); - $this->cleanupFunction($functionId); // Cleanup user @@ -2418,19 +2152,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']); - $this->assertNotEmpty($execution['body']['logs']); - $this->assertStringContainsString("total", (string) $execution['body']['logs']); - }, 10000, 500); - $this->cleanupFunction($functionId); } @@ -2510,17 +2231,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); } @@ -2732,17 +2442,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); @@ -2756,30 +2455,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 946f4069aa..1d7b487c30 100644 --- a/tests/e2e/Services/FunctionsSchedule/FunctionsScheduleTest.php +++ b/tests/e2e/Services/FunctionsSchedule/FunctionsScheduleTest.php @@ -42,32 +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->assertNotEmpty($asyncExecution['logs']); - $this->assertNotEmpty($asyncExecution['errors']); - $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); } @@ -126,23 +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->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); - /* Test for FAILURE */ // Schedule synchronous execution $execution = $this->createExecution($functionId, [ @@ -175,6 +132,7 @@ final class FunctionsScheduleTest extends Scope $this->cleanupFunction($functionId); } + public function testDeleteScheduledExecution() { $functionId = $this->setupFunction([ @@ -211,6 +169,16 @@ final class FunctionsScheduleTest extends Scope $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); } } diff --git a/tests/e2e/Services/GraphQL/FunctionsClientTest.php b/tests/e2e/Services/GraphQL/FunctionsClientTest.php index d649412685..21153ec5cc 100644 --- a/tests/e2e/Services/GraphQL/FunctionsClientTest.php +++ b/tests/e2e/Services/GraphQL/FunctionsClientTest.php @@ -209,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 - */ - 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); - - $this->assertIsArray($execution['body']['data']); - $this->assertArrayNotHasKey('errors', $execution['body']); - $execution = $execution['body']['data']['functionsGetExecution']; - $this->assertIsArray($execution); - - return $execution; - } } diff --git a/tests/e2e/Services/GraphQL/FunctionsServerTest.php b/tests/e2e/Services/GraphQL/FunctionsServerTest.php index cc0f2e0cb3..34440578eb 100644 --- a/tests/e2e/Services/GraphQL/FunctionsServerTest.php +++ b/tests/e2e/Services/GraphQL/FunctionsServerTest.php @@ -372,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 - */ - 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); - - $this->assertIsArray($execution['body']['data']); - $this->assertArrayNotHasKey('errors', $execution['body']); - $execution = $execution['body']['data']['functionsGetExecution']; - $this->assertIsArray($execution); - - return $execution; - } - /** * @return array * @throws \Exception 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());