1
0
Fork 0
mirror of synced 2024-06-14 00:34:51 +12:00

addressing some comments

This commit is contained in:
shimon 2023-10-17 06:44:36 +03:00
parent 0e2cf14e12
commit 9a6d88a819
12 changed files with 45 additions and 106 deletions

2
.env
View file

@ -98,4 +98,4 @@ _APP_VCS_GITHUB_CLIENT_SECRET=
_APP_VCS_GITHUB_WEBHOOK_SECRET=
_APP_MIGRATIONS_FIREBASE_CLIENT_ID=
_APP_MIGRATIONS_FIREBASE_CLIENT_SECRET=
_APP_ASSISTANT_OPENAI_API_KEY=
_APP_ASSISTANT_OPENAI_API_KEY=

View file

@ -24,8 +24,6 @@ use Utopia\Registry\Registry;
Authorization::disable();
global $register;
CLI::setResource('register', fn()=>$register);
CLI::setResource('cache', function ($pools) {
@ -146,8 +144,8 @@ CLI::setResource('influxdb', function (Registry $register) {
CLI::setResource('queue', function (Group $pools) {
return $pools->get('queue')->pop()->getResource();
}, ['pools']);
CLI::setResource('queueForFunctions', function (Group $pools) {
return new Func($pools->get('queue')->pop()->getResource());
CLI::setResource('queueForFunctions', function (Connection $queue) {
return new Func($queue);
}, ['pools']);
CLI::setResource('queueForDeletes', function (Connection $queue) {
return new Delete($queue);

View file

@ -32,12 +32,10 @@ use Utopia\Logger\Log;
use Utopia\Logger\Logger;
use Utopia\Pools\Group;
use Utopia\Queue\Connection;
use Utopia\Storage\Device;
Authorization::disable();
Runtime::enableCoroutine(SWOOLE_HOOK_ALL);
global $register;
Server::setResource('register', fn () => $register);
@ -118,6 +116,10 @@ Server::setResource('cache', function (Registry $register) {
return new Cache(new Sharding($adapters));
}, ['register']);
Server::setResource('log', fn() => new Log());
Server::setResource('usage', function ($register) {
return new Stats($register->get('statsd'));
}, ['register']);
Server::setResource('queue', function (Group $pools) {
return $pools->get('queue')->pop()->getResource();
}, ['pools']);
@ -148,60 +150,30 @@ Server::setResource('queueForFunctions', function (Connection $queue) {
Server::setResource('queueForCertificates', function (Connection $queue) {
return new Certificate($queue);
}, ['queue']);
Server::setResource('usage', function ($register) {
return new Stats($register->get('statsd'));
}, ['register']);
Server::setResource('queueForMigrations', function (Connection $queue) {
return new Migration($queue);
}, ['queue']);
Server::setResource('logger', function (Registry $register) {
return $register->get('logger');
}, ['register']);
Server::setResource('pools', function (Registry $register) {
return $register->get('pools');
}, ['register']);
Server::setResource('log', fn() => new Log());
/**
* Get Functions Storage Device
* @param string $projectId of the project
* @return Device
*/
Server::setResource('getFunctionsDevice', function () {
return function (string $projectId) {
return getDevice(APP_STORAGE_FUNCTIONS . '/app-' . $projectId);
};
});
/**
* Get Files Storage Device
* @param string $projectId of the project
* @return Device
*/
Server::setResource('getFilesDevice', function () {
return function (string $projectId) {
return getDevice(APP_STORAGE_UPLOADS . '/app-' . $projectId);
};
});
/**
* Get Builds Storage Device
* @param string $projectId of the project
* @return Device
*/
Server::setResource('getBuildsDevice', function () {
return function (string $projectId) {
return getDevice(APP_STORAGE_BUILDS . '/app-' . $projectId);
};
});
/**
* Get cache Device
* @param string $projectId of the project
* @return Device
*/
Server::setResource('getCacheDevice', function () {
return function (string $projectId) {
return getDevice(APP_STORAGE_CACHE . '/app-' . $projectId);
@ -221,7 +193,7 @@ if (!isset($args[1])) {
$workerName = $args[0];
$workerIndex = $args[1] ?? '';
if (!empty($workerNum)) {
if (!empty($workerIndex)) {
$workerName .= '_' . $workerIndex;
}
@ -291,13 +263,9 @@ $worker
Console::error('[Error] Line: ' . $error->getLine());
});
try {
$workerStart = $worker->getWorkerStart();
} catch (\Throwable $error) {
$worker->workerStart()
->action(function () use ($workerName) {
Console::info("Worker $workerName started");
});
}
$worker->start();
$worker->start();

View file

@ -59,10 +59,10 @@
"utopia-php/messaging": "0.2.*",
"utopia-php/migration": "0.3.*",
"utopia-php/orchestration": "0.9.*",
"utopia-php/platform": "dev-integrate-workers as 0.3.3",
"utopia-php/platform": "0.5.*",
"utopia-php/pools": "0.4.*",
"utopia-php/preloader": "0.2.*",
"utopia-php/queue": "dev-feat-get-worker-start as 0.5.3",
"utopia-php/queue": "0.5.*",
"utopia-php/registry": "0.5.*",
"utopia-php/storage": "0.17.*",
"utopia-php/swoole": "0.5.*",

View file

@ -84,9 +84,6 @@ services:
- ./public:/usr/src/code/public
- ./src:/usr/src/code/src
- ./dev:/usr/local/dev
- ./vendor/utopia-php/platform:/usr/src/code/vendor/utopia-php/platform
- ./vendor/utopia-php/framework:/usr/src/code/vendor/utopia-php/framework
depends_on:
- mariadb
- redis
@ -252,10 +249,6 @@ services:
volumes:
- ./app:/usr/src/code/app
- ./src:/usr/src/code/src
- ./vendor/utopia-php/platform:/usr/src/code/vendor/utopia-php/platform
- ./vendor/utopia-php/queue:/usr/src/code/vendor/utopia-php/queue
- ./vendor/utopia-php/pools:/usr/src/code/vendor/utopia-php/pools
depends_on:
- redis
- mariadb
@ -285,7 +278,6 @@ services:
volumes:
- ./app:/usr/src/code/app
- ./src:/usr/src/code/src
- ./vendor/utopia-php/platform:/usr/src/code/vendor/utopia-php/platform
depends_on:
- redis
- mariadb
@ -320,8 +312,6 @@ services:
- appwrite-certificates:/storage/certificates:rw
- ./app:/usr/src/code/app
- ./src:/usr/src/code/src
- ./vendor/utopia-php/storage:/usr/src/code/vendor/utopia-php/storage
- ./vendor/utopia-php/platform:/usr/src/code/vendor/utopia-php/platform
environment:
- _APP_ENV
- _APP_WORKER_PER_CORE
@ -370,9 +360,7 @@ services:
- appwrite
volumes:
- ./app:/usr/src/code/app
- ./src:/usr/src/code/src
- ./vendor/utopia-php/database:/usr/src/code/vendor/utopia-php/database
- ./vendor/utopia-php/platform:/usr/src/code/vendor/utopia-php/platform
- ./src:/usr/src/code/srcrm
depends_on:
- redis
- mariadb
@ -406,8 +394,6 @@ services:
- appwrite-builds:/storage/builds:rw
- ./app:/usr/src/code/app
- ./src:/usr/src/code/src
- ./vendor/utopia-php/platform:/usr/src/code/vendor/utopia-php/platform
- ./vendor/utopia-php/queue:/usr/src/code/vendor/utopia-php/queue
depends_on:
- redis
- mariadb
@ -637,8 +623,6 @@ services:
volumes:
- ./app:/usr/src/code/app
- ./src:/usr/src/code/src
- ./vendor/utopia-php/pools:/usr/src/code/vendor/utopia-php/pools
depends_on:
- redis
environment:

View file

@ -18,6 +18,7 @@ class Database extends Event
{
parent::__construct($connection);
$this->setQueue($this->getProject()->getAttribute('database'));
$this->setClass(Event::DATABASE_CLASS_NAME);
}
@ -108,7 +109,6 @@ class Database extends Event
*/
public function trigger(): string|bool
{
$this->setQueue($this->getProject()->getAttribute('database'));
$client = new Client($this->queue, $this->connection);

View file

@ -87,22 +87,4 @@ class Migration extends Event
'migration' => $this->migration
]);
}
/**
* Schedules the migration event and schedules it in the migrations worker queue.
*
* @param \DateTime|int $at
* @return void
* @throws \Resque_Exception
* @throws \ResqueScheduler_InvalidTimestampException
*/
public function schedule(DateTime|int $at): void
{
return;
// ResqueScheduler::enqueueAt($at, $this->queue, $this->class, [
// 'project' => $this->project,
// 'user' => $this->user,
// 'migration' => $this->migration
// ]);
}
}

View file

@ -2,6 +2,7 @@
namespace Appwrite\Platform\Tasks;
use Appwrite\Extend\Exception;
use Cron\CronExpression;
use Swoole\Timer;
use Utopia\App;
@ -26,6 +27,9 @@ class Schedule extends Action
return 'schedule';
}
/**
* @throws Exception
*/
public function __construct()
{
$this
@ -34,6 +38,8 @@ class Schedule extends Action
->inject('dbForConsole')
->inject('getProjectDB')
->callback(fn (Group $pools, Database $dbForConsole, callable $getProjectDB) => $this->action($pools, $dbForConsole, $getProjectDB));
throw new Exception('error on Schedule cli');
}
/**

View file

@ -50,9 +50,9 @@ class Builds extends Action
->inject('queueForFunctions')
->inject('usage')
->inject('cache')
->inject('getProjectDB')
->inject('dbForProject')
->inject('getFunctionsDevice')
->callback(fn($message, Database $dbForConsole, Event $queueForEvents, Func $queueForFunctions, Stats $usage, Cache $cache, callable $getProjectDB, callable $getFunctionsDevice) => $this->action($message, $dbForConsole, $queueForEvents, $queueForFunctions, $usage, $cache, $getProjectDB, $getFunctionsDevice));
->callback(fn($message, Database $dbForConsole, Event $queueForEvents, Func $queueForFunctions, Stats $usage, Cache $cache, Database $dbForProject, callable $getFunctionsDevice) => $this->action($message, $dbForConsole, $queueForEvents, $queueForFunctions, $usage, $cache, $dbForProject, $getFunctionsDevice));
}
/**
@ -62,12 +62,12 @@ class Builds extends Action
* @param Func $queueForFunctions
* @param Stats $usage
* @param Cache $cache
* @param callable $getProjectDB
* @param Database $dbForProject
* @param callable $getFunctionsDevice
* @return void
* @throws \Utopia\Database\Exception
*/
public function action(Message $message, Database $dbForConsole, Event $queueForEvents, Func $queueForFunctions, Stats $usage, Cache $cache, callable $getProjectDB, callable $getFunctionsDevice): void
public function action(Message $message, Database $dbForConsole, Event $queueForEvents, Func $queueForFunctions, Stats $usage, Cache $cache, Database $dbForProject, callable $getFunctionsDevice): void
{
$payload = $message->getPayload() ?? [];
@ -86,7 +86,7 @@ class Builds extends Action
case BUILD_TYPE_RETRY:
Console::info('Creating build for deployment: ' . $deployment->getId());
$github = new GitHub($cache);
$this->buildDeployment($getFunctionsDevice, $queueForFunctions, $queueForEvents, $usage, $dbForConsole, $getProjectDB, $github, $project, $resource, $deployment, $template);
$this->buildDeployment($getFunctionsDevice, $queueForFunctions, $queueForEvents, $usage, $dbForConsole, $dbForProject, $github, $project, $resource, $deployment, $template);
break;
default:
@ -100,7 +100,7 @@ class Builds extends Action
* @param Event $queueForEvents
* @param Stats $usage
* @param Database $dbForConsole
* @param callable $getProjectDB
* @param Database $dbForProject
* @param GitHub $github
* @param Document $project
* @param Document $function
@ -110,12 +110,10 @@ class Builds extends Action
* @throws \Utopia\Database\Exception
* @throws Exception
*/
protected function buildDeployment(callable $getFunctionsDevice, Func $queueForFunctions, Event $queueForEvents, Stats $usage, Database $dbForConsole, callable $getProjectDB, GitHub $github, Document $project, Document $function, Document $deployment, Document $template): void
protected function buildDeployment(callable $getFunctionsDevice, Func $queueForFunctions, Event $queueForEvents, Stats $usage, Database $dbForConsole, Database $dbForProject, GitHub $github, Document $project, Document $function, Document $deployment, Document $template): void
{
$executor = new Executor(App::getEnv('_APP_EXECUTOR_HOST'));
$dbForProject = $getProjectDB($project);
$function = $dbForProject->getDocument('functions', $function->getId());
if ($function->isEmpty()) {
throw new Exception('Function not found', 404);

View file

@ -136,7 +136,7 @@ class Certificates extends Action
// Validate domain and DNS records. Skip if job is forced
if (!$skipRenewCheck) {
$mainDomain = $this->getMainDomain($dbForConsole);
$mainDomain = $this->getMainDomain();
$isMainDomain = !isset($mainDomain) || $domain->get() === $mainDomain;
$this->validateDomain($domain, $isMainDomain);
}
@ -225,7 +225,7 @@ class Certificates extends Action
*
* @return null|string Returns main domain. If null, there is no main domain yet.
*/
private function getMainDomain(Database $dbForConsole): ?string
private function getMainDomain(): ?string
{
$envDomain = App::getEnv('_APP_DOMAIN', '');
if (!empty($envDomain) && $envDomain !== 'localhost') {
@ -498,6 +498,10 @@ class Certificates extends Action
/** Trigger Functions */
$queueForFunctions
->setProject($project)
->setEvent('rules.[ruleId].update')
->setParam('ruleId', $rule->getId())
->setPayload($rule->getArrayCopy(array_keys($ruleModel->getRules())))
->trigger();
/** Trigger realtime event */

View file

@ -4,7 +4,6 @@ namespace Appwrite\Platform\Workers;
use Appwrite\Event\Event;
use Appwrite\Messaging\Adapter\Realtime;
use Exception;
use Utopia\CLI\Console;
use Utopia\Database\Database;
use Utopia\Database\Document;
@ -23,7 +22,7 @@ class Databases extends Action
}
/**
* @throws Exception
* @throws \Exception
*/
public function __construct()
{
@ -40,14 +39,14 @@ class Databases extends Action
* @param Database $dbForConsole
* @param Database $dbForProject
* @return void
* @throws Exception
* @throws \Exception
*/
public function action(Message $message, Database $dbForConsole, Database $dbForProject): void
{
$payload = $message->getPayload() ?? [];
if (empty($payload)) {
throw new Exception('Missing payload');
throw new \Exception('Missing payload');
}
$type = $payload['type'];
@ -57,11 +56,11 @@ class Databases extends Action
$database = new Document($payload['database'] ?? []);
if ($collection->isEmpty()) {
throw new Exception('Missing collection');
throw new \Exception('Missing collection');
}
if ($document->isEmpty()) {
throw new Exception('Missing document');
throw new \Exception('Missing document');
}
match (strval($type)) {
@ -83,7 +82,7 @@ class Databases extends Action
* @return void
* @throws Authorization
* @throws Conflict
* @throws Exception
* @throws \Exception
*/
private function createAttribute(Document $database, Document $collection, Document $attribute, Document $project, Database $dbForConsole, Database $dbForProject): void
{
@ -144,7 +143,7 @@ class Databases extends Action
break;
default:
if (!$dbForProject->createAttribute('database_' . $database->getInternalId() . '_collection_' . $collection->getInternalId(), $key, $type, $size, $required, $default, $signed, $array, $format, $formatOptions, $filters)) {
throw new Exception('Failed to create Attribute');
throw new \Exception('Failed to create Attribute');
}
}
@ -211,7 +210,7 @@ class Databases extends Action
* @return void
* @throws Authorization
* @throws Conflict
* @throws Exception
* @throws \Exception
**/
private function deleteAttribute(Document $database, Document $collection, Document $attribute, Document $project, Database $dbForConsole, Database $dbForProject): void
{

View file

@ -44,19 +44,19 @@ class Migrations extends Action
$this
->desc('Migrations worker')
->inject('message')
->inject('getProjectDB')
->inject('dbForProject')
->inject('dbForConsole')
->callback(fn(Message $message, callable $getProjectDB, Database $dbForConsole) => $this->action($message, $getProjectDB, $dbForConsole));
->callback(fn(Message $message, Database $dbForProject, Database $dbForConsole) => $this->action($message, $dbForProject, $dbForConsole));
}
/**
* @param Message $message
* @param callable $getProjectDB
* @param Database $dbForProject
* @param Database $dbForConsole
* @return void
* @throws Exception
*/
public function action(Message $message, callable $getProjectDB, Database $dbForConsole): void
public function action(Message $message, Database $dbForProject, Database $dbForConsole): void
{
$payload = $message->getPayload() ?? [];
@ -72,7 +72,7 @@ class Migrations extends Action
return;
}
$this->dbForProject = $getProjectDB($project);
$this->dbForProject = $dbForProject;
$this->dbForConsole = $dbForConsole;
/**