Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Failing errors #4697

Merged
merged 19 commits into from
Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ _APP_WORKER_PER_CORE=6
_APP_CONSOLE_WHITELIST_ROOT=disabled
_APP_CONSOLE_WHITELIST_EMAILS=
_APP_CONSOLE_WHITELIST_IPS=
_APP_CONSOLE_INVITES=enabled
_APP_SYSTEM_EMAIL_NAME=Appwrite
_APP_SYSTEM_EMAIL_ADDRESS=team@appwrite.io
_APP_SYSTEM_SECURITY_EMAIL_ADDRESS=security@appwrite.io
Expand Down Expand Up @@ -62,7 +63,7 @@ _APP_MAINTENANCE_RETENTION_EXECUTION=1209600
_APP_MAINTENANCE_RETENTION_ABUSE=86400
_APP_MAINTENANCE_RETENTION_AUDIT=1209600
_APP_MAINTENANCE_RETENTION_SCHEDULES=86400
_APP_USAGE_TIMESERIES_INTERVAL=60
_APP_USAGE_TIMESERIES_INTERVAL=20
christyjacob4 marked this conversation as resolved.
Show resolved Hide resolved
_APP_MAINTENANCE_RETENTION_USAGE_HOURLY=8640000
_APP_USAGE_STATS=enabled
_APP_LOGGING_PROVIDER=
Expand Down
41 changes: 25 additions & 16 deletions app/cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@
$databaseName = $project->getAttribute('database');

if (isset($databases[$databaseName])) {
return $databases[$databaseName];
$database = $databases[$databaseName];
$database->setNamespace('_' . $project->getInternalId());
return $database;
}

$dbAdapter = $pools
Expand All @@ -76,10 +78,11 @@
->getResource();

$database = new Database($dbAdapter, $cache);
$database->setNamespace('_' . $project->getInternalId());

$databases[$databaseName] = $database;

$database->setNamespace('_' . $project->getInternalId());

return $database;
};

