1
0
Fork 0
mirror of synced 2024-07-09 08:27:01 +12:00

Merge branch 'refactor-usage-sn' of github.com:appwrite/appwrite into feat-get-env-sn

This commit is contained in:
shimon 2024-05-22 16:39:42 +03:00
commit 7c71e44250
12 changed files with 47 additions and 23 deletions

View file

@ -134,7 +134,7 @@ return [
Exception::USER_COUNT_EXCEEDED => [ Exception::USER_COUNT_EXCEEDED => [
'name' => Exception::USER_COUNT_EXCEEDED, 'name' => Exception::USER_COUNT_EXCEEDED,
'description' => 'The current project has exceeded the maximum number of users. Please check your user limit in the Appwrite console.', 'description' => 'The current project has exceeded the maximum number of users. Please check your user limit in the Appwrite console.',
'code' => 501, 'code' => 400,
], ],
Exception::USER_CONSOLE_COUNT_EXCEEDED => [ Exception::USER_CONSOLE_COUNT_EXCEEDED => [
'name' => Exception::USER_CONSOLE_COUNT_EXCEEDED, 'name' => Exception::USER_CONSOLE_COUNT_EXCEEDED,
@ -519,6 +519,11 @@ return [
'description' => 'Entrypoint for your Appwrite Function is missing. Please specify it when making deployment or update the entrypoint under your function\'s "Settings" > "Configuration" > "Entrypoint".', 'description' => 'Entrypoint for your Appwrite Function is missing. Please specify it when making deployment or update the entrypoint under your function\'s "Settings" > "Configuration" > "Entrypoint".',
'code' => 404, 'code' => 404,
], ],
Exception::FUNCTION_SYNCHRONOUS_TIMEOUT => [
'name' => Exception::FUNCTION_SYNCHRONOUS_TIMEOUT,
'description' => 'Synchronous function execution timed out. Use asynchronous execution instead, or ensure the execution duration doesn\'t exceed 30 seconds.',
'code' => 408,
],
/** Builds */ /** Builds */
Exception::BUILD_NOT_FOUND => [ Exception::BUILD_NOT_FOUND => [

View file

@ -9,6 +9,7 @@ use Appwrite\Event\Func;
use Appwrite\Event\Usage; use Appwrite\Event\Usage;
use Appwrite\Event\Validator\FunctionEvent; use Appwrite\Event\Validator\FunctionEvent;
use Appwrite\Extend\Exception; use Appwrite\Extend\Exception;
use Appwrite\Extend\Exception as AppwriteException;
use Appwrite\Messaging\Adapter\Realtime; use Appwrite\Messaging\Adapter\Realtime;
use Appwrite\Task\Validator\Cron; use Appwrite\Task\Validator\Cron;
use Appwrite\Utopia\Database\Validator\CustomId; use Appwrite\Utopia\Database\Validator\CustomId;
@ -1750,6 +1751,10 @@ App::post('/v1/functions/:functionId/executions')
->setAttribute('responseStatusCode', 500) ->setAttribute('responseStatusCode', 500)
->setAttribute('errors', $th->getMessage() . '\nError Code: ' . $th->getCode()); ->setAttribute('errors', $th->getMessage() . '\nError Code: ' . $th->getCode());
Console::error($th->getMessage()); Console::error($th->getMessage());
if ($th instanceof AppwriteException) {
throw $th;
}
} finally { } finally {
$queueForUsage $queueForUsage
->addMetric(METRIC_EXECUTIONS, 1) ->addMetric(METRIC_EXECUTIONS, 1)
@ -1757,12 +1762,12 @@ App::post('/v1/functions/:functionId/executions')
->addMetric(METRIC_EXECUTIONS_COMPUTE, (int)($execution->getAttribute('duration') * 1000)) // per project ->addMetric(METRIC_EXECUTIONS_COMPUTE, (int)($execution->getAttribute('duration') * 1000)) // per project
->addMetric(str_replace('{functionInternalId}', $function->getInternalId(), METRIC_FUNCTION_ID_EXECUTIONS_COMPUTE), (int)($execution->getAttribute('duration') * 1000)) // per function ->addMetric(str_replace('{functionInternalId}', $function->getInternalId(), METRIC_FUNCTION_ID_EXECUTIONS_COMPUTE), (int)($execution->getAttribute('duration') * 1000)) // per function
; ;
}
if ($function->getAttribute('logging')) { if ($function->getAttribute('logging')) {
/** @var Document $execution */ /** @var Document $execution */
$execution = Authorization::skip(fn () => $dbForProject->createDocument('executions', $execution)); $execution = Authorization::skip(fn () => $dbForProject->createDocument('executions', $execution));
} }
}
$roles = Authorization::getRoles(); $roles = Authorization::getRoles();
$isPrivilegedUser = Auth::isPrivilegedUser($roles); $isPrivilegedUser = Auth::isPrivilegedUser($roles);

View file

@ -152,10 +152,10 @@ App::post('/v1/projects')
throw new Exception(Exception::PROJECT_RESERVED_PROJECT, "'console' is a reserved project."); throw new Exception(Exception::PROJECT_RESERVED_PROJECT, "'console' is a reserved project.");
} }
// TODO: One in 20 projects use shared tables. Temporary until all projects are using shared tables. // TODO: 1 in 5 projects use shared tables. Temporary until all projects are using shared tables.
if ( if (
( (
!\mt_rand(0, 19) !\mt_rand(0, 4)
&& System::getEnv('_APP_DATABASE_SHARED_TABLES', 'enabled') === 'enabled' && System::getEnv('_APP_DATABASE_SHARED_TABLES', 'enabled') === 'enabled'
&& System::getEnv('_APP_EDITION', 'self-hosted') !== 'self-hosted' && System::getEnv('_APP_EDITION', 'self-hosted') !== 'self-hosted'
) || ) ||

View file

@ -289,6 +289,7 @@ function router(App $utopia, Database $dbForConsole, callable $getProjectDB, Swo
$execution->setAttribute('logs', $executionResponse['logs']); $execution->setAttribute('logs', $executionResponse['logs']);
$execution->setAttribute('errors', $executionResponse['errors']); $execution->setAttribute('errors', $executionResponse['errors']);
$execution->setAttribute('duration', $executionResponse['duration']); $execution->setAttribute('duration', $executionResponse['duration']);
} catch (\Throwable $th) { } catch (\Throwable $th) {
$durationEnd = \microtime(true); $durationEnd = \microtime(true);
@ -298,6 +299,10 @@ function router(App $utopia, Database $dbForConsole, callable $getProjectDB, Swo
->setAttribute('responseStatusCode', 500) ->setAttribute('responseStatusCode', 500)
->setAttribute('errors', $th->getMessage() . '\nError Code: ' . $th->getCode()); ->setAttribute('errors', $th->getMessage() . '\nError Code: ' . $th->getCode());
Console::error($th->getMessage()); Console::error($th->getMessage());
if ($th instanceof AppwriteException) {
throw $th;
}
} finally { } finally {
$queueForUsage $queueForUsage
->addMetric(METRIC_EXECUTIONS, 1) ->addMetric(METRIC_EXECUTIONS, 1)
@ -305,12 +310,12 @@ function router(App $utopia, Database $dbForConsole, callable $getProjectDB, Swo
->addMetric(METRIC_EXECUTIONS_COMPUTE, (int)($execution->getAttribute('duration') * 1000)) // per project ->addMetric(METRIC_EXECUTIONS_COMPUTE, (int)($execution->getAttribute('duration') * 1000)) // per project
->addMetric(str_replace('{functionInternalId}', $function->getInternalId(), METRIC_FUNCTION_ID_EXECUTIONS_COMPUTE), (int)($execution->getAttribute('duration') * 1000)) // per function ->addMetric(str_replace('{functionInternalId}', $function->getInternalId(), METRIC_FUNCTION_ID_EXECUTIONS_COMPUTE), (int)($execution->getAttribute('duration') * 1000)) // per function
; ;
}
if ($function->getAttribute('logging')) { if ($function->getAttribute('logging')) {
/** @var Document $execution */ /** @var Document $execution */
$execution = Authorization::skip(fn () => $dbForProject->createDocument('executions', $execution)); $execution = Authorization::skip(fn () => $dbForProject->createDocument('executions', $execution));
} }
}
$execution->setAttribute('logs', ''); $execution->setAttribute('logs', '');
$execution->setAttribute('errors', ''); $execution->setAttribute('errors', '');

View file

@ -215,6 +215,7 @@ const MESSAGE_TYPE_PUSH = 'push';
const METRIC_TEAMS = 'teams'; const METRIC_TEAMS = 'teams';
const METRIC_USERS = 'users'; const METRIC_USERS = 'users';
const METRIC_MESSAGES = 'messages'; const METRIC_MESSAGES = 'messages';
const METRIC_MESSAGES_COUNTRY_CODE = '{countryCode}.messages';
const METRIC_SESSIONS = 'sessions'; const METRIC_SESSIONS = 'sessions';
const METRIC_DATABASES = 'databases'; const METRIC_DATABASES = 'databases';
const METRIC_COLLECTIONS = 'collections'; const METRIC_COLLECTIONS = 'collections';

View file

@ -731,7 +731,7 @@ services:
<<: *x-logging <<: *x-logging
restart: unless-stopped restart: unless-stopped
stop_signal: SIGINT stop_signal: SIGINT
image: openruntimes/executor:0.4.12 image: openruntimes/executor:0.5.5
networks: networks:
- appwrite - appwrite
- runtimes - runtimes

View file

@ -338,10 +338,6 @@ $worker
$pools->reclaim(); $pools->reclaim();
$version = System::getEnv('_APP_VERSION', 'UNKNOWN'); $version = System::getEnv('_APP_VERSION', 'UNKNOWN');
if ($error instanceof PDOException) {
throw $error;
}
if ($logger) { if ($logger) {
$log->setNamespace("appwrite-worker"); $log->setNamespace("appwrite-worker");
$log->setServer(\gethostname()); $log->setServer(\gethostname());

View file

@ -794,7 +794,7 @@ services:
hostname: exc1 hostname: exc1
<<: *x-logging <<: *x-logging
stop_signal: SIGINT stop_signal: SIGINT
image: openruntimes/executor:0.5.1 image: openruntimes/executor:0.5.5
restart: unless-stopped restart: unless-stopped
networks: networks:
- appwrite - appwrite

View file

@ -58,7 +58,6 @@ class Usage extends Event
public function trigger(): string|bool public function trigger(): string|bool
{ {
$client = new Client($this->queue, $this->connection); $client = new Client($this->queue, $this->connection);
return $client->enqueue([ return $client->enqueue([
'project' => $this->getProject(), 'project' => $this->getProject(),
'reduce' => $this->reduce, 'reduce' => $this->reduce,

View file

@ -153,6 +153,7 @@ class Exception extends \Exception
public const FUNCTION_NOT_FOUND = 'function_not_found'; public const FUNCTION_NOT_FOUND = 'function_not_found';
public const FUNCTION_RUNTIME_UNSUPPORTED = 'function_runtime_unsupported'; public const FUNCTION_RUNTIME_UNSUPPORTED = 'function_runtime_unsupported';
public const FUNCTION_ENTRYPOINT_MISSING = 'function_entrypoint_missing'; public const FUNCTION_ENTRYPOINT_MISSING = 'function_entrypoint_missing';
public const FUNCTION_SYNCHRONOUS_TIMEOUT = 'function_synchronous_timeout';
/** Deployments */ /** Deployments */
public const DEPLOYMENT_NOT_FOUND = 'deployment_not_found'; public const DEPLOYMENT_NOT_FOUND = 'deployment_not_found';

View file

@ -441,17 +441,24 @@ class Messaging extends Action
try { try {
$adapter->send($data); $adapter->send($data);
$countryCode = $adapter->getCountryCode($message['to'][0] ?? '');
if (!empty($countryCode)) {
$queueForUsage
->addMetric(str_replace('{countryCode}', $countryCode, METRIC_MESSAGES_COUNTRY_CODE), 1);
}
$queueForUsage $queueForUsage
->addMetric(METRIC_MESSAGES, 1) ->addMetric(METRIC_MESSAGES, 1)
->setProject($project) ->setProject($project)
->trigger(); ->trigger();
} catch (\Throwable $e) { } catch (\Throwable $th) {
throw new \Exception('Failed sending to targets with error: ' . $e->getMessage()); throw new \Exception('Failed sending to targets with error: ' . $th->getMessage());
} }
}; };
}, $batches)); }, $batches));
} }
private function getSmsAdapter(Document $provider): ?SMSAdapter private function getSmsAdapter(Document $provider): ?SMSAdapter
{ {
$credentials = $provider->getAttribute('credentials'); $credentials = $provider->getAttribute('credentials');

View file

@ -2,6 +2,7 @@
namespace Executor; namespace Executor;
use Appwrite\Extend\Exception as AppwriteException;
use Exception; use Exception;
use Utopia\System\System; use Utopia\System\System;
@ -193,7 +194,6 @@ class Executor
'path' => $path, 'path' => $path,
'method' => $method, 'method' => $method,
'headers' => $headers, 'headers' => $headers,
'image' => $image, 'image' => $image,
'source' => $source, 'source' => $source,
'entrypoint' => $entrypoint, 'entrypoint' => $entrypoint,
@ -311,6 +311,8 @@ class Executor
$responseType = $responseHeaders['content-type'] ?? ''; $responseType = $responseHeaders['content-type'] ?? '';
$responseStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE); $responseStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curlError = curl_errno($ch);
$curlErrorMessage = curl_error($ch);
if ($decode) { if ($decode) {
switch (substr($responseType, 0, strpos($responseType, ';'))) { switch (substr($responseType, 0, strpos($responseType, ';'))) {
@ -327,8 +329,11 @@ class Executor
} }
} }
if ((curl_errno($ch)/* || 200 != $responseStatus*/)) { if ($curlError) {
throw new Exception(curl_error($ch) . ' with status code ' . $responseStatus, $responseStatus); if ($curlError == CURLE_OPERATION_TIMEDOUT) {
throw new AppwriteException(AppwriteException::FUNCTION_SYNCHRONOUS_TIMEOUT);
}
throw new Exception($curlErrorMessage . ' with status code ' . $responseStatus, $responseStatus);
} }
curl_close($ch); curl_close($ch);