Merge pull request #14628 from Art4/refactor-entrypoint-worker.php

Refactor entrypoint `worker.php`
This commit is contained in:
Hypolite Petovan 2024-12-28 20:11:41 -05:00 committed by GitHub
commit 3f39b6366f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 54 additions and 54 deletions

View file

@ -10,6 +10,7 @@ parameters:
- bin/auth_ejabberd.php
- bin/console.php
- bin/daemon.php
- bin/worker.php
- index.php
- src/

View file

@ -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 ?: []);

View file

@ -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 */