Tests
This commit is contained in:
parent
269266127d
commit
8e492a9797
5 changed files with 232 additions and 9 deletions
2
.env
2
.env
|
@ -25,6 +25,8 @@ _APP_DB_PASS=password
|
||||||
_APP_DB_ROOT_PASS=rootsecretpassword
|
_APP_DB_ROOT_PASS=rootsecretpassword
|
||||||
_APP_CONNECTIONS_DB_PROJECT=db_fra1_02=mysql://user:password@mariadb:3306/appwrite
|
_APP_CONNECTIONS_DB_PROJECT=db_fra1_02=mysql://user:password@mariadb:3306/appwrite
|
||||||
_APP_CONNECTIONS_DB_CONSOLE=db_fra1_01=mysql://user:password@mariadb:3306/appwrite
|
_APP_CONNECTIONS_DB_CONSOLE=db_fra1_01=mysql://user:password@mariadb:3306/appwrite
|
||||||
|
#_APP_CONNECTIONS_DB_PROJECT=db_fra1_02=mysql://doadmin:AVNS_sgtjg6ZSHBdg66422x-@db-mysql-fra1-shmuel-test-do-user-10204879-0.b.db.ondigitalocean.com:25060/console
|
||||||
|
#_APP_CONNECTIONS_DB_CONSOLE=db_fra1_01=mysql://doadmin:AVNS_sgtjg6ZSHBdg66422x-@db-mysql-fra1-shmuel-test-do-user-10204879-0.b.db.ondigitalocean.com:25060/projects
|
||||||
_APP_CONNECTIONS_CACHE=redis_fra1_01=redis://redis:6379
|
_APP_CONNECTIONS_CACHE=redis_fra1_01=redis://redis:6379
|
||||||
_APP_CONNECTIONS_QUEUE=redis_fra1_01=redis://redis:6379
|
_APP_CONNECTIONS_QUEUE=redis_fra1_01=redis://redis:6379
|
||||||
_APP_CONNECTIONS_PUBSUB=redis_fra1_01=redis://redis:6379
|
_APP_CONNECTIONS_PUBSUB=redis_fra1_01=redis://redis:6379
|
||||||
|
|
|
@ -1003,7 +1003,7 @@ $collections = [
|
||||||
'$id' => ID::custom('secret'),
|
'$id' => ID::custom('secret'),
|
||||||
'type' => Database::VAR_STRING,
|
'type' => Database::VAR_STRING,
|
||||||
'format' => '',
|
'format' => '',
|
||||||
'size' => 512, // var_dump of \bin2hex(\random_bytes(128)) => string(256) doubling for encryption
|
'size' => 512, // Output of \bin2hex(\random_bytes(128)) => string(256) doubling for encryption
|
||||||
'signed' => true,
|
'signed' => true,
|
||||||
'required' => true,
|
'required' => true,
|
||||||
'default' => null,
|
'default' => null,
|
||||||
|
|
14
app/http.php
14
app/http.php
|
@ -20,6 +20,7 @@ use Utopia\Database\Database;
|
||||||
use Utopia\Database\Document;
|
use Utopia\Database\Document;
|
||||||
use Utopia\Swoole\Files;
|
use Utopia\Swoole\Files;
|
||||||
use Appwrite\Utopia\Request;
|
use Appwrite\Utopia\Request;
|
||||||
|
use Swoole\Coroutine\Channel;
|
||||||
use Utopia\Logger\Log;
|
use Utopia\Logger\Log;
|
||||||
use Utopia\Logger\Log\User;
|
use Utopia\Logger\Log\User;
|
||||||
use Utopia\Pools\Group;
|
use Utopia\Pools\Group;
|
||||||
|
@ -59,9 +60,13 @@ include __DIR__ . '/controllers/general.php';
|
||||||
|
|
||||||
$http->on('start', function (Server $http) use ($payloadSize, $register) {
|
$http->on('start', function (Server $http) use ($payloadSize, $register) {
|
||||||
$app = new App('UTC');
|
$app = new App('UTC');
|
||||||
|
global $http;
|
||||||
|
|
||||||
|
var_dump('Test size is:'.$register->get('test')->length());
|
||||||
|
|
||||||
|
var_dump('Startup is served by:'.$http->worker_id);
|
||||||
go(function () use ($register, $app) {
|
go(function () use ($register, $app) {
|
||||||
|
var_dump('Test 2 size is:'.$register->get('test')->length());
|
||||||
$pools = $register->get('pools'); /** @var Group $pools */
|
$pools = $register->get('pools'); /** @var Group $pools */
|
||||||
App::setResource('pools', fn() => $pools);
|
App::setResource('pools', fn() => $pools);
|
||||||
|
|
||||||
|
@ -119,7 +124,7 @@ $http->on('start', function (Server $http) use ($payloadSize, $register) {
|
||||||
/**
|
/**
|
||||||
* Skip to prevent 0.16 migration issues.
|
* Skip to prevent 0.16 migration issues.
|
||||||
*/
|
*/
|
||||||
if (in_array($key, ['cache', 'variables']) && $dbForConsole->exists(App::getEnv('_APP_DB_SCHEMA', 'appwrite'), 'bucket_1')) {
|
if (in_array($key, ['cache', 'variables']) && $dbForConsole->exists($dbForConsole->getDefaultDatabase(), 'bucket_1')) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -155,7 +160,7 @@ $http->on('start', function (Server $http) use ($payloadSize, $register) {
|
||||||
$dbForConsole->createCollection($key, $attributes, $indexes);
|
$dbForConsole->createCollection($key, $attributes, $indexes);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($dbForConsole->getDocument('buckets', 'default')->isEmpty() && !$dbForConsole->exists(App::getEnv('_APP_DB_SCHEMA', 'appwrite'), 'bucket_1')) {
|
if ($dbForConsole->getDocument('buckets', 'default')->isEmpty() && !$dbForConsole->exists($dbForConsole->getDefaultDatabase(), 'bucket_1')) {
|
||||||
Console::success('[Setup] - Creating default bucket...');
|
Console::success('[Setup] - Creating default bucket...');
|
||||||
$dbForConsole->createDocument('buckets', new Document([
|
$dbForConsole->createDocument('buckets', new Document([
|
||||||
'$id' => ID::custom('default'),
|
'$id' => ID::custom('default'),
|
||||||
|
@ -233,6 +238,9 @@ $http->on('start', function (Server $http) use ($payloadSize, $register) {
|
||||||
$http->on('request', function (SwooleRequest $swooleRequest, SwooleResponse $swooleResponse) use ($register) {
|
$http->on('request', function (SwooleRequest $swooleRequest, SwooleResponse $swooleResponse) use ($register) {
|
||||||
$request = new Request($swooleRequest);
|
$request = new Request($swooleRequest);
|
||||||
$response = new Response($swooleResponse);
|
$response = new Response($swooleResponse);
|
||||||
|
var_dump('Test 3 size is:'.$register->get('test')->length());
|
||||||
|
global $http;
|
||||||
|
var_dump('Served by:'.$http->worker_id);
|
||||||
|
|
||||||
if (Files::isFileLoaded($request->getURI())) {
|
if (Files::isFileLoaded($request->getURI())) {
|
||||||
$time = (60 * 60 * 24 * 365 * 2); // 45 days cache
|
$time = (60 * 60 * 24 * 365 * 2); // 45 days cache
|
||||||
|
|
219
app/init.php
219
app/init.php
|
@ -1,5 +1,206 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
use Swoole\Coroutine\Channel;
|
||||||
|
use Utopia\Pools\Connection;
|
||||||
|
use Utopia\Pools\Pool as PoolsPool;
|
||||||
|
|
||||||
|
class Pool extends PoolsPool
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected string $name;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
|
protected int $size = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var callable
|
||||||
|
*/
|
||||||
|
protected $init = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
|
protected int $reconnectAttempts = 3;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
|
protected int $reconnectSleep = 1; // seconds
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var Channel
|
||||||
|
*/
|
||||||
|
public Channel $channel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected array $active = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string $name
|
||||||
|
* @var int $size
|
||||||
|
* @var callable $init
|
||||||
|
*/
|
||||||
|
public function __construct(string $name, int $size, callable $init)
|
||||||
|
{
|
||||||
|
$this->name = $name;
|
||||||
|
$this->size = $size;
|
||||||
|
$this->init = $init;
|
||||||
|
$this->channel = new Channel($size);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getName(): string
|
||||||
|
{
|
||||||
|
return $this->name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function getSize(): int
|
||||||
|
{
|
||||||
|
return $this->size;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function getReconnectAttempts(): int
|
||||||
|
{
|
||||||
|
return $this->reconnectAttempts;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var int $reconnectAttempts
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
public function setReconnectAttempts(int $reconnectAttempts): self
|
||||||
|
{
|
||||||
|
$this->reconnectAttempts = $reconnectAttempts;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function getReconnectSleep(): int
|
||||||
|
{
|
||||||
|
return $this->reconnectSleep;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var int $reconnectSleep
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
public function setReconnectSleep(int $reconnectSleep): self
|
||||||
|
{
|
||||||
|
$this->reconnectSleep = $reconnectSleep;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
public function fill(): self
|
||||||
|
{
|
||||||
|
if(!$this->channel->isEmpty()) {
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
for ($i=0; $i < $this->size; $i++) {
|
||||||
|
$attempts = 0;
|
||||||
|
|
||||||
|
do {
|
||||||
|
try {
|
||||||
|
$attempts++;
|
||||||
|
$connection = new Connection(($this->init)());
|
||||||
|
break; // leave loop if successful
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
if ($attempts >= $this->getReconnectAttempts()) {
|
||||||
|
throw new \Exception('Failed to create connection: ' . $e->getMessage());
|
||||||
|
}
|
||||||
|
sleep($this->getReconnectSleep());
|
||||||
|
}
|
||||||
|
} while ($attempts < $this->getReconnectAttempts());
|
||||||
|
|
||||||
|
$connection->setID($this->getName().'-'.$i);
|
||||||
|
|
||||||
|
$this->channel->push($connection);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Connection
|
||||||
|
*/
|
||||||
|
public function pop(): Connection
|
||||||
|
{
|
||||||
|
if ($this->channel->isEmpty()) {
|
||||||
|
throw new Exception('Pool is empty');
|
||||||
|
}
|
||||||
|
|
||||||
|
$connection = $this->channel->pop();
|
||||||
|
$this->active[$connection->getID()] = $connection;
|
||||||
|
|
||||||
|
return $connection;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Connection $connection
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
public function push(Connection $connection): self
|
||||||
|
{
|
||||||
|
$this->channel->push($connection);
|
||||||
|
unset($this->active[$connection->getID()]);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function count(): int
|
||||||
|
{
|
||||||
|
return $this->channel->length();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
public function reclaim(): self
|
||||||
|
{
|
||||||
|
foreach ($this->active as $connection) {
|
||||||
|
$this->push($connection);
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function isEmpty(): bool
|
||||||
|
{
|
||||||
|
return $this->channel->isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function isFull(): bool
|
||||||
|
{
|
||||||
|
return $this->channel->isFull();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Init
|
* Init
|
||||||
*
|
*
|
||||||
|
@ -69,7 +270,6 @@ use Utopia\CLI\Console;
|
||||||
use Utopia\Database\Adapter\MariaDB;
|
use Utopia\Database\Adapter\MariaDB;
|
||||||
use Utopia\Database\Adapter\MySQL;
|
use Utopia\Database\Adapter\MySQL;
|
||||||
use Utopia\Pools\Group;
|
use Utopia\Pools\Group;
|
||||||
use Utopia\Pools\Pool;
|
|
||||||
use Ahc\Jwt\JWT;
|
use Ahc\Jwt\JWT;
|
||||||
use Ahc\Jwt\JWTException;
|
use Ahc\Jwt\JWTException;
|
||||||
use MaxMind\Db\Reader;
|
use MaxMind\Db\Reader;
|
||||||
|
@ -498,6 +698,7 @@ $register->set('logger', function () {
|
||||||
return new Logger($adapter);
|
return new Logger($adapter);
|
||||||
});
|
});
|
||||||
$register->set('pools', function () {
|
$register->set('pools', function () {
|
||||||
|
Console::info('Initializing pools');
|
||||||
|
|
||||||
$group = new Group();
|
$group = new Group();
|
||||||
|
|
||||||
|
@ -622,6 +823,9 @@ $register->set('pools', function () {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
$pool = new Pool($name, 64, function () use ($type, $resource, $dsn) {
|
$pool = new Pool($name, 64, function () use ($type, $resource, $dsn) {
|
||||||
// Get Adapter
|
// Get Adapter
|
||||||
$adapter = null;
|
$adapter = null;
|
||||||
|
@ -634,7 +838,6 @@ $register->set('pools', function () {
|
||||||
default => null
|
default => null
|
||||||
};
|
};
|
||||||
|
|
||||||
var_dump($dsn->getDatabase());
|
|
||||||
$adapter->setDefaultDatabase($dsn->getDatabase());
|
$adapter->setDefaultDatabase($dsn->getDatabase());
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
@ -658,7 +861,7 @@ $register->set('pools', function () {
|
||||||
|
|
||||||
return $adapter;
|
return $adapter;
|
||||||
});
|
});
|
||||||
|
var_dump($pool->channel->length());
|
||||||
$group->add($pool);
|
$group->add($pool);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -667,12 +870,22 @@ $register->set('pools', function () {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$group->fill();
|
$group->fill();
|
||||||
|
Console::success('Pools have been filled succefully');
|
||||||
} catch (\Throwable $th) {
|
} catch (\Throwable $th) {
|
||||||
Console::error('Connection failure: ' . $th->getMessage());
|
Console::error('Connection failure: ' . $th->getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
return $group;
|
return $group;
|
||||||
});
|
});
|
||||||
|
$register->set('test', function () {
|
||||||
|
var_dump('[[init test!!]]');
|
||||||
|
$test = new Channel(5);
|
||||||
|
$test->push(1);
|
||||||
|
$test->push(1);
|
||||||
|
$test->push(1);
|
||||||
|
$test->push(1);
|
||||||
|
return $test;
|
||||||
|
});
|
||||||
$register->set('influxdb', function () {
|
$register->set('influxdb', function () {
|
||||||
// Register DB connection
|
// Register DB connection
|
||||||
$host = App::getEnv('_APP_INFLUXDB_HOST', '');
|
$host = App::getEnv('_APP_INFLUXDB_HOST', '');
|
||||||
|
|
|
@ -206,7 +206,7 @@ $cli
|
||||||
unset($models[$key]);
|
unset($models[$key]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// var_dump($models);
|
|
||||||
$arguments = [new App('UTC'), $services, $routes, $models, $keys[$platform], $authCounts[$platform] ?? 0];
|
$arguments = [new App('UTC'), $services, $routes, $models, $keys[$platform], $authCounts[$platform] ?? 0];
|
||||||
foreach (['swagger2', 'open-api3'] as $format) {
|
foreach (['swagger2', 'open-api3'] as $format) {
|
||||||
$formatInstance = match ($format) {
|
$formatInstance = match ($format) {
|
||||||
|
|
Loading…
Reference in a new issue