Move process functions to Model\Process

- Add start|endProcess functions to Core\Worker
This commit is contained in:
Hypolite Petovan 2018-01-15 19:08:28 -05:00
parent b699637ab7
commit 78ac7afe9c
5 changed files with 104 additions and 56 deletions

View file

@ -29,7 +29,7 @@ function worker_init($a){
return; return;
} }
$a->start_process(); Worker::startProcess();
logger("Front end worker started: ".getmypid()); logger("Front end worker started: ".getmypid());
@ -50,7 +50,7 @@ function worker_init($a){
Worker::unclaimProcess(); Worker::unclaimProcess();
$a->end_process(); Worker::endProcess();
logger("Front end worker ended: ".getmypid()); logger("Front end worker ended: ".getmypid());

View file

@ -57,7 +57,7 @@ Worker::processQueue($run_cron);
Worker::unclaimProcess(); Worker::unclaimProcess();
$a->end_process(); Worker::endProcess();
killme(); killme();

View file

@ -2,20 +2,17 @@
namespace Friendica; namespace Friendica;
use Friendica\Core\System;
use Friendica\Core\Cache; use Friendica\Core\Cache;
use Friendica\Core\Config; use Friendica\Core\Config;
use Friendica\Core\PConfig; use Friendica\Core\PConfig;
use Friendica\Database\DBM; use Friendica\Core\System;
use dba;
use Detection\MobileDetect; use Detection\MobileDetect;
use Exception; use Exception;
require_once 'boot.php'; 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; $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() { function get_useragent() {
return return
FRIENDICA_PLATFORM . " '" . FRIENDICA_PLATFORM . " '" .

View file

@ -4,11 +4,10 @@
*/ */
namespace Friendica\Core; namespace Friendica\Core;
use Friendica\App;
use Friendica\Core\System;
use Friendica\Core\Config; use Friendica\Core\Config;
use Friendica\Core\Worker; use Friendica\Core\System;
use Friendica\Database\DBM; use Friendica\Database\DBM;
use Friendica\Model\Process;
use Friendica\Util\Lock; use Friendica\Util\Lock;
use dba; 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. // 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 // Kill stale processes every 5 minutes
$last_cleanup = Config::get('system', 'poller_last_cleaned', 0); $last_cleanup = Config::get('system', 'poller_last_cleaned', 0);
@ -915,7 +914,7 @@ class Worker
if (self::tooMuchWorkers()) { if (self::tooMuchWorkers()) {
// Cleaning dead processes // Cleaning dead processes
self::killStaleWorkers(); self::killStaleWorkers();
get_app()->remove_inactive_processes(); Process::deleteInactive();
return; return;
} }
@ -1092,4 +1091,31 @@ class Worker
return true; 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();
}
} }

68
src/Model/Process.php Normal file
View file

@ -0,0 +1,68 @@
<?php
/**
* @file src/Model/Process.php
*/
namespace Friendica\Model;
use Friendica\BaseObject;
use dba;
require_once 'include/dba.php';
/**
* @brief functions for interacting with a process
*/
class Process extends BaseObject
{
/**
* Insert a new process row. If the pid parameter is omitted, we use the current pid
*
* @param string $command
* @param string $pid
* @return bool
*/
public static function insert($command, $pid = null)
{
dba::transaction();
if (!dba::exists('process', ['pid' => 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();
}
}