1
0
Fork 0
mirror of synced 2024-07-04 06:00:53 +12:00

Add function logging toggle

This commit is contained in:
Matej Bačo 2023-02-24 10:24:20 +01:00
parent 068acef0f6
commit 464f88634c
9 changed files with 56 additions and 17 deletions

View file

@ -2313,6 +2313,16 @@ $collections = [
'required' => true,
'array' => false,
],
[
'$id' => ID::custom('logging'),
'type' => Database::VAR_BOOLEAN,
'signed' => true,
'size' => 0,
'format' => '',
'filters' => [],
'required' => true,
'array' => false,
],
[
'$id' => ID::custom('runtime'),
'type' => Database::VAR_STRING,

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

@ -1 +1 @@
Subproject commit cc84474d77de8b726fd477d794c262a00b2f04e8
Subproject commit 007bcc514294034029d4cbd0a53c684cfc5d5978

View file

@ -67,19 +67,21 @@ App::post('/v1/functions')
->param('schedule', '', new Cron(), 'Schedule CRON syntax.', true)
->param('timeout', 15, new Range(1, (int) App::getEnv('_APP_FUNCTIONS_TIMEOUT', 900)), 'Function maximum execution time in seconds.', true)
->param('enabled', true, new Boolean(), 'Is function enabled?', true)
->param('logging', true, new Boolean(), 'Do executions get logged?', true)
->inject('response')
->inject('dbForProject')
->inject('project')
->inject('user')
->inject('events')
->inject('dbForConsole')
->action(function (string $functionId, string $name, array $execute, string $runtime, array $events, string $schedule, int $timeout, bool $enabled, Response $response, Database $dbForProject, Document $project, Document $user, Event $eventsInstance, Database $dbForConsole) {
->action(function (string $functionId, string $name, array $execute, string $runtime, array $events, string $schedule, int $timeout, bool $enabled, bool $logging, Response $response, Database $dbForProject, Document $project, Document $user, Event $eventsInstance, Database $dbForConsole) {
$functionId = ($functionId == 'unique()') ? ID::unique() : $functionId;
$function = $dbForProject->createDocument('functions', new Document([
'$id' => $functionId,
'execute' => $execute,
'enabled' => $enabled,
'logging' => $logging,
'name' => $name,
'runtime' => $runtime,
'deploymentInternalId' => '',
@ -489,13 +491,14 @@ App::put('/v1/functions/:functionId')
->param('schedule', '', new Cron(), 'Schedule CRON syntax.', true)
->param('timeout', 15, new Range(1, (int) App::getEnv('_APP_FUNCTIONS_TIMEOUT', 900)), 'Maximum execution time in seconds.', true)
->param('enabled', true, new Boolean(), 'Is function enabled?', true)
->param('logging', true, new Boolean(), 'Do executions get logged?', true)
->inject('response')
->inject('dbForProject')
->inject('project')
->inject('user')
->inject('events')
->inject('dbForConsole')
->action(function (string $functionId, string $name, array $execute, array $events, string $schedule, int $timeout, bool $enabled, Response $response, Database $dbForProject, Document $project, Document $user, Event $eventsInstance, Database $dbForConsole) {
->action(function (string $functionId, string $name, array $execute, array $events, string $schedule, int $timeout, bool $enabled, bool $logging, Response $response, Database $dbForProject, Document $project, Document $user, Event $eventsInstance, Database $dbForConsole) {
$function = $dbForProject->getDocument('functions', $functionId);
@ -513,6 +516,7 @@ App::put('/v1/functions/:functionId')
'scheduleUpdatedAt' => DateTime::now(),
'timeout' => $timeout,
'enabled' => $enabled,
'logging' => $logging,
'search' => implode(' ', [$functionId, $name, $function->getAttribute('runtime')]),
])));
@ -1166,8 +1170,7 @@ App::post('/v1/functions/:functionId/executions')
$executionId = ID::unique();
/** @var Document $execution */
$execution = Authorization::skip(fn () => $dbForProject->createDocument('executions', new Document([
$execution = new Document([
'$id' => $executionId,
'$permissions' => !$user->isEmpty() ? [Permission::read(Role::user($user->getId()))] : [],
'functionInternalId' => $function->getInternalId(),
@ -1183,7 +1186,13 @@ App::post('/v1/functions/:functionId/executions')
'logs' => '',
'duration' => 0.0,
'search' => implode(' ', [$functionId, $executionId]),
])));
]);
if($function->getAttribute('logging')) {
/** @var Document $execution */
$execution = Authorization::skip(fn () => $dbForProject->createDocument('executions', $execution));
}
$jwt = ''; // initialize
if (!$user->isEmpty()) { // If userId exists, generate a JWT for function
@ -1285,7 +1294,10 @@ App::post('/v1/functions/:functionId/executions')
Console::error($th->getMessage());
}
Authorization::skip(fn () => $dbForProject->updateDocument('executions', $executionId, $execution));
if($function->getAttribute('logging')) {
$execution->setAttribute('body', \mb_strcut($execution->getAttribute('body') , 0, 1000000)); // Limit to 1MB
Authorization::skip(fn () => $dbForProject->updateDocument('executions', $executionId, $execution));
}
// TODO revise this later using route label
$usage

View file

@ -81,13 +81,13 @@ Server::setResource('execute', function () {
$execution = $dbForProject->getDocument('executions', $executionId ?? '');
if ($execution->isEmpty()) {
$executionId = ID::unique();
$execution = $dbForProject->createDocument('executions', new Document([
$execution = new Document([
'$id' => $executionId,
'$permissions' => $user->isEmpty() ? [] : [Permission::read(Role::user($user->getId()))],
'functionId' => $functionId,
'deploymentId' => $deploymentId,
'trigger' => $trigger,
'status' => 'waiting',
'status' => 'processing',
'statusCode' => 0,
'body' => '',
'headers' => [],
@ -95,7 +95,11 @@ Server::setResource('execute', function () {
'logs' => '',
'duration' => 0.0,
'search' => implode(' ', [$functionId, $executionId]),
]));
]);
if($function->getAttribute('logging')) {
$execution = $dbForProject->createDocument('executions', $execution);
}
// TODO: @Meldiron Trigger executions.create event here
@ -104,8 +108,13 @@ Server::setResource('execute', function () {
}
}
$execution->setAttribute('status', 'processing');
$execution = $dbForProject->updateDocument('executions', $executionId, $execution);
if($execution->getAttribute('status') !== 'processing') {
$execution->setAttribute('status', 'processing');
if($function->getAttribute('logging')) {
$execution = $dbForProject->updateDocument('executions', $executionId, $execution);
}
}
$vars = array_reduce($function->getAttribute('vars', []), function (array $carry, Document $var) {
$carry[$var->getAttribute('key')] = $var->getAttribute('value');
@ -171,7 +180,9 @@ Server::setResource('execute', function () {
Console::error($th->getMessage());
}
$execution = $dbForProject->updateDocument('executions', $executionId, $execution);
if($function->getAttribute('logging')) {
$execution = $dbForProject->updateDocument('executions', $executionId, $execution);
}
/** Trigger Webhook */
$executionModel = new Execution();

View file

@ -55,6 +55,12 @@ class Func extends Model
'default' => true,
'example' => false,
])
->addRule('logging', [
'type' => self::TYPE_BOOLEAN,
'description' => 'Function logging.',
'default' => true,
'example' => false,
])
->addRule('runtime', [
'type' => self::TYPE_STRING,
'description' => 'Function execution runtime.',