]> git.mxchange.org Git - friendica.git/commitdiff
Move 'bin/worker.php' to 'bin/console.php worker'
authorPhilipp <admin@philipp.info>
Thu, 2 Jan 2025 13:37:56 +0000 (14:37 +0100)
committerPhilipp <admin@philipp.info>
Sun, 5 Jan 2025 22:13:37 +0000 (23:13 +0100)
bin/worker.php
src/App.php
src/Console/Worker.php [new file with mode: 0644]
src/Core/Console.php

index b88c30aeb5a1020a21e59a2d71be67353a21fddf..4837cd67eb63b0deadfb58dc286382f063c9d392 100755 (executable)
@@ -7,6 +7,8 @@
  * SPDX-License-Identifier: AGPL-3.0-or-later
  *
  * Starts the background processing
+ *
+ * @deprecated 2025.01 use bin/console.php worker instead
  */
 
 if (php_sapi_name() !== 'cli') {
@@ -16,9 +18,6 @@ if (php_sapi_name() !== 'cli') {
 
 use Dice\Dice;
 
-// Get options
-$options = getopt('sn', ['spawn', 'no_cron']);
-
 // Ensure that worker.php is executed from the base path of the installation
 chdir(dirname(__DIR__));
 
@@ -28,4 +27,7 @@ $dice = (new Dice())->addRules(require(dirname(__DIR__) . '/static/dependencies.
 
 $app = \Friendica\App::fromDice($dice);
 
-$app->processWorker($options ?: []);
+$argv = $_SERVER['argv'] ?? [];
+array_splice($argv, 1, 0, "worker");
+
+$app->processConsole($argv);
index 923400a21c3839e7fffc362bc3cbbeb4b410a81e..79981526652649526919127c5f6bd5638d4fac15 100644 (file)
@@ -20,7 +20,6 @@ use Friendica\Content\Nav;
 use Friendica\Core\Config\Factory\Config;
 use Friendica\Core\Renderer;
 use Friendica\Core\Session\Capability\IHandleUserSessions;
-use Friendica\Core\Worker\Repository\Process as ProcessRepository;
 use Friendica\Database\Definition\DbaDefinition;
 use Friendica\Database\Definition\ViewDefinition;
 use Friendica\Module\Maintenance;
@@ -31,7 +30,6 @@ use Friendica\Core\Logger\Capability\LogChannel;
 use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
 use Friendica\Core\System;
 use Friendica\Core\Update;
-use Friendica\Core\Worker;
 use Friendica\Module\Special\HTTPException as ModuleHTTPException;
 use Friendica\Network\HTTPException;
 use Friendica\Protocol\ATProtocol\DID;
@@ -215,55 +213,6 @@ class App
                (new \Friendica\Core\Console($this->container, $argv))->execute();
        }
 
-       public function processWorker(array $options): void
-       {
-               $this->setupContainerForAddons();
-
-               $this->setupContainerForLogger(LogChannel::WORKER);
-
-               $this->setupLegacyServiceLocator();
-
-               $this->registerErrorHandler();
-
-               $this->registerTemplateEngine();
-
-               /** @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 */
diff --git a/src/Console/Worker.php b/src/Console/Worker.php
new file mode 100644 (file)
index 0000000..fa15b2e
--- /dev/null
@@ -0,0 +1,100 @@
+<?php
+
+// Copyright (C) 2010-2024, the Friendica project
+// SPDX-FileCopyrightText: 2010-2024 the Friendica project
+//
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+declare(strict_types=1);
+
+namespace Friendica\Console;
+
+use Friendica\App\Mode;
+use Asika\SimpleConsole\Console;
+use Friendica\Core\Update;
+use Friendica\Core\Worker as CoreWorker;
+use Friendica\Core\Worker\Repository\Process as ProcessRepository;
+use Friendica\Util\BasePath;
+
+/**
+ * Console command for interacting with the daemon
+ */
+final class Worker extends Console
+{
+       private Mode $mode;
+       private BasePath $basePath;
+       private ProcessRepository $processRepo;
+
+       /**
+        * @param Mode              $mode
+        * @param BasePath          $basePath
+        * @param ProcessRepository $processRepo
+        * @param array|null        $argv
+        */
+       public function __construct(Mode $mode, BasePath $basePath, ProcessRepository $processRepo, array $argv = null)
+       {
+               parent::__construct($argv);
+
+               $this->mode        = $mode;
+               $this->basePath    = $basePath;
+               $this->processRepo = $processRepo;
+       }
+
+       protected function getHelp(): string
+       {
+               return <<<HELP
+Worker - Start a worker
+Synopsis
+       bin/console worker [-h|--help|-?] [-v] [-n|--no_cron] [-s|--spawn]
+
+Description
+    Start a worker process
+
+Options
+    -h|--help|-?            Show help information
+    -v                      Show more debug information.
+    -n|--no_cron            Don't executes the Cronjob
+    -s|--spawn              Spawn an additional worker
+
+Examples
+       bin/console daemon start -f
+               Starts the daemon in the foreground
+
+       bin/console daemon status
+               Gets the status of the daemon
+HELP;
+       }
+
+       protected function doExecute()
+       {
+               if ($this->executable !== 'bin/console.php') {
+                       $this->out(sprintf("'%s' is deprecated and will removed. Please use 'bin/console.php worker' instead", $this->executable));
+               }
+
+               $this->mode->setExecutor(Mode::WORKER);
+
+               // Check the database structure and possibly fixes it
+               Update::check($this->basePath->getPath(), true);
+
+               // Quit when in maintenance
+               if (!$this->mode->has(Mode::MAINTENANCEDISABLED)) {
+                       return;
+               }
+
+               $spawn = $this->getOption(['s', 'spawn'], false);
+
+               if ($spawn) {
+                       CoreWorker::spawnWorker();
+                       exit();
+               }
+
+               $run_cron = !$this->getOption(['n', 'no_cron'], false);
+
+               $process = $this->processRepo->create(getmypid(), 'worker.php');
+
+               CoreWorker::processQueue($run_cron, $process);
+               CoreWorker::unclaimProcess($process);
+
+               $this->processRepo->delete($process);
+       }
+}
index 6865a4d25a34cea7f2492fe65cc22212289af3ac..e3718824658e695a64c53c8fb7c1784b43bf6ca9 100644 (file)
@@ -39,6 +39,7 @@ Commands:
        createdoxygen          Generate Doxygen headers
        daemon                 Interact with the Friendica daemon
        jetstream              Interact with the Jetstream daemon
+       worker                 Start worker process
        dbstructure            Do database updates
        docbloxerrorchecker    Check the file tree for DocBlox errors
        extract                Generate translation string file for the Friendica project (deprecated)
@@ -79,6 +80,7 @@ HELP;
                'createdoxygen'                     => Friendica\Console\CreateDoxygen::class,
                'daemon'                            => Friendica\Console\Daemon::class,
                'jetstream'                         => Friendica\Console\JetstreamDaemon::class,
+               'worker'                            => Friendica\Console\Worker::class,
                'docbloxerrorchecker'               => Friendica\Console\DocBloxErrorChecker::class,
                'dbstructure'                       => Friendica\Console\DatabaseStructure::class,
                'extract'                           => Friendica\Console\Extract::class,