Merge pull request #8209 from nupplaphil/task/mod_worker

Move mod/worker to src\Module\Worker
This commit is contained in:
Hypolite Petovan 2020-01-31 18:12:36 -05:00 committed by GitHub
commit dad530802f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 68 additions and 65 deletions

View file

@ -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();
}

67
src/Module/Worker.php Normal file
View file

@ -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.');
}
}

View file

@ -271,4 +271,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]],
];