Expand Down Expand Up @@ -176,25 +179,31 @@
do {
$attempts++;

// Prepare database connection
$dbAdapter = $pools
->get('console')
->pop()
->getResource();
try {
$pools->get('console')->reclaim();

// Prepare database connection
$dbAdapter = $pools
->get('console')
->pop()
->getResource();

$dbForConsole = new Database($dbAdapter, $cache);
$dbForConsole->setNamespace('console');
$dbForConsole = new Database($dbAdapter, $cache);
$dbForConsole->setNamespace('console');

// Ensure tables exist
$collections = Config::getParam('collections', []);
$last = \array_key_last($collections);
// Ensure tables exist
$collections = Config::getParam('collections', []);
$last = \array_key_last($collections);

if (!($dbForConsole->exists($dbForConsole->getDefaultDatabase(), $last))) {
throw new Exception('Tables not ready yet.');
}

if ($dbForConsole->exists($dbForConsole->getDefaultDatabase(), $last)) {
$ready = true;
break;
} catch (\Exception $err) {
Console::warning($err->getMessage());
sleep($sleep);
}

sleep($sleep);
} while ($attempts < $maxAttempts);

if (!$ready) {
Expand Down
11 changes: 10 additions & 1 deletion app/config/variables.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,16 @@
'required' => false,
'question' => '',
'filter' => ''
]
],
[
'name' => '_APP_CONSOLE_INVITES',
'description' => 'This option allows you to disable the invitation of new users to the Appwrite console. When enabled, console users are allowed to invite new users to a project. By default this option is enabled.',
'introduction' => '1.2.0',
'default' => 'enabled',
'required' => false,
'question' => '',
'filter' => ''
],
],
],
[
Expand Down
1 change: 1 addition & 0 deletions app/controllers/api/account.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@
->action(function (string $provider, string $success, string $failure, array $scopes, Request $request, Response $response, Document $project) use ($oauthDefaultSuccess, $oauthDefaultFailure) {

$protocol = $request->getProtocol();

$callback = $protocol . '://' . $request->getHostname() . '/v1/account/sessions/oauth2/callback/' . $provider . '/' . $project->getId();
$appId = $project->getAttribute('authProviders', [])[$provider . 'Appid'] ?? '';
$appSecret = $project->getAttribute('authProviders', [])[$provider . 'Secret'] ?? '{}';
Expand Down
9 changes: 6 additions & 3 deletions app/controllers/api/health.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Utopia\Config\Config;
use Utopia\Database\Document;
use Utopia\Pools\Group;
use Utopia\Queue\Client;
use Utopia\Queue\Connection;
use Utopia\Registry\Registry;
use Utopia\Storage\Device;
use Utopia\Storage\Device\Local;
Expand Down Expand Up @@ -396,10 +398,11 @@
->label('sdk.response.code', Response::STATUS_CODE_OK)
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
->label('sdk.response.model', Response::MODEL_HEALTH_QUEUE)
->inject('queueConnection')
christyjacob4 marked this conversation as resolved.
Show resolved Hide resolved
->inject('response')
->action(function (Response $response) {

$response->dynamic(new Document([ 'size' => Resque::size(Event::FUNCTIONS_QUEUE_NAME) ]), Response::MODEL_HEALTH_QUEUE);
->action(function (Connection $queueConnection, Response $response) {
$client = new Client(Event::FUNCTIONS_QUEUE_NAME, $queueConnection);
$response->dynamic(new Document([ 'size' => $client->sumProcessingJobs() ]), Response::MODEL_HEALTH_QUEUE);
}, ['response']);

App::get('/v1/health/storage/local')
Expand Down
1 change: 0 additions & 1 deletion app/controllers/api/project.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@
'executions' => $stats[$metrics[6]] ?? [],
'buckets' => $stats[$metrics[7]] ?? [],
]);

}

$response->dynamic($usage, Response::MODEL_USAGE_PROJECT);
Expand Down
10 changes: 7 additions & 3 deletions app/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
use Swoole\Database\PDOProxy;
use Utopia\CLI\Console;
use Utopia\Queue;
use Utopia\Queue\Connection;
use Utopia\Storage\Storage;

const APP_NAME = 'Appwrite';
Expand Down Expand Up @@ -847,9 +848,12 @@ function (mixed $value) {
App::setResource('deletes', fn() => new Delete());
App::setResource('database', fn() => new EventDatabase());
App::setResource('messaging', fn() => new Phone());
App::setResource('queueForFunctions', function (Group $pools) {
return new Func($pools->get('queue')->pop()->getResource());
App::setResource('queue', function (Group $pools) {
return $pools->get('queue')->pop()->getResource();
}, ['pools']);
App::setResource('queueForFunctions', function (Connection $queue) {
return new Func($queue);
}, ['queue']);
App::setResource('usage', function ($register) {
return new Stats($register->get('statsd'));
}, ['register']);
Expand Down Expand Up @@ -1024,7 +1028,7 @@ function (mixed $value) {
'legalAddress' => '',
'legalTaxId' => '',
'auths' => [
'invites' => false,
'invites' => App::getEnv('_APP_CONSOLE_INVITES', 'enabled') === 'enabled',
'limit' => (App::getEnv('_APP_CONSOLE_WHITELIST_ROOT', 'enabled') === 'enabled') ? 1 : 0, // limit signup to 1 user
'duration' => Auth::TOKEN_EXPIRATION_LOGIN_LONG, // 1 Year in seconds
],
Expand Down
1 change: 1 addition & 0 deletions app/views/install/compose.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ services:
- _APP_CONSOLE_WHITELIST_ROOT
- _APP_CONSOLE_WHITELIST_EMAILS
- _APP_CONSOLE_WHITELIST_IPS
- _APP_CONSOLE_INVITES
- _APP_SYSTEM_EMAIL_NAME
- _APP_SYSTEM_EMAIL_ADDRESS
- _APP_SYSTEM_SECURITY_EMAIL_ADDRESS
Expand Down
13 changes: 12 additions & 1 deletion app/worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Utopia\Registry\Registry;
use Utopia\Logger\Log;
use Utopia\Logger\Logger;
use Utopia\Pools\Group;

Runtime::enableCoroutine(SWOOLE_HOOK_ALL);

Expand Down Expand Up @@ -55,7 +56,6 @@

$adapter = new Database($database, $cache);
$adapter->setNamespace('_' . $project->getInternalId());

return $adapter;
}, ['cache', 'register', 'message', 'dbForConsole']);

Expand Down Expand Up @@ -93,6 +93,10 @@
return $register->get('statsd');
}, ['register']);

Server::setResource('pools', function ($register) {
return $register->get('pools');
}, ['register']);

$pools = $register->get('pools');
$connection = $pools->get('queue')->pop()->getResource();
$workerNumber = swoole_cpu_num() * intval(App::getEnv('_APP_WORKER_PER_CORE', 6));
Expand All @@ -104,6 +108,13 @@
$adapter = new Swoole($connection, $workerNumber, App::getEnv('QUEUE'));
$server = new Server($adapter);

$server
->shutdown()
->inject('pools')
->action(function (Group $pools) {
$pools->reclaim();
});

$server
->error()
->inject('error')
Expand Down
25 changes: 12 additions & 13 deletions app/workers/deletes.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,38 +307,37 @@ protected function deleteUser(Document $document, Document $project): void
{
$userId = $document->getId();

$dbForProject = $this->getProjectDB($project);

// Delete all sessions of this user from the sessions table and update the sessions field of the user record
$this->deleteByGroup('sessions', [
Query::equal('userId', [$userId])
], $this->getProjectDB($project));
], $dbForProject);

$this->getProjectDB($project)->deleteCachedDocument('users', $userId);
$dbForProject->deleteCachedDocument('users', $userId);

// Delete Memberships and decrement team membership counts
$this->deleteByGroup('memberships', [
Query::equal('userId', [$userId])
], $this->getProjectDB($project), function (Document $document) use ($project) {

], $dbForProject, function (Document $document) use ($dbForProject) {
if ($document->getAttribute('confirm')) { // Count only confirmed members
$teamId = $document->getAttribute('teamId');
$team = $this->getProjectDB($project)->getDocument('teams', $teamId);
$team = $dbForProject->getDocument('teams', $teamId);
if (!$team->isEmpty()) {
$team = $this
->getProjectDB($project)
->updateDocument(
'teams',
$teamId,
// Ensure that total >= 0
$team = $dbForProject->updateDocument(
'teams',
$teamId,
// Ensure that total >= 0
$team->setAttribute('total', \max($team->getAttribute('total', 0) - 1, 0))
);
);
}
}
});

// Delete tokens
$this->deleteByGroup('tokens', [
Query::equal('userId', [$userId])
], $this->getProjectDB($project));
], $dbForProject);
}

