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/ diff --git a/bin/worker.php b/bin/worker.php index 29fd5a4fb7..b88c30aeb5 100755 --- a/bin/worker.php +++ b/bin/worker.php @@ -15,64 +15,17 @@ 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 -$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 -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'; -$dice = (new Dice())->addRules(include __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]]); +$dice = (new Dice())->addRules(require(dirname(__DIR__) . '/static/dependencies.config.php')); -DI::init($dice); -\Friendica\Core\Logger\Handler\ErrorHandler::register($dice->create(\Psr\Log\LoggerInterface::class)); +$app = \Friendica\App::fromDice($dice); -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 e0f1a0e2da..0afdf8bd42 100644 --- a/src/App.php +++ b/src/App.php @@ -21,6 +21,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; @@ -48,8 +49,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 +435,53 @@ class App } } + public function processWorker(array $options): void + { + $this->setupContainerForAddons(); + + $this->setupContainerForLogger(LogChannel::WORKER); + + $this->setupLegacyServiceLocator(); + + $this->registerErrorHandler(); + + /** @var Mode */ + $mode = $this->container->create(Mode::class); + + $mode->setExecutor(Mode::WORKER); + + /** @var BasePath */ + $basePath = $this->container->create(BasePath::class); + + // Check the database structure and possibly fixes it + Update::check($basePath->getPath(), true); + + // Quit when in maintenance + if (!$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); + + /** @var ProcessRepository */ + $processRepository = $this->container->create(ProcessRepository::class); + + $process = $processRepository->create(getmypid(), 'worker.php'); + + Worker::processQueue($run_cron, $process); + + Worker::unclaimProcess($process); + + $processRepository->delete($process); + } + private function setupContainerForAddons(): void { /** @var \Friendica\Core\Addon\Capability\ICanLoadAddons $addonLoader */