From 17389de26165f10a9bebeccb725e987ab6574a9d Mon Sep 17 00:00:00 2001 From: nupplaPhil Date: Fri, 31 Jan 2020 22:38:49 +0100 Subject: [PATCH] Move mod/worker to src\Module\Worker --- mod/worker.php | 65 -------------------------------------- src/Module/Worker.php | 67 ++++++++++++++++++++++++++++++++++++++++ static/routes.config.php | 1 + 3 files changed, 68 insertions(+), 65 deletions(-) delete mode 100644 mod/worker.php create mode 100644 src/Module/Worker.php diff --git a/mod/worker.php b/mod/worker.php deleted file mode 100644 index 2ebd463a76..0000000000 --- a/mod/worker.php +++ /dev/null @@ -1,65 +0,0 @@ -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 index 0000000000..390e25980d --- /dev/null +++ b/src/Module/Worker.php @@ -0,0 +1,67 @@ +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.'); + } +} diff --git a/static/routes.config.php b/static/routes.config.php index 4aad69d8c7..2eca8e1a6c 100644 --- a/static/routes.config.php +++ b/static/routes.config.php @@ -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]], ];