1
0
Fork 0
mirror of synced 2024-06-29 19:50:26 +12:00

Fix database stuff

This commit is contained in:
Bradley Schofield 2022-01-20 11:34:50 +00:00
parent 0694d53826
commit 4286e8d70b
4 changed files with 46 additions and 31 deletions

View file

@ -78,8 +78,8 @@ App::init(function ($utopia, $request, $response, $project, $user, $events, $aud
; ;
} }
if (($abuse->check() // Route is rate-limited if ((App::getEnv('_APP_OPTIONS_ABUSE', 'enabled') !== 'disabled' // Route is rate-limited
&& App::getEnv('_APP_OPTIONS_ABUSE', 'enabled') !== 'disabled') // Abuse is not disabled && $abuse->check()) // Abuse is not disabled
&& (!$isAppUser && !$isPrivilegedUser)) // User is not an admin or API key && (!$isAppUser && !$isPrivilegedUser)) // User is not an admin or API key
{ {
throw new Exception('Too many requests', 429); throw new Exception('Too many requests', 429);

View file

@ -36,10 +36,14 @@ use Utopia\Logger\Log;
require_once __DIR__ . '/init.php'; require_once __DIR__ . '/init.php';
Swoole\Runtime::enableCoroutine(true, SWOOLE_HOOK_ALL);
global $register; global $register;
$logError = function(Throwable $error, string $action, Utopia\Route $route = null) use ($register) { $logError = function(Throwable $error, string $action, Utopia\Route $route = null) use ($register) {
$logger = $register->get('logger'); $logger = $register->get('logger');
var_dump($error->getTraceAsString());
if($logger) { if($logger) {
$version = App::getEnv('_APP_VERSION', 'UNKNOWN'); $version = App::getEnv('_APP_VERSION', 'UNKNOWN');
@ -85,8 +89,6 @@ try {
$runtimes = Config::getParam('runtimes'); $runtimes = Config::getParam('runtimes');
Swoole\Runtime::enableCoroutine(true, SWOOLE_HOOK_ALL);
// Warmup: make sure images are ready to run fast 🚀 // Warmup: make sure images are ready to run fast 🚀
Co\run(function () use ($runtimes, $orchestration) { Co\run(function () use ($runtimes, $orchestration) {
foreach ($runtimes as $runtime) { foreach ($runtimes as $runtime) {
@ -149,6 +151,7 @@ $createRuntimeServer = function(string $functionId, string $projectId, string $t
}); });
if ($tag->getAttribute('buildId') === null) { if ($tag->getAttribute('buildId') === null) {
var_dump($tag->getArrayCopy());
throw new Exception('Tag has no buildId'); throw new Exception('Tag has no buildId');
} }
@ -337,7 +340,7 @@ $execute = function(string $trigger, string $projectId, string $executionId, str
return (!empty($executionId)) ? $database->getDocument('executions', $executionId) : $database->createDocument('executions', new Document([ return (!empty($executionId)) ? $database->getDocument('executions', $executionId) : $database->createDocument('executions', new Document([
'$id' => $executionId, '$id' => $executionId,
'$read' => (!$userId == '') ? ['user:' . $userId] : [], '$read' => (!$userId == '') ? ['user:' . $userId] : [],
'$write' => [], '$write' => ['role:all'],
'dateCreated' => time(), 'dateCreated' => time(),
'functionId' => $function->getId(), 'functionId' => $function->getId(),
'tagId' => $tag->getId(), 'tagId' => $tag->getId(),
@ -407,7 +410,7 @@ $execute = function(string $trigger, string $projectId, string $executionId, str
$database->createDocument('builds', new Document([ $database->createDocument('builds', new Document([
'$id' => $buildId, '$id' => $buildId,
'$read' => (!$userId == '') ? ['user:' . $userId] : [], '$read' => (!$userId == '') ? ['user:' . $userId] : [],
'$write' => [], '$write' => ['role:all'],
'dateCreated' => time(), 'dateCreated' => time(),
'status' => 'processing', 'status' => 'processing',
'outputPath' => '', 'outputPath' => '',
@ -856,7 +859,7 @@ App::post('/v1/tag')
$dbForProject->createDocument('builds', new Document([ $dbForProject->createDocument('builds', new Document([
'$id' => $buildId, '$id' => $buildId,
'$read' => (!empty($userId)) ? ['user:' . $userId] : [], '$read' => (!empty($userId)) ? ['user:' . $userId] : [],
'$write' => [], '$write' => ['role:all'],
'dateCreated' => time(), 'dateCreated' => time(),
'status' => 'processing', 'status' => 'processing',
'runtime' => $function->getAttribute('runtime'), 'runtime' => $function->getAttribute('runtime'),
@ -995,10 +998,19 @@ function runBuildStage(string $buildId, string $projectID, Database $database):
{ {
global $runtimes; global $runtimes;
global $orchestration; global $orchestration;
global $register;
$buildStdout = ''; $buildStdout = '';
$buildStderr = ''; $buildStderr = '';
$db = $register->get('dbPool')->get();
$redis = $register->get('redisPool')->get();
$cache = new Cache(new RedisCache($redis));
$database = new Database(new MariaDB($db), $cache);
$database->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite'));
$database->setNamespace('_project_'.$projectID);
// Check if build has already been run // Check if build has already been run
$build = Authorization::skip(function () use ($buildId, $database) { $build = Authorization::skip(function () use ($buildId, $database) {
return $database->getDocument('builds', $buildId); return $database->getDocument('builds', $buildId);
@ -1223,6 +1235,9 @@ function runBuildStage(string $buildId, string $projectID, Database $database):
} }
throw new Exception('Build failed: ' . $e->getMessage()); throw new Exception('Build failed: ' . $e->getMessage());
} finally {
$register->get('dbPool')->put($db);
$register->get('redisPool')->put($redis);
} }
return $build; return $build;
@ -1252,9 +1267,9 @@ $handleShutdown = function() use($logError)
$cache = $register->get('redisPool')->get(); $cache = $register->get('redisPool')->get();
$cache = new Cache(new RedisCache($cache)); $cache = new Cache(new RedisCache($cache));
$database = new Database(new MariaDB($db), $cache); $database = new Database(new MariaDB($db), $cache);
$database->setNamespace('project_'.$container->getLabels()["appwrite-project"].'_internal'); $database->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite'));
$database->setNamespace('_project_'.$container->getLabels()["appwrite-project"]);
// Get list of all processing executions // Get list of all processing executions
$executions = Authorization::skip(function () use ($database, $container) { $executions = Authorization::skip(function () use ($database, $container) {
@ -1386,7 +1401,7 @@ $http->on('request', function (SwooleRequest $swooleRequest, SwooleResponse $swo
->addHeader('Cache-Control', 'no-cache, no-store, must-revalidate') ->addHeader('Cache-Control', 'no-cache, no-store, must-revalidate')
->addHeader('Expires', '0') ->addHeader('Expires', '0')
->addHeader('Pragma', 'no-cache') ->addHeader('Pragma', 'no-cache')
->setStatusCode($code); ->setStatusCode(500);
$response->dynamic( $response->dynamic(
new Document($output), new Document($output),

32
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "0674f59e4259487551f294c7cc8ef1ee", "content-hash": "adf8727742248da9d7143546e513f96d",
"packages": [ "packages": [
{ {
"name": "adhocore/jwt", "name": "adhocore/jwt",
@ -118,8 +118,8 @@
"version": "dev-refactor", "version": "dev-refactor",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/appwrite/php-runtimes.git", "url": "https://github.com/appwrite/runtimes.git",
"reference": "30bf6781acee1e3f32c89821e5706ac6cf7d7c0e" "reference": "96a536e0ad7e3788cd2493e39cdf94b44206411d"
}, },
"require": { "require": {
"php": ">=8.0", "php": ">=8.0",
@ -160,7 +160,7 @@
"php", "php",
"runtimes" "runtimes"
], ],
"time": "2022-01-05T13:09:45+00:00" "time": "2022-01-19T09:26:05+00:00"
}, },
{ {
"name": "chillerlan/php-qrcode", "name": "chillerlan/php-qrcode",
@ -349,16 +349,16 @@
}, },
{ {
"name": "composer/package-versions-deprecated", "name": "composer/package-versions-deprecated",
"version": "1.11.99.4", "version": "1.11.99.5",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/composer/package-versions-deprecated.git", "url": "https://github.com/composer/package-versions-deprecated.git",
"reference": "b174585d1fe49ceed21928a945138948cb394600" "reference": "b4f54f74ef3453349c24a845d22392cd31e65f1d"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/composer/package-versions-deprecated/zipball/b174585d1fe49ceed21928a945138948cb394600", "url": "https://api.github.com/repos/composer/package-versions-deprecated/zipball/b4f54f74ef3453349c24a845d22392cd31e65f1d",
"reference": "b174585d1fe49ceed21928a945138948cb394600", "reference": "b4f54f74ef3453349c24a845d22392cd31e65f1d",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -402,7 +402,7 @@
"description": "Composer plugin that provides efficient querying for installed package versions (no runtime IO)", "description": "Composer plugin that provides efficient querying for installed package versions (no runtime IO)",
"support": { "support": {
"issues": "https://github.com/composer/package-versions-deprecated/issues", "issues": "https://github.com/composer/package-versions-deprecated/issues",
"source": "https://github.com/composer/package-versions-deprecated/tree/1.11.99.4" "source": "https://github.com/composer/package-versions-deprecated/tree/1.11.99.5"
}, },
"funding": [ "funding": [
{ {
@ -418,7 +418,7 @@
"type": "tidelift" "type": "tidelift"
} }
], ],
"time": "2021-09-13T08:41:34+00:00" "time": "2022-01-17T14:14:24+00:00"
}, },
{ {
"name": "dragonmantank/cron-expression", "name": "dragonmantank/cron-expression",
@ -3077,16 +3077,16 @@
}, },
{ {
"name": "appwrite/sdk-generator", "name": "appwrite/sdk-generator",
"version": "0.17.0", "version": "0.17.1",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/appwrite/sdk-generator.git", "url": "https://github.com/appwrite/sdk-generator.git",
"reference": "a68e072170a81532cfb0ff914864d8f074a73a37" "reference": "3542c6ed0f808b6a9f6735a8aad7ccda961bea29"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/appwrite/sdk-generator/zipball/a68e072170a81532cfb0ff914864d8f074a73a37", "url": "https://api.github.com/repos/appwrite/sdk-generator/zipball/3542c6ed0f808b6a9f6735a8aad7ccda961bea29",
"reference": "a68e072170a81532cfb0ff914864d8f074a73a37", "reference": "3542c6ed0f808b6a9f6735a8aad7ccda961bea29",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -3120,9 +3120,9 @@
"description": "Appwrite PHP library for generating API SDKs for multiple programming languages and platforms", "description": "Appwrite PHP library for generating API SDKs for multiple programming languages and platforms",
"support": { "support": {
"issues": "https://github.com/appwrite/sdk-generator/issues", "issues": "https://github.com/appwrite/sdk-generator/issues",
"source": "https://github.com/appwrite/sdk-generator/tree/0.17.0" "source": "https://github.com/appwrite/sdk-generator/tree/0.17.1"
}, },
"time": "2022-01-04T09:24:44+00:00" "time": "2022-01-07T12:55:37+00:00"
}, },
{ {
"name": "composer/pcre", "name": "composer/pcre",

View file

@ -128,13 +128,13 @@ class Func extends Model
*/ */
public function filter(Document $document): Document public function filter(Document $document): Document
{ {
$prefs = $document->getAttribute('vars'); $vars = $document->getAttribute('vars');
if($prefs instanceof Document) { if($vars instanceof Document) {
$prefs = $prefs->getArrayCopy(); $vars = $vars->getArrayCopy();
} }
if(is_array($prefs) && empty($prefs)) { if(is_array($vars) && empty($vars)) {
$document->setAttribute('vars', new stdClass); $document->setAttribute('vars', new stdClass());
} }
return $document; return $document;
} }