]> git.mxchange.org Git - friendica.git/commitdiff
Move mod/worker to src\Module\Worker
authornupplaPhil <admin+github@philipp.info>
Fri, 31 Jan 2020 21:38:49 +0000 (22:38 +0100)
committernupplaPhil <admin+github@philipp.info>
Fri, 31 Jan 2020 21:38:49 +0000 (22:38 +0100)
mod/worker.php [deleted file]
src/Module/Worker.php [new file with mode: 0644]
static/routes.config.php

diff --git a/mod/worker.php b/mod/worker.php
deleted file mode 100644 (file)
index 2ebd463..0000000
+++ /dev/null
@@ -1,65 +0,0 @@
-<?php
-/**
- * @file mod/worker.php
- * Module for running the worker as frontend process
- */
-
-use Friendica\Core\Logger;
-use Friendica\Core\Worker;
-use Friendica\Database\DBA;
-use Friendica\DI;
-use Friendica\Util\DateTimeFormat;
-
-function worker_init()
-{
-
-       if (!DI::config()->get("system", "frontend_worker")) {
-               return;
-       }
-
-       // Ensure that all "strtotime" operations do run timezone independent
-       date_default_timezone_set('UTC');
-
-       // We don't need the following lines if we can execute background jobs.
-       // So we just wake up the worker if it sleeps.
-       if (function_exists("proc_open")) {
-               Worker::executeIfIdle();
-               return;
-       }
-
-       Worker::clearProcesses();
-
-       $workers = q("SELECT COUNT(*) AS `processes` FROM `process` WHERE `command` = 'worker.php'");
-
-       if ($workers[0]["processes"] > DI::config()->get("system", "worker_queues", 4)) {
-               return;
-       }
-
-       Worker::startProcess();
-
-       Logger::log("Front end worker started: ".getmypid());
-
-       Worker::callWorker();
-
-       if ($r = Worker::workerProcess()) {
-               // On most configurations this parameter wouldn't have any effect.
-               // But since it doesn't destroy anything, we just try to get more execution time in any way.
-               set_time_limit(0);
-
-               $fields = ['executed' => DateTimeFormat::utcNow(), 'pid' => getmypid(), 'done' => false];
-               $condition =  ['id' => $r[0]["id"], 'pid' => 0];
-               if (DBA::update('workerqueue', $fields, $condition)) {
-                       Worker::execute($r[0]);
-               }
-       }
-
-       Worker::callWorker();
-
-       Worker::unclaimProcess();
-
-       Worker::endProcess();
-
-       Logger::log("Front end worker ended: ".getmypid());
-
-       exit();
-}
diff --git a/src/Module/Worker.php b/src/Module/Worker.php
new file mode 100644 (file)
index 0000000..390e259
--- /dev/null
@@ -0,0 +1,67 @@
+<?php
+
+namespace Friendica\Module;
+
+use Friendica\BaseModule;
+use Friendica\Core\System;
+use Friendica\Core\Worker as WorkerCore;
+use Friendica\Database\DBA;
+use Friendica\DI;
+use Friendica\Util\DateTimeFormat;
+
+/**
+ * Module for starting the backend worker through a frontend call
+ */
+class Worker extends BaseModule
+{
+       public static function rawContent(array $parameters = [])
+       {
+               if (!DI::config()->get("system", "frontend_worker")) {
+                       return;
+               }
+
+               // Ensure that all "strtotime" operations do run timezone independent
+               date_default_timezone_set('UTC');
+
+               // We don't need the following lines if we can execute background jobs.
+               // So we just wake up the worker if it sleeps.
+               if (function_exists("proc_open")) {
+                       WorkerCore::executeIfIdle();
+                       return;
+               }
+
+               WorkerCore::clearProcesses();
+
+               $workers = DBA::count('process', ['command' => 'worker.php']);
+
+               if ($workers > DI::config()->get("system", "worker_queues", 4)) {
+                       return;
+               }
+
+               WorkerCore::startProcess();
+
+               DI::logger()->notice('Front end worker started.', ['pid' => getmypid()]);
+
+               WorkerCore::callWorker();
+
+               if ($r = WorkerCore::workerProcess()) {
+                       // On most configurations this parameter wouldn't have any effect.
+                       // But since it doesn't destroy anything, we just try to get more execution time in any way.
+                       set_time_limit(0);
+
+                       $fields = ['executed' => DateTimeFormat::utcNow(), 'pid' => getmypid(), 'done' => false];
+                       $condition =  ['id' => $r[0]["id"], 'pid' => 0];
+                       if (DBA::update('workerqueue', $fields, $condition)) {
+                               WorkerCore::execute($r[0]);
+                       }
+               }
+
+               WorkerCore::callWorker();
+
+               WorkerCore::unclaimProcess();
+
+               WorkerCore::endProcess();
+
+               System::httpExit(200, 'Frontend worker stopped.');
+       }
+}
index 4aad69d8c7f5ec8e2162d973845f0c65d5cf6010..2eca8e1a6caa81ff86ae5604aa1b96b682283d65 100644 (file)
@@ -268,4 +268,5 @@ return [
        '/viewsrc/{item:\d+}'            => [Module\Debug\ItemBody::class,        [R::GET]],
        '/webfinger'                     => [Module\Debug\WebFinger::class,       [R::GET]],
        '/xrd'                           => [Module\Xrd::class,                   [R::GET]],
+       '/worker'                        => [Module\Worker::class,                [R::GET]],
 ];