From d238d79e2924590f7a638c4112dd1d64131f137c Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 11 Oct 2023 21:28:29 +1300 Subject: [PATCH] Allow multiprocess for db queue --- .env | 1 + app/workers/databases.php | 28 ---------------------------- bin/worker-databases | 16 +++++++++++++++- docker-compose.yml | 1 + src/Appwrite/Event/Database.php | 11 ++++++++++- 5 files changed, 27 insertions(+), 30 deletions(-) diff --git a/.env b/.env index b3a57baae5..4777d7fdcb 100644 --- a/.env +++ b/.env @@ -5,6 +5,7 @@ _APP_CONSOLE_WHITELIST_ROOT=disabled _APP_CONSOLE_WHITELIST_EMAILS= _APP_CONSOLE_WHITELIST_IPS= _APP_CONNECTIONS_QUEUE_PER_WORKER=enabled +_APP_CONNECTIONS_DB_QUEUES=v1-database-0,v1-database-1,v1-database-2 _APP_SYSTEM_EMAIL_NAME=Appwrite _APP_SYSTEM_EMAIL_ADDRESS=team@appwrite.io _APP_SYSTEM_SECURITY_EMAIL_ADDRESS=security@appwrite.io diff --git a/app/workers/databases.php b/app/workers/databases.php index 46a7074623..f0c90ccfe4 100644 --- a/app/workers/databases.php +++ b/app/workers/databases.php @@ -15,33 +15,10 @@ require_once __DIR__ . '/../init.php'; Console::title('Database V1 Worker'); Console::success(APP_NAME . ' database worker v1 has started' . "\n"); -$table = new Swoole\Table(1); -$table->column('workerCount', Swoole\Table::TYPE_INT); -$table->create(); -$table->set('databases', ['workerCount' => 0]); - -$lock = new Swoole\Lock(SWOOLE_MUTEX); - class DatabaseV1 extends Worker { public function init(): void { - global $table, $lock; - - $dbQueues = App::getEnv('_APP_CONNECTIONS_QUEUE_PER_WORKER', 'disabled'); - - if ($dbQueues !== 'enabled') { - $queue = 'v1-database'; - } else { - $project = new Document($this->args['project']); - $queue = $project->getAttribute('database'); - } - - \putenv('QUEUE=' . $queue); - - $lock->lock(); - $table->incr('databases', 'workerCount'); - $lock->unlock(); } public function run(): void @@ -82,11 +59,6 @@ class DatabaseV1 extends Worker public function shutdown(): void { - global $table, $lock; - - $lock->lock(); - $table->decr('databases', 'workerCount'); - $lock->unlock(); } /** diff --git a/bin/worker-databases b/bin/worker-databases index 4f46e8f40b..6f27d3d13e 100644 --- a/bin/worker-databases +++ b/bin/worker-databases @@ -7,4 +7,18 @@ else REDIS_BACKEND="redis://${_APP_REDIS_USER}:${_APP_REDIS_PASS}@${_APP_REDIS_HOST}:${_APP_REDIS_PORT}" fi -INTERVAL=0.1 APP_INCLUDE='/usr/src/code/app/workers/databases.php' php /usr/src/code/vendor/bin/resque -dopcache.preload=opcache.preload=/usr/src/code/app/preload.php +queues="${_APP_CONNECTIONS_DB_QUEUES}" +if [ -z "${queues}" ]; then + queues="v1-databases" +fi + +count=1 +if [ "${_APP_CONNECTIONS_QUEUE_PER_WORKER}" = "enabled" ]; then + count=$(echo "${queues}" | tr ',' '\n' | wc -l) +fi + +INTERVAL=0.1 \ + QUEUE="${queues}" \ + COUNT=${count} \ + APP_INCLUDE='/usr/src/code/app/workers/databases.php' \ + php /usr/src/code/vendor/bin/resque -dopcache.preload=opcache.preload=/usr/src/code/app/preload.php diff --git a/docker-compose.yml b/docker-compose.yml index b41237f5d8..e231f4d7d2 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -366,6 +366,7 @@ services: - mariadb environment: - _APP_ENV + - _APP_CONNECTIONS_QUEUE_PER_WORKER - _APP_CONNECTIONS_DB_QUEUES - _APP_WORKER_PER_CORE - _APP_OPENSSL_KEY_V1 diff --git a/src/Appwrite/Event/Database.php b/src/Appwrite/Event/Database.php index 1822f06c71..f36688eb01 100644 --- a/src/Appwrite/Event/Database.php +++ b/src/Appwrite/Event/Database.php @@ -3,6 +3,7 @@ namespace Appwrite\Event; use Resque; +use Utopia\App; use Utopia\Database\Document; class Database extends Event @@ -14,7 +15,15 @@ class Database extends Event public function __construct() { - parent::__construct(Event::DATABASE_QUEUE_NAME, Event::DATABASE_CLASS_NAME); + $dbQueues = App::getEnv('_APP_CONNECTIONS_DB_QUEUES'); + + if (empty($dbQueues)) { + $queue = Event::DATABASE_QUEUE_NAME; + } else { + $queue = $this->getProject()->getAttribute('database'); + } + + parent::__construct($queue, Event::DATABASE_CLASS_NAME); } /**