mirror of
https://github.com/appwrite/appwrite.git
synced 2026-05-26 13:51:13 +00:00
100 lines
3.7 KiB
PHP
100 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace Appwrite\Platform\Modules\Functions\Http\Executions;
|
|
|
|
use Appwrite\Extend\Exception;
|
|
use Appwrite\Platform\Modules\Compute\Base;
|
|
use Appwrite\SDK\AuthType;
|
|
use Appwrite\SDK\Method;
|
|
use Appwrite\SDK\Response as SDKResponse;
|
|
use Appwrite\Utopia\Database\Documents\User;
|
|
use Appwrite\Utopia\Response;
|
|
use Utopia\Database\Database;
|
|
use Utopia\Database\Validator\Authorization;
|
|
use Utopia\Database\Validator\UID;
|
|
use Utopia\Platform\Action;
|
|
use Utopia\Platform\Scope\HTTP;
|
|
|
|
class Get extends Base
|
|
{
|
|
use HTTP;
|
|
|
|
public static function getName()
|
|
{
|
|
return 'getExecution';
|
|
}
|
|
|
|
public function __construct()
|
|
{
|
|
$this
|
|
->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('resourceType', RESOURCE_TYPE_FUNCTIONS)
|
|
->label('sdk', new Method(
|
|
namespace: 'functions',
|
|
group: 'executions',
|
|
name: 'getExecution',
|
|
description: <<<EOT
|
|
Get a function execution log by its unique ID.
|
|
EOT,
|
|
auth: [AuthType::ADMIN, AuthType::SESSION, AuthType::KEY, AuthType::JWT],
|
|
responses: [
|
|
new SDKResponse(
|
|
code: Response::STATUS_CODE_OK,
|
|
model: Response::MODEL_EXECUTION,
|
|
)
|
|
]
|
|
))
|
|
->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->isApp($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);
|
|
}
|
|
}
|