/**
Expand Down
26 changes: 15 additions & 11 deletions app/workers/messaging.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
<?php

use Appwrite\SMS\Adapter\Mock;
use Appwrite\SMS\Adapter\Telesign;
use Appwrite\SMS\Adapter\TextMagic;
use Appwrite\SMS\Adapter\Twilio;
use Appwrite\SMS\Adapter\Msg91;
use Appwrite\SMS\Adapter\Vonage;
use Utopia\DSN\DSN;
use Appwrite\Resque\Worker;
use Appwrite\SMS\Adapter;
use Utopia\App;
use Utopia\CLI\Console;
use Utopia\DSN\DSN;
use Utopia\Messaging\Adapter;
use Utopia\Messaging\Adapters\SMS\Mock;
use Utopia\Messaging\Adapters\SMS\Msg91;
use Utopia\Messaging\Adapters\SMS\Telesign;
use Utopia\Messaging\Adapters\SMS\TextMagic;
use Utopia\Messaging\Adapters\SMS\Twilio;
use Utopia\Messaging\Adapters\SMS\Vonage;
use Utopia\Messaging\Messages\SMS;

require_once __DIR__ . '/../init.php';

Expand Down Expand Up @@ -58,11 +59,14 @@ public function run(): void
return;
}

$recipient = $this->args['recipient'];
$message = $this->args['message'];
$message = new SMS(
to: [$this->args['recipient']],
content: $this->args['message'],
from: $this->from,
);

try {
$this->sms->send($this->from, $recipient, $message);
$this->sms->send($message);
} catch (\Exception $error) {
throw new Exception('Error sending message: ' . $error->getMessage(), 500);
}
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"utopia-php/cli": "0.14.*",
"utopia-php/config": "0.2.*",
"utopia-php/database": "0.28.*",
"utopia-php/queue": "0.4.*",
"utopia-php/queue": "0.5.*",
"utopia-php/orchestration": "0.9.*",
"utopia-php/platform": "0.3.*",
"utopia-php/pools": "0.4.*",
Expand Down
Loading