appwrite/app/worker.php

164 lines
5.2 KiB
PHP
Raw Permalink Normal View History

2022-11-09 17:01:43 +00:00
<?php
require_once __DIR__ . '/init.php';
$registerWorkerMessageResources = require __DIR__ . '/init/worker/message.php';
2026-03-23 04:36:58 +00:00
2024-11-20 10:10:57 +00:00
use Appwrite\Certificates\LetsEncrypt;
2023-05-29 13:58:45 +00:00
use Appwrite\Platform\Appwrite;
2022-11-09 17:01:43 +00:00
use Swoole\Runtime;
use Utopia\Console;
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;
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;
2024-10-08 07:54:40 +00:00
use Utopia\Pools\Group;
2026-03-18 12:14:03 +00:00
use Utopia\Queue\Adapter\Swoole;
use Utopia\Queue\Broker\Pool as BrokerPool;
2024-10-08 07:54:40 +00:00
use Utopia\Queue\Server;
use Utopia\Span\Span;
2024-04-01 11:02:47 +00:00
use Utopia\System\System;
2022-11-16 10:40:41 +00:00
Runtime::enableCoroutine();
require_once __DIR__ . '/init/span.php';
2023-06-05 16:13:00 +00:00
2026-03-18 12:14:03 +00:00
global $container;
$container->set('pools', function ($register) {
return $register->get('pools');
}, ['register']);
2024-10-08 07:54:40 +00:00
2026-03-18 12:14:03 +00:00
$container->set('authorization', function () {
$authorization = new Authorization();
$authorization->disable();
return $authorization;
}, []);
2026-03-18 12:14:03 +00:00
$container->set('project', fn () => new Document([]), []);
2026-03-18 12:14:03 +00:00
$container->set('log', fn () => new Log(), []);
2025-04-17 05:09:08 +00:00
2026-03-18 12:14:03 +00:00
$container->set('consumer', function (Group $pools) {
return new BrokerPool(consumer: $pools->get('consumer'));
2024-10-08 07:54:40 +00:00
}, ['pools']);
2026-03-18 12:14:03 +00:00
$container->set('consumerDatabases', function (BrokerPool $consumer) {
2025-07-16 17:11:04 +00:00
return $consumer;
}, ['consumer']);
2026-03-18 12:14:03 +00:00
$container->set('consumerMigrations', function (BrokerPool $consumer) {
2025-07-16 17:11:04 +00:00
return $consumer;
}, ['consumer']);
2026-03-18 12:14:03 +00:00
$container->set('consumerStatsUsage', function (BrokerPool $consumer) {
2025-07-16 17:11:04 +00:00
return $consumer;
}, ['consumer']);
2025-07-03 18:09:08 +00:00
2026-03-18 12:14:03 +00:00
$container->set('certificates', function () {
2024-11-20 10:10:57 +00:00
$email = System::getEnv('_APP_EMAIL_CERTIFICATES', System::getEnv('_APP_SYSTEM_SECURITY_EMAIL_ADDRESS'));
if (empty($email)) {
throw new Exception('You must set a valid security email address (_APP_EMAIL_CERTIFICATES) to issue a LetsEncrypt SSL certificate.');
}
return new LetsEncrypt($email);
2026-03-18 12:14:03 +00:00
}, []);
2026-01-16 09:36:35 +00:00
2023-05-29 13:58:45 +00:00
$platform = new Appwrite();
2026-04-01 10:07:34 +00:00
$args = $_SERVER['argv'] ?? [];
2024-05-22 13:37:45 +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
if (\str_starts_with($workerName, 'databases')) {
2024-04-01 11:02:47 +00:00
$queueName = System::getEnv('_APP_QUEUE_NAME', 'database_db_main');
} else {
2024-04-01 11:02:47 +00:00
$queueName = System::getEnv('_APP_QUEUE_NAME', 'v1-' . strtolower($workerName));
}
2026-03-23 04:50:45 +00:00
/** @var \Utopia\Pools\Group $pools */
2026-03-19 04:47:25 +00:00
$pools = $container->get('pools');
2026-03-18 12:14:03 +00:00
2026-03-19 04:47:25 +00:00
$adapter = new Swoole(
$pools->get('consumer')->pop()->getResource(),
System::getEnv('_APP_WORKERS_NUM', 1),
$queueName
);
2026-03-18 12:14:03 +00:00
2026-03-19 04:47:25 +00:00
$worker = new Server($adapter, $container);
2026-03-18 12:14:03 +00:00
2026-03-19 04:47:25 +00:00
try {
$worker->init()->action(function () use ($worker, $registerWorkerMessageResources, $queueName) {
2026-03-23 04:36:58 +00:00
$registerWorkerMessageResources($worker->getContainer());
Span::init("worker.{$queueName}");
});
$worker->shutdown()->action(function () {
Span::current()?->finish();
2026-03-18 12:14:03 +00:00
});
$container->set('bus', function ($register) use ($worker) {
return $register->get('bus')->setResolver(
fn (string $name) => $worker->getContainer()->get($name)
);
}, ['register']);
$platform->setWorker($worker);
$platform->init(Service::TYPE_WORKER, [
2026-03-18 12:14:03 +00:00
'workerName' => strtolower($workerName),
]);
} catch (\Throwable $e) {
Console::error($e->getMessage() . ', File: ' . $e->getFile() . ', Line: ' . $e->getLine());
2026-03-18 12:14:03 +00:00
Console::exit(1);
2023-06-02 03:54:34 +00:00
}
2023-05-29 13:58:45 +00:00
2024-10-08 07:54:40 +00:00
$worker
->error()
2023-05-29 13:58:45 +00:00
->inject('error')
->inject('logger')
2023-05-24 18:14:58 +00:00
->inject('log')
2024-02-15 15:12:37 +00:00
->inject('project')
->inject('authorization')
2026-03-18 12:14:03 +00:00
->action(function (Throwable $error, ?Logger $logger, Log $log, Document $project, Authorization $authorization) use ($queueName) {
2024-04-01 11:02:47 +00:00
$version = System::getEnv('_APP_VERSION', 'UNKNOWN');
2023-05-29 13:58:45 +00:00
Span::error($error);
2024-01-02 13:02:11 +00:00
if ($logger) {
$log->setNamespace('appwrite-worker');
2024-11-26 13:54:27 +00:00
$log->setServer(System::getEnv('_APP_LOGGING_SERVICE_IDENTIFIER', \gethostname()));
2023-05-29 13:58:45 +00:00
$log->setVersion($version);
$log->setType(Log::TYPE_ERROR);
$log->setMessage($error->getMessage());
$log->setAction('appwrite-queue-' . $queueName);
2023-05-29 13:58:45 +00:00
$log->addTag('verboseType', get_class($error));
$log->addTag('code', $error->getCode());
chore: bump PHPStan to level 4 and fix all new errors Raises `phpstan.neon` level from 3 to 4 and fixes the 549 new errors that level 4 surfaces across 157 files. Fixes are root-cause — no `@phpstan-ignore`, no `@var` casts, no baseline entries, no widened types. A handful of latent bugs were fixed along the way: - `app/controllers/general.php`: path-traversal guard was negating `\substr(...)` before the strict comparison (`!\substr(...) === $base` was always `false === $base`). Rewritten as `\substr(...) !== $base`. - `src/Appwrite/Platform/Modules/Databases/Http/Databases/Logs/XList.php` and `.../TablesDB/Logs/XList.php`: were importing the raw Matomo `DeviceDetector` (whose `getDevice()` returns `?int`) but treating the result as an array with `deviceName/deviceBrand/deviceModel` keys. Swapped to `Appwrite\Detector\Detector`, matching the wrapper already used a few lines below for `$os`/`$client`. - `src/Appwrite/Platform/Modules/Functions/Workers/Builds.php`: a match key was checking `$resourceKey === 'functions'` when `$resourceKey` is `'functionId'|'siteId'` — always false. Switched to the intended `$resource->getCollection() === 'functions'` check. - `src/Appwrite/OpenSSL/OpenSSL.php`: `encrypt()` return type tightened to `string|false` to match `openssl_encrypt`; this lets callers' `=== false` error handling remain meaningful. - `app/controllers/api/messaging.php`: removed a dead `array_key_exists('from', [])` branch in the Msg91 provider (empty array literal; branch was unreachable). Large cleanup categories across the 549 fixes: - Removed redundant `?? default` on array offsets and expressions that PHPStan now knows are non-nullable. - Removed unreachable statements (mostly `return;` after `throw` or `markTestSkipped()`). - Removed redundant `is_array`/`is_string`/`is_bool`/`instanceof` checks on already-narrowed types. - Added `default =>` arms (or throwing arms) to non-exhaustive matches on `string`/`mixed` input. - Removed dead `$document === false` branches where method return types were tightened to non-nullable `Document`. - Removed unused properties (`$version` on Etsy/Zoom OAuth2, `$paths` on Installer State, `$source` on MigrationsWorker, `$account2` on two GraphQL auth tests), unused traits (`ApiVectorsDB`, `DatabaseFixture`), and an unused `cleanupStaleExecutions` task method. - Replaced `assertTrue(true)` and redundant `assertIsArray`/`assertIsString`/ `assertNotNull` assertions with `addToAssertionCount(1)` or `assertNotEmpty` where the runtime type was already known.
2026-04-19 12:01:20 +00:00
$log->addTag('projectId', $project->getId());
2023-05-29 13:58:45 +00:00
$log->addExtra('file', $error->getFile());
$log->addExtra('line', $error->getLine());
$log->addExtra('trace', $error->getTraceAsString());
$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);
try {
$responseCode = $logger->addLog($log);
Console::info('Error log pushed with status code: ' . $responseCode);
} catch (Throwable $th) {
Console::error('Error pushing log: ' . $th->getMessage());
}
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-10-08 07:54:40 +00:00
$worker->start();