2022-11-09 17:01:43 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
require_once __DIR__ . '/init.php';
|
|
|
|
|
|
2022-12-06 15:06:40 +00:00
|
|
|
use Appwrite\Event\Audit;
|
2022-12-20 12:22:58 +00:00
|
|
|
use Appwrite\Event\Build;
|
2022-12-13 11:16:12 +00:00
|
|
|
use Appwrite\Event\Certificate;
|
2022-12-13 15:54:01 +00:00
|
|
|
use Appwrite\Event\Database as EventDatabase;
|
2022-12-20 12:22:58 +00:00
|
|
|
use Appwrite\Event\Delete;
|
2024-03-06 17:34:21 +00:00
|
|
|
use Appwrite\Event\Event;
|
2022-11-15 18:13:17 +00:00
|
|
|
use Appwrite\Event\Func;
|
2022-12-20 12:22:58 +00:00
|
|
|
use Appwrite\Event\Mail;
|
2024-01-24 11:29:36 +00:00
|
|
|
use Appwrite\Event\Messaging;
|
2023-10-01 17:39:26 +00:00
|
|
|
use Appwrite\Event\Migration;
|
2023-10-25 07:39:59 +00:00
|
|
|
use Appwrite\Event\Usage;
|
2024-01-28 09:28:59 +00:00
|
|
|
use Appwrite\Event\UsageDump;
|
2023-05-29 13:58:45 +00:00
|
|
|
use Appwrite\Platform\Appwrite;
|
2022-11-09 17:01:43 +00:00
|
|
|
use Swoole\Runtime;
|
2024-05-03 09:30:38 +00:00
|
|
|
use Utopia\App;
|
2022-11-09 17:01:43 +00:00
|
|
|
use Utopia\Cache\Adapter\Sharding;
|
|
|
|
|
use Utopia\Cache\Cache;
|
2022-11-16 10:40:41 +00:00
|
|
|
use Utopia\CLI\Console;
|
2022-11-09 17:01:43 +00:00
|
|
|
use Utopia\Config\Config;
|
|
|
|
|
use Utopia\Database\Database;
|
2023-12-12 08:07:24 +00:00
|
|
|
use Utopia\Database\DateTime;
|
2022-11-12 14:35:42 +00:00
|
|
|
use Utopia\Database\Document;
|
2023-05-29 13:58:45 +00:00
|
|
|
use Utopia\Database\Validator\Authorization;
|
2024-05-06 05:33:36 +00:00
|
|
|
use Utopia\DSN\DSN;
|
2022-11-16 10:40:41 +00:00
|
|
|
use Utopia\Logger\Log;
|
2022-11-16 13:34:11 +00:00
|
|
|
use Utopia\Logger\Logger;
|
2024-03-06 17:34:21 +00:00
|
|
|
use Utopia\Platform\Service;
|
2022-11-16 19:39:35 +00:00
|
|
|
use Utopia\Pools\Group;
|
2022-12-20 12:22:58 +00:00
|
|
|
use Utopia\Queue\Connection;
|
2024-03-06 17:34:21 +00:00
|
|
|
use Utopia\Queue\Message;
|
|
|
|
|
use Utopia\Queue\Server;
|
|
|
|
|
use Utopia\Registry\Registry;
|
2024-04-01 11:02:47 +00:00
|
|
|
use Utopia\System\System;
|
2022-11-16 10:40:41 +00:00
|
|
|
|
2023-10-26 08:24:30 +00:00
|
|
|
Authorization::disable();
|
2022-11-16 10:40:41 +00:00
|
|
|
Runtime::enableCoroutine(SWOOLE_HOOK_ALL);
|
2022-11-09 17:01:43 +00:00
|
|
|
|
2022-12-20 11:11:36 +00:00
|
|
|
Server::setResource('register', fn () => $register);
|
2022-11-09 17:01:43 +00:00
|
|
|
|
|
|
|
|
Server::setResource('dbForConsole', function (Cache $cache, Registry $register) {
|
|
|
|
|
$pools = $register->get('pools');
|
2022-11-16 04:17:46 +00:00
|
|
|
$database = $pools
|
2022-11-09 17:01:43 +00:00
|
|
|
->get('console')
|
|
|
|
|
->pop()
|
2024-02-20 11:40:55 +00:00
|
|
|
->getResource();
|
2022-11-09 17:01:43 +00:00
|
|
|
|
2022-11-16 04:17:46 +00:00
|
|
|
$adapter = new Database($database, $cache);
|
2023-08-16 23:34:54 +00:00
|
|
|
$adapter->setNamespace('_console');
|
2022-11-09 17:01:43 +00:00
|
|
|
|
2022-11-16 04:17:46 +00:00
|
|
|
return $adapter;
|
2022-11-09 17:01:43 +00:00
|
|
|
}, ['cache', 'register']);
|
|
|
|
|
|
2024-02-20 11:40:55 +00:00
|
|
|
Server::setResource('project', function (Message $message, Database $dbForConsole) {
|
2022-11-16 04:17:46 +00:00
|
|
|
$payload = $message->getPayload() ?? [];
|
|
|
|
|
$project = new Document($payload['project'] ?? []);
|
2022-11-12 14:35:42 +00:00
|
|
|
|
2024-02-20 11:40:55 +00:00
|
|
|
if ($project->getId() === 'console') {
|
|
|
|
|
return $project;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $dbForConsole->getDocument('projects', $project->getId());
|
|
|
|
|
}, ['message', 'dbForConsole']);
|
|
|
|
|
|
|
|
|
|
Server::setResource('dbForProject', function (Cache $cache, Registry $register, Message $message, Document $project, Database $dbForConsole) {
|
2022-11-12 14:35:42 +00:00
|
|
|
if ($project->isEmpty() || $project->getId() === 'console') {
|
|
|
|
|
return $dbForConsole;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$pools = $register->get('pools');
|
2023-11-22 12:13:24 +00:00
|
|
|
|
2024-05-06 09:19:19 +00:00
|
|
|
try {
|
|
|
|
|
$dsn = new DSN($project->getAttribute('database'));
|
|
|
|
|
} catch (\InvalidArgumentException) {
|
2024-05-07 02:07:04 +00:00
|
|
|
// TODO: Temporary until all projects are using shared tables
|
2024-05-06 09:19:19 +00:00
|
|
|
$dsn = new DSN('mysql://' . $project->getAttribute('database'));
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-22 12:13:24 +00:00
|
|
|
$adapter = $pools
|
2024-05-06 09:19:19 +00:00
|
|
|
->get($dsn->getHost())
|
2022-11-12 14:35:42 +00:00
|
|
|
->pop()
|
2023-11-22 12:13:24 +00:00
|
|
|
->getResource();
|
2022-11-12 14:35:42 +00:00
|
|
|
|
2023-11-22 12:13:24 +00:00
|
|
|
$database = new Database($adapter, $cache);
|
|
|
|
|
|
2024-05-06 06:13:41 +00:00
|
|
|
try {
|
|
|
|
|
$dsn = new DSN($project->getAttribute('database'));
|
|
|
|
|
} catch (\InvalidArgumentException) {
|
2024-05-07 02:07:04 +00:00
|
|
|
// TODO: Temporary until all projects are using shared tables
|
2024-05-06 06:13:41 +00:00
|
|
|
$dsn = new DSN('mysql://' . $project->getAttribute('database'));
|
|
|
|
|
}
|
2024-05-06 05:33:36 +00:00
|
|
|
|
2024-06-06 08:11:19 +00:00
|
|
|
if ($dsn->getHost() === System::getEnv('_APP_DATABASE_SHARED_TABLES', '')) {
|
2023-11-22 12:13:24 +00:00
|
|
|
$database
|
2024-03-07 16:49:59 +00:00
|
|
|
->setSharedTables(true)
|
2023-11-27 11:27:16 +00:00
|
|
|
->setTenant($project->getInternalId())
|
2024-05-06 05:33:36 +00:00
|
|
|
->setNamespace($dsn->getParam('namespace'));
|
2024-03-07 13:52:13 +00:00
|
|
|
} else {
|
|
|
|
|
$database
|
2024-03-07 16:49:59 +00:00
|
|
|
->setSharedTables(false)
|
2024-03-07 13:52:13 +00:00
|
|
|
->setTenant(null)
|
|
|
|
|
->setNamespace('_' . $project->getInternalId());
|
|
|
|
|
}
|
2023-11-22 12:13:24 +00:00
|
|
|
|
|
|
|
|
return $database;
|
2024-02-20 11:40:55 +00:00
|
|
|
}, ['cache', 'register', 'message', 'project', 'dbForConsole']);
|
2023-12-12 11:08:14 +00:00
|
|
|
|
2023-06-05 16:13:00 +00:00
|
|
|
Server::setResource('getProjectDB', function (Group $pools, Database $dbForConsole, $cache) {
|
|
|
|
|
$databases = []; // TODO: @Meldiron This should probably be responsibility of utopia-php/pools
|
|
|
|
|
|
2023-10-01 17:39:26 +00:00
|
|
|
return function (Document $project) use ($pools, $dbForConsole, $cache, &$databases): Database {
|
2023-06-05 16:13:00 +00:00
|
|
|
if ($project->isEmpty() || $project->getId() === 'console') {
|
|
|
|
|
return $dbForConsole;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-06 06:13:41 +00:00
|
|
|
try {
|
|
|
|
|
$dsn = new DSN($project->getAttribute('database'));
|
|
|
|
|
} catch (\InvalidArgumentException) {
|
2024-05-07 02:07:04 +00:00
|
|
|
// TODO: Temporary until all projects are using shared tables
|
2024-05-06 06:13:41 +00:00
|
|
|
$dsn = new DSN('mysql://' . $project->getAttribute('database'));
|
|
|
|
|
}
|
2023-06-05 16:13:00 +00:00
|
|
|
|
2024-05-06 06:13:41 +00:00
|
|
|
if (isset($databases[$dsn->getHost()])) {
|
|
|
|
|
$database = $databases[$dsn->getHost()];
|
2023-11-27 11:27:16 +00:00
|
|
|
|
2024-06-06 08:11:19 +00:00
|
|
|
if ($dsn->getHost() === System::getEnv('_APP_DATABASE_SHARED_TABLES', '')) {
|
2023-11-22 12:13:24 +00:00
|
|
|
$database
|
2024-03-07 16:49:59 +00:00
|
|
|
->setSharedTables(true)
|
2023-11-27 11:27:16 +00:00
|
|
|
->setTenant($project->getInternalId())
|
2024-05-06 05:33:36 +00:00
|
|
|
->setNamespace($dsn->getParam('namespace'));
|
2024-03-07 13:52:13 +00:00
|
|
|
} else {
|
|
|
|
|
$database
|
2024-03-07 16:49:59 +00:00
|
|
|
->setSharedTables(false)
|
2024-03-07 13:52:13 +00:00
|
|
|
->setTenant(null)
|
|
|
|
|
->setNamespace('_' . $project->getInternalId());
|
|
|
|
|
}
|
2023-11-27 11:27:16 +00:00
|
|
|
|
2023-06-05 16:13:00 +00:00
|
|
|
return $database;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$dbAdapter = $pools
|
2024-05-06 06:13:41 +00:00
|
|
|
->get($dsn->getHost())
|
2023-06-05 16:13:00 +00:00
|
|
|
->pop()
|
|
|
|
|
->getResource();
|
|
|
|
|
|
|
|
|
|
$database = new Database($dbAdapter, $cache);
|
|
|
|
|
|
2024-05-06 06:13:41 +00:00
|
|
|
$databases[$dsn->getHost()] = $database;
|
2023-06-05 16:13:00 +00:00
|
|
|
|
2024-06-06 08:11:19 +00:00
|
|
|
if ($dsn->getHost() === System::getEnv('_APP_DATABASE_SHARED_TABLES', '')) {
|
2023-11-22 12:13:24 +00:00
|
|
|
$database
|
2024-03-07 16:49:59 +00:00
|
|
|
->setSharedTables(true)
|
2023-11-27 11:27:16 +00:00
|
|
|
->setTenant($project->getInternalId())
|
2024-05-06 05:33:36 +00:00
|
|
|
->setNamespace($dsn->getParam('namespace'));
|
2024-03-07 13:52:13 +00:00
|
|
|
} else {
|
|
|
|
|
$database
|
2024-03-07 16:49:59 +00:00
|
|
|
->setSharedTables(false)
|
2024-03-07 13:52:13 +00:00
|
|
|
->setTenant(null)
|
|
|
|
|
->setNamespace('_' . $project->getInternalId());
|
|
|
|
|
}
|
2023-06-05 16:13:00 +00:00
|
|
|
|
|
|
|
|
return $database;
|
|
|
|
|
};
|
|
|
|
|
}, ['pools', 'dbForConsole', 'cache']);
|
|
|
|
|
|
2023-12-12 11:26:14 +00:00
|
|
|
Server::setResource('abuseRetention', function () {
|
2024-04-01 11:02:47 +00:00
|
|
|
return DateTime::addSeconds(new \DateTime(), -1 * System::getEnv('_APP_MAINTENANCE_RETENTION_ABUSE', 86400));
|
2023-12-12 08:07:24 +00:00
|
|
|
});
|
|
|
|
|
|
2023-12-12 11:26:14 +00:00
|
|
|
Server::setResource('auditRetention', function () {
|
2024-04-01 11:02:47 +00:00
|
|
|
return DateTime::addSeconds(new \DateTime(), -1 * System::getEnv('_APP_MAINTENANCE_RETENTION_AUDIT', 1209600));
|
2023-12-12 08:07:24 +00:00
|
|
|
});
|
|
|
|
|
|
2023-12-12 11:26:14 +00:00
|
|
|
Server::setResource('executionRetention', function () {
|
2024-04-01 11:02:47 +00:00
|
|
|
return DateTime::addSeconds(new \DateTime(), -1 * System::getEnv('_APP_MAINTENANCE_RETENTION_EXECUTION', 1209600));
|
2023-12-12 08:07:24 +00:00
|
|
|
});
|
|
|
|
|
|
2022-11-09 17:01:43 +00:00
|
|
|
Server::setResource('cache', function (Registry $register) {
|
|
|
|
|
$pools = $register->get('pools');
|
|
|
|
|
$list = Config::getParam('pools-cache', []);
|
|
|
|
|
$adapters = [];
|
|
|
|
|
|
|
|
|
|
foreach ($list as $value) {
|
|
|
|
|
$adapters[] = $pools
|
|
|
|
|
->get($value)
|
|
|
|
|
->pop()
|
2023-10-17 19:08:01 +00:00
|
|
|
->getResource()
|
|
|
|
|
;
|
2022-11-09 17:01:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new Cache(new Sharding($adapters));
|
|
|
|
|
}, ['register']);
|
2024-02-20 11:40:55 +00:00
|
|
|
|
2024-03-06 17:34:21 +00:00
|
|
|
Server::setResource('log', fn () => new Log());
|
2024-02-20 11:40:55 +00:00
|
|
|
|
2023-10-25 07:39:59 +00:00
|
|
|
Server::setResource('queueForUsage', function (Connection $queue) {
|
|
|
|
|
return new Usage($queue);
|
|
|
|
|
}, ['queue']);
|
2024-02-20 11:40:55 +00:00
|
|
|
|
2024-01-28 09:28:59 +00:00
|
|
|
Server::setResource('queueForUsageDump', function (Connection $queue) {
|
|
|
|
|
return new UsageDump($queue);
|
|
|
|
|
}, ['queue']);
|
2024-02-21 12:29:28 +00:00
|
|
|
|
2022-12-20 12:22:58 +00:00
|
|
|
Server::setResource('queue', function (Group $pools) {
|
|
|
|
|
return $pools->get('queue')->pop()->getResource();
|
|
|
|
|
}, ['pools']);
|
2024-02-20 11:40:55 +00:00
|
|
|
|
2023-06-05 16:13:00 +00:00
|
|
|
Server::setResource('queueForDatabase', function (Connection $queue) {
|
|
|
|
|
return new EventDatabase($queue);
|
|
|
|
|
}, ['queue']);
|
2024-02-20 11:40:55 +00:00
|
|
|
|
2022-12-20 16:11:30 +00:00
|
|
|
Server::setResource('queueForMessaging', function (Connection $queue) {
|
2023-10-20 09:25:17 +00:00
|
|
|
return new Messaging($queue);
|
2022-12-20 12:22:58 +00:00
|
|
|
}, ['queue']);
|
2024-02-20 11:40:55 +00:00
|
|
|
|
2023-06-11 14:08:48 +00:00
|
|
|
Server::setResource('queueForMails', function (Connection $queue) {
|
2022-12-20 12:22:58 +00:00
|
|
|
return new Mail($queue);
|
|
|
|
|
}, ['queue']);
|
2024-02-20 11:40:55 +00:00
|
|
|
|
2022-12-20 16:11:30 +00:00
|
|
|
Server::setResource('queueForBuilds', function (Connection $queue) {
|
2022-12-20 12:22:58 +00:00
|
|
|
return new Build($queue);
|
|
|
|
|
}, ['queue']);
|
2024-02-20 11:40:55 +00:00
|
|
|
|
2022-12-20 16:11:30 +00:00
|
|
|
Server::setResource('queueForDeletes', function (Connection $queue) {
|
2022-12-20 12:22:58 +00:00
|
|
|
return new Delete($queue);
|
|
|
|
|
}, ['queue']);
|
2024-02-20 11:40:55 +00:00
|
|
|
|
2022-12-20 16:11:30 +00:00
|
|
|
Server::setResource('queueForEvents', function (Connection $queue) {
|
2023-06-05 16:13:00 +00:00
|
|
|
return new Event($queue);
|
2022-12-20 12:22:58 +00:00
|
|
|
}, ['queue']);
|
2024-02-20 11:40:55 +00:00
|
|
|
|
2022-12-20 16:11:30 +00:00
|
|
|
Server::setResource('queueForAudits', function (Connection $queue) {
|
2022-12-20 12:22:58 +00:00
|
|
|
return new Audit($queue);
|
|
|
|
|
}, ['queue']);
|
2024-02-20 11:40:55 +00:00
|
|
|
|
2022-12-20 12:22:58 +00:00
|
|
|
Server::setResource('queueForFunctions', function (Connection $queue) {
|
|
|
|
|
return new Func($queue);
|
|
|
|
|
}, ['queue']);
|
2024-02-20 11:40:55 +00:00
|
|
|
|
2022-12-20 16:11:30 +00:00
|
|
|
Server::setResource('queueForCertificates', function (Connection $queue) {
|
2022-12-20 12:22:58 +00:00
|
|
|
return new Certificate($queue);
|
|
|
|
|
}, ['queue']);
|
2024-02-20 11:40:55 +00:00
|
|
|
|
2023-10-01 17:39:26 +00:00
|
|
|
Server::setResource('queueForMigrations', function (Connection $queue) {
|
|
|
|
|
return new Migration($queue);
|
|
|
|
|
}, ['queue']);
|
2024-02-20 11:40:55 +00:00
|
|
|
|
2023-07-12 08:12:56 +00:00
|
|
|
Server::setResource('logger', function (Registry $register) {
|
2022-11-09 17:01:43 +00:00
|
|
|
return $register->get('logger');
|
|
|
|
|
}, ['register']);
|
2024-02-20 11:40:55 +00:00
|
|
|
|
2023-07-12 08:12:56 +00:00
|
|
|
Server::setResource('pools', function (Registry $register) {
|
2022-11-16 19:39:35 +00:00
|
|
|
return $register->get('pools');
|
|
|
|
|
}, ['register']);
|
2024-02-20 11:40:55 +00:00
|
|
|
|
2024-02-20 14:10:51 +00:00
|
|
|
Server::setResource('deviceForFunctions', function (Document $project) {
|
2024-02-20 11:40:55 +00:00
|
|
|
return getDevice(APP_STORAGE_FUNCTIONS . '/app-' . $project->getId());
|
|
|
|
|
}, ['project']);
|
|
|
|
|
|
2024-02-20 14:10:51 +00:00
|
|
|
Server::setResource('deviceForFiles', function (Document $project) {
|
2024-02-20 11:40:55 +00:00
|
|
|
return getDevice(APP_STORAGE_UPLOADS . '/app-' . $project->getId());
|
|
|
|
|
}, ['project']);
|
|
|
|
|
|
2024-02-20 14:10:51 +00:00
|
|
|
Server::setResource('deviceForBuilds', function (Document $project) {
|
2024-02-20 11:40:55 +00:00
|
|
|
return getDevice(APP_STORAGE_BUILDS . '/app-' . $project->getId());
|
|
|
|
|
}, ['project']);
|
|
|
|
|
|
2024-02-20 14:10:51 +00:00
|
|
|
Server::setResource('deviceForCache', function (Document $project) {
|
2024-02-20 11:40:55 +00:00
|
|
|
return getDevice(APP_STORAGE_CACHE . '/app-' . $project->getId());
|
|
|
|
|
}, ['project']);
|
2023-05-29 13:58:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
$pools = $register->get('pools');
|
|
|
|
|
$platform = new Appwrite();
|
2024-05-22 13:37:45 +00:00
|
|
|
$args = $platform->getEnv('argv');
|
|
|
|
|
|
2023-10-12 04:55:30 +00:00
|
|
|
if (!isset($args[1])) {
|
2023-06-02 03:54:34 +00:00
|
|
|
Console::error('Missing worker name');
|
2023-10-12 04:55:30 +00:00
|
|
|
Console::exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
\array_shift($args);
|
|
|
|
|
$workerName = $args[0];
|
2023-05-29 13:58:45 +00:00
|
|
|
|
2023-11-22 13:50:57 +00:00
|
|
|
if (\str_starts_with($workerName, 'databases')) {
|
2024-04-01 11:02:47 +00:00
|
|
|
$queueName = System::getEnv('_APP_QUEUE_NAME', 'database_db_main');
|
2023-11-22 13:50:57 +00:00
|
|
|
} else {
|
2024-04-01 11:02:47 +00:00
|
|
|
$queueName = System::getEnv('_APP_QUEUE_NAME', 'v1-' . strtolower($workerName));
|
2023-11-22 13:50:57 +00:00
|
|
|
}
|
|
|
|
|
|
2023-06-02 03:54:34 +00:00
|
|
|
try {
|
2023-10-12 06:11:08 +00:00
|
|
|
/**
|
|
|
|
|
* Any worker can be configured with the following env vars:
|
|
|
|
|
* - _APP_WORKERS_NUM The total number of worker processes
|
|
|
|
|
* - _APP_WORKER_PER_CORE The number of worker processes per core (ignored if _APP_WORKERS_NUM is set)
|
2023-11-27 11:27:16 +00:00
|
|
|
* - _APP_QUEUE_NAME The name of the queue to read for database events
|
2023-10-12 06:11:08 +00:00
|
|
|
*/
|
2023-06-02 03:54:34 +00:00
|
|
|
$platform->init(Service::TYPE_WORKER, [
|
2024-04-01 11:02:47 +00:00
|
|
|
'workersNum' => System::getEnv('_APP_WORKERS_NUM', 1),
|
2023-06-02 03:54:34 +00:00
|
|
|
'connection' => $pools->get('queue')->pop()->getResource(),
|
2023-06-04 16:25:56 +00:00
|
|
|
'workerName' => strtolower($workerName) ?? null,
|
2023-10-17 13:55:08 +00:00
|
|
|
'queueName' => $queueName
|
2023-06-02 03:54:34 +00:00
|
|
|
]);
|
2024-02-08 01:17:54 +00:00
|
|
|
} catch (\Throwable $e) {
|
2023-06-04 08:19:49 +00:00
|
|
|
Console::error($e->getMessage() . ', File: ' . $e->getFile() . ', Line: ' . $e->getLine());
|
2023-06-02 03:54:34 +00:00
|
|
|
}
|
2023-05-29 13:58:45 +00:00
|
|
|
|
|
|
|
|
$worker = $platform->getWorker();
|
|
|
|
|
|
|
|
|
|
$worker
|
|
|
|
|
->shutdown()
|
|
|
|
|
->inject('pools')
|
|
|
|
|
->action(function (Group $pools) {
|
|
|
|
|
$pools->reclaim();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$worker
|
|
|
|
|
->error()
|
|
|
|
|
->inject('error')
|
|
|
|
|
->inject('logger')
|
2023-05-24 18:14:58 +00:00
|
|
|
->inject('log')
|
2024-01-18 09:13:11 +00:00
|
|
|
->inject('pools')
|
2024-02-15 15:12:37 +00:00
|
|
|
->inject('project')
|
2024-03-05 09:31:51 +00:00
|
|
|
->action(function (Throwable $error, ?Logger $logger, Log $log, Group $pools, Document $project) use ($queueName) {
|
2024-01-18 09:13:11 +00:00
|
|
|
$pools->reclaim();
|
2024-04-01 11:02:47 +00:00
|
|
|
$version = System::getEnv('_APP_VERSION', 'UNKNOWN');
|
2023-05-29 13:58:45 +00:00
|
|
|
|
2024-01-02 13:02:11 +00:00
|
|
|
if ($logger) {
|
2023-05-29 13:58:45 +00:00
|
|
|
$log->setNamespace("appwrite-worker");
|
|
|
|
|
$log->setServer(\gethostname());
|
|
|
|
|
$log->setVersion($version);
|
|
|
|
|
$log->setType(Log::TYPE_ERROR);
|
|
|
|
|
$log->setMessage($error->getMessage());
|
2023-11-22 13:50:57 +00:00
|
|
|
$log->setAction('appwrite-queue-' . $queueName);
|
2023-05-29 13:58:45 +00:00
|
|
|
$log->addTag('verboseType', get_class($error));
|
|
|
|
|
$log->addTag('code', $error->getCode());
|
2024-02-15 15:15:10 +00:00
|
|
|
$log->addTag('projectId', $project->getId() ?? 'n/a');
|
2023-05-29 13:58:45 +00:00
|
|
|
$log->addExtra('file', $error->getFile());
|
|
|
|
|
$log->addExtra('line', $error->getLine());
|
|
|
|
|
$log->addExtra('trace', $error->getTraceAsString());
|
2023-08-23 19:08:26 +00:00
|
|
|
$log->addExtra('roles', Authorization::getRoles());
|
2023-05-29 13:58:45 +00:00
|
|
|
|
2024-04-01 11:02:47 +00:00
|
|
|
$isProduction = System::getEnv('_APP_ENV', 'development') === 'production';
|
2023-05-29 13:58:45 +00:00
|
|
|
$log->setEnvironment($isProduction ? Log::ENVIRONMENT_PRODUCTION : Log::ENVIRONMENT_STAGING);
|
|
|
|
|
|
2023-05-24 19:07:41 +00:00
|
|
|
$responseCode = $logger->addLog($log);
|
|
|
|
|
Console::info('Usage stats log pushed with status code: ' . $responseCode);
|
2023-05-29 13:58:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Console::error('[Error] Type: ' . get_class($error));
|
|
|
|
|
Console::error('[Error] Message: ' . $error->getMessage());
|
|
|
|
|
Console::error('[Error] File: ' . $error->getFile());
|
|
|
|
|
Console::error('[Error] Line: ' . $error->getLine());
|
|
|
|
|
});
|
|
|
|
|
|
2024-01-28 09:28:59 +00:00
|
|
|
$worker->workerStart()
|
|
|
|
|
->action(function () use ($workerName) {
|
|
|
|
|
Console::info("Worker $workerName started");
|
|
|
|
|
});
|
2023-07-10 08:07:54 +00:00
|
|
|
|
2023-10-25 07:39:59 +00:00
|
|
|
$worker->start();
|