From 40927178f4658fb36aef2542d9f1c5c252235dc3 Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 26 Dec 2024 11:56:25 +0000 Subject: [PATCH 01/12] refactor chdir() in worker.php --- bin/worker.php | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/bin/worker.php b/bin/worker.php index 29fd5a4fb7..668e99f904 100755 --- a/bin/worker.php +++ b/bin/worker.php @@ -28,16 +28,7 @@ $longopts = ['spawn', 'no_cron']; $options = getopt($shortopts, $longopts); // Ensure that worker.php is executed from the base path of the installation -if (!file_exists("index.php") && (sizeof($_SERVER["argv"]) != 0)) { - $directory = dirname($_SERVER["argv"][0]); - - if (substr($directory, 0, 1) != '/') { - $directory = $_SERVER["PWD"] . '/' . $directory; - } - $directory = realpath($directory . '/..'); - - chdir($directory); -} +chdir(dirname(__DIR__)); require dirname(__DIR__) . '/vendor/autoload.php'; From 51b24cbac9c4f3b7950cd3bacf1f98caf162c5d1 Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 26 Dec 2024 13:57:46 +0000 Subject: [PATCH 02/12] refactor getopt() call --- bin/worker.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/bin/worker.php b/bin/worker.php index 668e99f904..c0b990fd23 100755 --- a/bin/worker.php +++ b/bin/worker.php @@ -23,9 +23,7 @@ use Friendica\DI; use Psr\Log\LoggerInterface; // Get options -$shortopts = 'sn'; -$longopts = ['spawn', 'no_cron']; -$options = getopt($shortopts, $longopts); +$options = getopt('sn', ['spawn', 'no_cron']); // Ensure that worker.php is executed from the base path of the installation chdir(dirname(__DIR__)); From bb911296a6e042d5b513cde1f1fc18df0fa1dc62 Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 26 Dec 2024 14:08:40 +0000 Subject: [PATCH 03/12] require dice rules --- bin/worker.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bin/worker.php b/bin/worker.php index c0b990fd23..684a5d8076 100755 --- a/bin/worker.php +++ b/bin/worker.php @@ -30,7 +30,8 @@ chdir(dirname(__DIR__)); require dirname(__DIR__) . '/vendor/autoload.php'; -$dice = (new Dice())->addRules(include __DIR__ . '/../static/dependencies.config.php'); +$dice = (new Dice())->addRules(require(dirname(__DIR__) . '/static/dependencies.config.php')); + /** @var \Friendica\Core\Addon\Capability\ICanLoadAddons $addonLoader */ $addonLoader = $dice->create(\Friendica\Core\Addon\Capability\ICanLoadAddons::class); $dice = $dice->addRules($addonLoader->getActiveAddonConfig('dependencies')); From bb9e09ef51f1525452190ac11b6ae3c242bf1c6b Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 26 Dec 2024 18:58:07 +0000 Subject: [PATCH 04/12] check worker.php with PHPStan --- .phpstan.neon | 1 + 1 file changed, 1 insertion(+) diff --git a/.phpstan.neon b/.phpstan.neon index 918ebe6eb8..6f8b9ddf83 100644 --- a/.phpstan.neon +++ b/.phpstan.neon @@ -10,6 +10,7 @@ parameters: - bin/auth_ejabberd.php - bin/console.php - bin/daemon.php + - bin/worker.php - index.php - src/ From c5d7f26a8c22eef66ab25adade8513a8df5183fd Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 26 Dec 2024 19:04:27 +0000 Subject: [PATCH 05/12] Move code from worker.php into App::processWorker() --- bin/worker.php | 41 ++--------------------------------------- src/App.php | 41 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 41 deletions(-) diff --git a/bin/worker.php b/bin/worker.php index 684a5d8076..b88c30aeb5 100755 --- a/bin/worker.php +++ b/bin/worker.php @@ -15,12 +15,6 @@ if (php_sapi_name() !== 'cli') { } use Dice\Dice; -use Friendica\App\Mode; -use Friendica\Core\Logger\Capability\LogChannel; -use Friendica\Core\Update; -use Friendica\Core\Worker; -use Friendica\DI; -use Psr\Log\LoggerInterface; // Get options $options = getopt('sn', ['spawn', 'no_cron']); @@ -32,37 +26,6 @@ require dirname(__DIR__) . '/vendor/autoload.php'; $dice = (new Dice())->addRules(require(dirname(__DIR__) . '/static/dependencies.config.php')); -/** @var \Friendica\Core\Addon\Capability\ICanLoadAddons $addonLoader */ -$addonLoader = $dice->create(\Friendica\Core\Addon\Capability\ICanLoadAddons::class); -$dice = $dice->addRules($addonLoader->getActiveAddonConfig('dependencies')); -$dice = $dice->addRule(LoggerInterface::class, ['constructParams' => [LogChannel::WORKER]]); +$app = \Friendica\App::fromDice($dice); -DI::init($dice); -\Friendica\Core\Logger\Handler\ErrorHandler::register($dice->create(\Psr\Log\LoggerInterface::class)); - -DI::mode()->setExecutor(Mode::WORKER); - -// Check the database structure and possibly fixes it -Update::check(DI::basePath(), true); - -// Quit when in maintenance -if (!DI::mode()->has(Mode::MAINTENANCEDISABLED)) { - return; -} - -$spawn = array_key_exists('s', $options) || array_key_exists('spawn', $options); - -if ($spawn) { - Worker::spawnWorker(); - exit(); -} - -$run_cron = !array_key_exists('n', $options) && !array_key_exists('no_cron', $options); - -$process = DI::process()->create(getmypid(), basename(__FILE__)); - -Worker::processQueue($run_cron, $process); - -Worker::unclaimProcess($process); - -DI::process()->delete($process); +$app->processWorker($options ?: []); diff --git a/src/App.php b/src/App.php index 0345eb3adf..fc42ca2b1a 100644 --- a/src/App.php +++ b/src/App.php @@ -47,8 +47,6 @@ use Friendica\Util\Profiler; use Psr\Http\Message\ServerRequestInterface; use Psr\Log\LoggerInterface; - - /** * Our main application structure for the life of this page. * @@ -436,6 +434,45 @@ class App } } + public function processWorker(array $options): void + { + /** @var \Friendica\Core\Addon\Capability\ICanLoadAddons $addonLoader */ + $addonLoader = $this->container->create(\Friendica\Core\Addon\Capability\ICanLoadAddons::class); + $this->container = $this->container->addRules($addonLoader->getActiveAddonConfig('dependencies')); + + $this->container = $this->container->addRule(LoggerInterface::class, ['constructParams' => [LogChannel::WORKER]]); + + DI::init($this->container); + \Friendica\Core\Logger\Handler\ErrorHandler::register($this->container->create(\Psr\Log\LoggerInterface::class)); + + DI::mode()->setExecutor(Mode::WORKER); + + // Check the database structure and possibly fixes it + Update::check(DI::basePath(), true); + + // Quit when in maintenance + if (!DI::mode()->has(Mode::MAINTENANCEDISABLED)) { + return; + } + + $spawn = array_key_exists('s', $options) || array_key_exists('spawn', $options); + + if ($spawn) { + Worker::spawnWorker(); + exit(); + } + + $run_cron = !array_key_exists('n', $options) && !array_key_exists('no_cron', $options); + + $process = DI::process()->create(getmypid(), basename(__FILE__)); + + Worker::processQueue($run_cron, $process); + + Worker::unclaimProcess($process); + + DI::process()->delete($process); + } + private function setupContainerForAddons(): void { /** @var \Friendica\Core\Addon\Capability\ICanLoadAddons $addonLoader */ From 8edca631e6e2527bf43be4ad2cfd1203bb48a13f Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 26 Dec 2024 19:11:21 +0000 Subject: [PATCH 06/12] Refactor setup Container for addons --- src/App.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/App.php b/src/App.php index fc42ca2b1a..38408e897f 100644 --- a/src/App.php +++ b/src/App.php @@ -436,9 +436,7 @@ class App public function processWorker(array $options): void { - /** @var \Friendica\Core\Addon\Capability\ICanLoadAddons $addonLoader */ - $addonLoader = $this->container->create(\Friendica\Core\Addon\Capability\ICanLoadAddons::class); - $this->container = $this->container->addRules($addonLoader->getActiveAddonConfig('dependencies')); + $this->setupContainerForAddons(); $this->container = $this->container->addRule(LoggerInterface::class, ['constructParams' => [LogChannel::WORKER]]); From 31deac51178a533279a6639892a0ec0a87996b5b Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 26 Dec 2024 19:11:54 +0000 Subject: [PATCH 07/12] Refactor setup Container for Logger --- src/App.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/App.php b/src/App.php index 38408e897f..51134c05e7 100644 --- a/src/App.php +++ b/src/App.php @@ -438,7 +438,7 @@ class App { $this->setupContainerForAddons(); - $this->container = $this->container->addRule(LoggerInterface::class, ['constructParams' => [LogChannel::WORKER]]); + $this->setupContainerForLogger(LogChannel::WORKER); DI::init($this->container); \Friendica\Core\Logger\Handler\ErrorHandler::register($this->container->create(\Psr\Log\LoggerInterface::class)); From e0a249f9a4335bebbe8db6cc94e027598790ec17 Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 26 Dec 2024 19:12:32 +0000 Subject: [PATCH 08/12] Refactor r init service locator --- src/App.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/App.php b/src/App.php index 51134c05e7..f5c0240f26 100644 --- a/src/App.php +++ b/src/App.php @@ -440,7 +440,8 @@ class App $this->setupContainerForLogger(LogChannel::WORKER); - DI::init($this->container); + $this->setupLegacyServiceLocator(); + \Friendica\Core\Logger\Handler\ErrorHandler::register($this->container->create(\Psr\Log\LoggerInterface::class)); DI::mode()->setExecutor(Mode::WORKER); From 3ae9ae8728ec94f9a1785fb09ab7a4ef28b6a2ff Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 26 Dec 2024 19:13:34 +0000 Subject: [PATCH 09/12] use registerErrorHandler() in processWorker() --- src/App.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/App.php b/src/App.php index f5c0240f26..e0ab61c5dd 100644 --- a/src/App.php +++ b/src/App.php @@ -442,7 +442,7 @@ class App $this->setupLegacyServiceLocator(); - \Friendica\Core\Logger\Handler\ErrorHandler::register($this->container->create(\Psr\Log\LoggerInterface::class)); + $this->registerErrorHandler(); DI::mode()->setExecutor(Mode::WORKER); From caec1ed5767c1d21285a3e5c02b026377bc3b9fd Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 26 Dec 2024 19:15:33 +0000 Subject: [PATCH 10/12] replace DI::mode() with container --- src/App.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/App.php b/src/App.php index e0ab61c5dd..79ad712392 100644 --- a/src/App.php +++ b/src/App.php @@ -444,13 +444,16 @@ class App $this->registerErrorHandler(); - DI::mode()->setExecutor(Mode::WORKER); + /** @var Mode */ + $mode = $this->container->create(Mode::class); + + $mode->setExecutor(Mode::WORKER); // Check the database structure and possibly fixes it Update::check(DI::basePath(), true); // Quit when in maintenance - if (!DI::mode()->has(Mode::MAINTENANCEDISABLED)) { + if (!$mode->has(Mode::MAINTENANCEDISABLED)) { return; } From 867eba73814a1db97396dfe98977544f3663d9b8 Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 26 Dec 2024 19:17:18 +0000 Subject: [PATCH 11/12] Replace DI::basePath() with BasePath service --- src/App.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/App.php b/src/App.php index 79ad712392..f37e38e677 100644 --- a/src/App.php +++ b/src/App.php @@ -449,8 +449,11 @@ class App $mode->setExecutor(Mode::WORKER); + /** @var BasePath */ + $basePath = $this->container->create(BasePath::class); + // Check the database structure and possibly fixes it - Update::check(DI::basePath(), true); + Update::check($basePath->getPath(), true); // Quit when in maintenance if (!$mode->has(Mode::MAINTENANCEDISABLED)) { From 04e92766fc1ab10df6ecdc5b172f27418b57866e Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 26 Dec 2024 19:29:23 +0000 Subject: [PATCH 12/12] Refactor calling process repository --- src/App.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/App.php b/src/App.php index f37e38e677..99a788b8ca 100644 --- a/src/App.php +++ b/src/App.php @@ -20,6 +20,7 @@ use Friendica\Core\Config\Factory\Config; use Friendica\Core\KeyValueStorage\Capability\IManageKeyValuePairs; use Friendica\Core\Renderer; use Friendica\Core\Session\Capability\IHandleUserSessions; +use Friendica\Core\Worker\Repository\Process as ProcessRepository; use Friendica\Database\DBA; use Friendica\Database\Definition\DbaDefinition; use Friendica\Database\Definition\ViewDefinition; @@ -469,13 +470,16 @@ class App $run_cron = !array_key_exists('n', $options) && !array_key_exists('no_cron', $options); - $process = DI::process()->create(getmypid(), basename(__FILE__)); + /** @var ProcessRepository */ + $processRepository = $this->container->create(ProcessRepository::class); + + $process = $processRepository->create(getmypid(), 'worker.php'); Worker::processQueue($run_cron, $process); Worker::unclaimProcess($process); - DI::process()->delete($process); + $processRepository->delete($process); } private function setupContainerForAddons(): void