diff --git a/mod/worker.php b/mod/worker.php index d6ecbecc26..056416e799 100644 --- a/mod/worker.php +++ b/mod/worker.php @@ -29,7 +29,7 @@ function worker_init($a){ return; } - $a->start_process(); + Worker::startProcess(); logger("Front end worker started: ".getmypid()); @@ -50,7 +50,7 @@ function worker_init($a){ Worker::unclaimProcess(); - $a->end_process(); + Worker::endProcess(); logger("Front end worker ended: ".getmypid()); diff --git a/scripts/worker.php b/scripts/worker.php index a3738038b1..d69b089aec 100755 --- a/scripts/worker.php +++ b/scripts/worker.php @@ -57,7 +57,7 @@ Worker::processQueue($run_cron); Worker::unclaimProcess(); -$a->end_process(); +Worker::endProcess(); killme(); diff --git a/src/App.php b/src/App.php index fdd928ba1a..62b5f12e2c 100644 --- a/src/App.php +++ b/src/App.php @@ -2,20 +2,17 @@ namespace Friendica; -use Friendica\Core\System; use Friendica\Core\Cache; use Friendica\Core\Config; use Friendica\Core\PConfig; -use Friendica\Database\DBM; - -use dba; +use Friendica\Core\System; use Detection\MobileDetect; use Exception; require_once 'boot.php'; -require_once 'include/dba.php'; +require_once 'include/text.php'; /** * @@ -695,49 +692,6 @@ class App { $this->callstack[$value][$callstack] += (float) $duration; } - /** - * @brief Log active processes into the "process" table - */ - function start_process() { - $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1); - - $command = basename($trace[0]['file']); - - $this->remove_inactive_processes(); - - dba::transaction(); - - $r = q('SELECT `pid` FROM `process` WHERE `pid` = %d', intval(getmypid())); - if (!DBM::is_result($r)) { - dba::insert('process', ['pid' => getmypid(), 'command' => $command, 'created' => datetime_convert()]); - } - dba::commit(); - } - - /** - * @brief Remove inactive processes - */ - function remove_inactive_processes() { - dba::transaction(); - - $r = q('SELECT `pid` FROM `process`'); - if (DBM::is_result($r)) { - foreach ($r AS $process) { - if (!posix_kill($process['pid'], 0)) { - dba::delete('process', ['pid' => $process['pid']]); - } - } - } - dba::commit(); - } - - /** - * @brief Remove the active process from the "process" table - */ - function end_process() { - dba::delete('process', ['pid' => getmypid()]); - } - function get_useragent() { return FRIENDICA_PLATFORM . " '" . diff --git a/src/Core/Worker.php b/src/Core/Worker.php index da7425a616..478f01a967 100644 --- a/src/Core/Worker.php +++ b/src/Core/Worker.php @@ -4,11 +4,10 @@ */ namespace Friendica\Core; -use Friendica\App; -use Friendica\Core\System; use Friendica\Core\Config; -use Friendica\Core\Worker; +use Friendica\Core\System; use Friendica\Database\DBM; +use Friendica\Model\Process; use Friendica\Util\Lock; use dba; @@ -50,7 +49,7 @@ class Worker } // We now start the process. This is done after the load check since this could increase the load. - $a->start_process(); + self::startProcess(); // Kill stale processes every 5 minutes $last_cleanup = Config::get('system', 'poller_last_cleaned', 0); @@ -915,7 +914,7 @@ class Worker if (self::tooMuchWorkers()) { // Cleaning dead processes self::killStaleWorkers(); - get_app()->remove_inactive_processes(); + Process::deleteInactive(); return; } @@ -1092,4 +1091,31 @@ class Worker return true; } + + /** + * Log active processes into the "process" table + * + * @brief Log active processes into the "process" table + */ + public static function startProcess() + { + $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1); + + $command = basename($trace[0]['file']); + + Process::deleteInactive(); + + Process::insert($command); + } + + /** + * Remove the active process from the "process" table + * + * @brief Remove the active process from the "process" table + * @return bool + */ + public static function endProcess() + { + return Process::deleteByPid(); + } } diff --git a/src/Model/Process.php b/src/Model/Process.php new file mode 100644 index 0000000000..6b2d2fb64b --- /dev/null +++ b/src/Model/Process.php @@ -0,0 +1,68 @@ + getmypid()])) { + $return = dba::insert('process', ['pid' => $pid, 'command' => $command, 'created' => datetime_convert()]); + } + + dba::commit(); + + return $return; + } + + /** + * Remove a process row by pid. If the pid parameter is omitted, we use the current pid + * + * @param string $pid + * @return bool + */ + public static function deleteByPid($pid = null) + { + if ($pid === null) { + $pid = getmypid(); + } + + return dba::delete('process', ['pid' => $pid]); + } + + /** + * Clean the process table of inactive physical processes + */ + public static function deleteInactive() + { + dba::transaction(); + + $processes = dba::select('process', ['pid']); + while($process = dba::fetch($processes)) { + if (!posix_kill($process['pid'], 0)) { + self::deleteByPid($process['pid']); + } + } + + dba::commit(); + } +}