Merge pull request #9223 from nupplaphil/task/process_class
Cleanup Process classes
This commit is contained in:
commit
280458fb65
9 changed files with 168 additions and 64 deletions
|
@ -23,6 +23,7 @@
|
||||||
|
|
||||||
use Dice\Dice;
|
use Dice\Dice;
|
||||||
use Friendica\App;
|
use Friendica\App;
|
||||||
|
use Friendica\Core\Process;
|
||||||
use Friendica\Core\Update;
|
use Friendica\Core\Update;
|
||||||
use Friendica\Core\Worker;
|
use Friendica\Core\Worker;
|
||||||
use Friendica\DI;
|
use Friendica\DI;
|
||||||
|
@ -76,4 +77,4 @@ Worker::processQueue($run_cron);
|
||||||
|
|
||||||
Worker::unclaimProcess();
|
Worker::unclaimProcess();
|
||||||
|
|
||||||
Worker::endProcess();
|
DI::process()->end();
|
||||||
|
|
|
@ -23,6 +23,7 @@ namespace Friendica\Core;
|
||||||
|
|
||||||
use Friendica\App;
|
use Friendica\App;
|
||||||
use Friendica\Core\Config\IConfig;
|
use Friendica\Core\Config\IConfig;
|
||||||
|
use Friendica\Model;
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -56,12 +57,48 @@ class Process
|
||||||
*/
|
*/
|
||||||
private $basePath;
|
private $basePath;
|
||||||
|
|
||||||
public function __construct(LoggerInterface $logger, App\Mode $mode, IConfig $config, string $basepath)
|
/** @var Model\Process */
|
||||||
|
private $processModel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Process ID of this process
|
||||||
|
*
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
|
private $pid;
|
||||||
|
|
||||||
|
public function __construct(LoggerInterface $logger, App\Mode $mode, IConfig $config, Model\Process $processModel, string $basepath, int $pid)
|
||||||
{
|
{
|
||||||
$this->logger = $logger;
|
$this->logger = $logger;
|
||||||
$this->mode = $mode;
|
$this->mode = $mode;
|
||||||
$this->config = $config;
|
$this->config = $config;
|
||||||
$this->basePath = $basepath;
|
$this->basePath = $basepath;
|
||||||
|
$this->processModel = $processModel;
|
||||||
|
$this->pid = $pid;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Log active processes into the "process" table
|
||||||
|
*/
|
||||||
|
public function start()
|
||||||
|
{
|
||||||
|
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1);
|
||||||
|
|
||||||
|
$command = basename($trace[0]['file']);
|
||||||
|
|
||||||
|
$this->processModel->deleteInactive();
|
||||||
|
$this->processModel->insert($command, $this->pid);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove the active process from the "process" table
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
public function end()
|
||||||
|
{
|
||||||
|
return $this->processModel->deleteByPid($this->pid);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -22,6 +22,7 @@
|
||||||
namespace Friendica\Core;
|
namespace Friendica\Core;
|
||||||
|
|
||||||
use Friendica\Core;
|
use Friendica\Core;
|
||||||
|
use Friendica\Core\Process as ProcessAlias;
|
||||||
use Friendica\Database\DBA;
|
use Friendica\Database\DBA;
|
||||||
use Friendica\DI;
|
use Friendica\DI;
|
||||||
use Friendica\Model\Process;
|
use Friendica\Model\Process;
|
||||||
|
@ -72,7 +73,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.
|
||||||
self::startProcess();
|
DI::process()->start();
|
||||||
|
|
||||||
// Kill stale processes every 5 minutes
|
// Kill stale processes every 5 minutes
|
||||||
$last_cleanup = DI::config()->get('system', 'worker_last_cleaned', 0);
|
$last_cleanup = DI::config()->get('system', 'worker_last_cleaned', 0);
|
||||||
|
@ -1092,7 +1093,7 @@ class Worker
|
||||||
if (self::tooMuchWorkers()) {
|
if (self::tooMuchWorkers()) {
|
||||||
// Cleaning dead processes
|
// Cleaning dead processes
|
||||||
self::killStaleWorkers();
|
self::killStaleWorkers();
|
||||||
Process::deleteInactive();
|
DI::modelProcess()->deleteInactive();
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -1171,7 +1172,7 @@ class Worker
|
||||||
$args = ['no_cron' => !$do_cron];
|
$args = ['no_cron' => !$do_cron];
|
||||||
|
|
||||||
$a = DI::app();
|
$a = DI::app();
|
||||||
$process = new Core\Process(DI::logger(), DI::mode(), DI::config(), $a->getBasePath());
|
$process = new Core\Process(DI::logger(), DI::mode(), DI::config(), DI::modelProcess(), $a->getBasePath(), getmypid());
|
||||||
$process->run($command, $args);
|
$process->run($command, $args);
|
||||||
|
|
||||||
// after spawning we have to remove the flag.
|
// after spawning we have to remove the flag.
|
||||||
|
@ -1360,31 +1361,6 @@ class Worker
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 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
|
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
* @throws \Exception
|
|
||||||
*/
|
|
||||||
public static function endProcess()
|
|
||||||
{
|
|
||||||
return Process::deleteByPid();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the flag if some job is waiting
|
* Set the flag if some job is waiting
|
||||||
*
|
*
|
||||||
|
|
|
@ -314,6 +314,13 @@ abstract class DI
|
||||||
//
|
//
|
||||||
// "Model" namespace instances
|
// "Model" namespace instances
|
||||||
//
|
//
|
||||||
|
/**
|
||||||
|
* @return Model\Process
|
||||||
|
*/
|
||||||
|
public static function modelProcess()
|
||||||
|
{
|
||||||
|
return self::$dice->create(Model\Process::class);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Model\User\Cookie
|
* @return Model\User\Cookie
|
||||||
|
|
|
@ -21,7 +21,7 @@
|
||||||
|
|
||||||
namespace Friendica\Model;
|
namespace Friendica\Model;
|
||||||
|
|
||||||
use Friendica\Database\DBA;
|
use Friendica\Database\Database;
|
||||||
use Friendica\Util\DateTimeFormat;
|
use Friendica\Util\DateTimeFormat;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -29,29 +29,33 @@ use Friendica\Util\DateTimeFormat;
|
||||||
*/
|
*/
|
||||||
class Process
|
class Process
|
||||||
{
|
{
|
||||||
|
/** @var Database */
|
||||||
|
private $dba;
|
||||||
|
|
||||||
|
public function __construct(Database $dba)
|
||||||
|
{
|
||||||
|
$this->dba = $dba;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Insert a new process row. If the pid parameter is omitted, we use the current pid
|
* Insert a new process row. If the pid parameter is omitted, we use the current pid
|
||||||
*
|
*
|
||||||
* @param string $command
|
* @param string $command
|
||||||
* @param string $pid
|
* @param int $pid The process id to insert
|
||||||
* @return bool
|
* @return bool
|
||||||
* @throws \Exception
|
* @throws \Exception
|
||||||
*/
|
*/
|
||||||
public static function insert($command, $pid = null)
|
public function insert(string $command, int $pid)
|
||||||
{
|
{
|
||||||
$return = true;
|
$return = true;
|
||||||
|
|
||||||
if (is_null($pid)) {
|
$this->dba->transaction();
|
||||||
$pid = getmypid();
|
|
||||||
|
if (!$this->dba->exists('process', ['pid' => $pid])) {
|
||||||
|
$return = $this->dba->insert('process', ['pid' => $pid, 'command' => $command, 'created' => DateTimeFormat::utcNow()]);
|
||||||
}
|
}
|
||||||
|
|
||||||
DBA::transaction();
|
$this->dba->commit();
|
||||||
|
|
||||||
if (!DBA::exists('process', ['pid' => $pid])) {
|
|
||||||
$return = DBA::insert('process', ['pid' => $pid, 'command' => $command, 'created' => DateTimeFormat::utcNow()]);
|
|
||||||
}
|
|
||||||
|
|
||||||
DBA::commit();
|
|
||||||
|
|
||||||
return $return;
|
return $return;
|
||||||
}
|
}
|
||||||
|
@ -59,33 +63,29 @@ class Process
|
||||||
/**
|
/**
|
||||||
* Remove a process row by pid. If the pid parameter is omitted, we use the current pid
|
* Remove a process row by pid. If the pid parameter is omitted, we use the current pid
|
||||||
*
|
*
|
||||||
* @param string $pid
|
* @param int $pid The pid to delete
|
||||||
* @return bool
|
* @return bool
|
||||||
* @throws \Exception
|
* @throws \Exception
|
||||||
*/
|
*/
|
||||||
public static function deleteByPid($pid = null)
|
public function deleteByPid(int $pid)
|
||||||
{
|
{
|
||||||
if ($pid === null) {
|
return $this->dba->delete('process', ['pid' => $pid]);
|
||||||
$pid = getmypid();
|
|
||||||
}
|
|
||||||
|
|
||||||
return DBA::delete('process', ['pid' => $pid]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clean the process table of inactive physical processes
|
* Clean the process table of inactive physical processes
|
||||||
*/
|
*/
|
||||||
public static function deleteInactive()
|
public function deleteInactive()
|
||||||
{
|
{
|
||||||
DBA::transaction();
|
$this->dba->transaction();
|
||||||
|
|
||||||
$processes = DBA::select('process', ['pid']);
|
$processes = $this->dba->select('process', ['pid']);
|
||||||
while($process = DBA::fetch($processes)) {
|
while($process = $this->dba->fetch($processes)) {
|
||||||
if (!posix_kill($process['pid'], 0)) {
|
if (!posix_kill($process['pid'], 0)) {
|
||||||
self::deleteByPid($process['pid']);
|
$this->deleteByPid($process['pid']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
DBA::close($processes);
|
$this->dba->close($processes);
|
||||||
DBA::commit();
|
$this->dba->commit();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,6 +22,7 @@
|
||||||
namespace Friendica\Module;
|
namespace Friendica\Module;
|
||||||
|
|
||||||
use Friendica\BaseModule;
|
use Friendica\BaseModule;
|
||||||
|
use Friendica\Core\Process;
|
||||||
use Friendica\Core\System;
|
use Friendica\Core\System;
|
||||||
use Friendica\Core\Worker as WorkerCore;
|
use Friendica\Core\Worker as WorkerCore;
|
||||||
use Friendica\Database\DBA;
|
use Friendica\Database\DBA;
|
||||||
|
@ -57,7 +58,7 @@ class Worker extends BaseModule
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
WorkerCore::startProcess();
|
DI::process()->start();
|
||||||
|
|
||||||
DI::logger()->notice('Front end worker started.', ['pid' => getmypid()]);
|
DI::logger()->notice('Front end worker started.', ['pid' => getmypid()]);
|
||||||
|
|
||||||
|
@ -79,7 +80,7 @@ class Worker extends BaseModule
|
||||||
|
|
||||||
WorkerCore::unclaimProcess();
|
WorkerCore::unclaimProcess();
|
||||||
|
|
||||||
WorkerCore::endProcess();
|
DI::process()->end();
|
||||||
|
|
||||||
System::httpExit(200, 'Frontend worker stopped.');
|
System::httpExit(200, 'Frontend worker stopped.');
|
||||||
}
|
}
|
||||||
|
|
|
@ -187,6 +187,7 @@ return [
|
||||||
Process::class => [
|
Process::class => [
|
||||||
'constructParams' => [
|
'constructParams' => [
|
||||||
[Dice::INSTANCE => '$basepath'],
|
[Dice::INSTANCE => '$basepath'],
|
||||||
|
getmypid(),
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
App\Router::class => [
|
App\Router::class => [
|
||||||
|
|
81
tests/src/Model/ProcessTest.php
Normal file
81
tests/src/Model/ProcessTest.php
Normal file
|
@ -0,0 +1,81 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Friendica\Test\src\Model;
|
||||||
|
|
||||||
|
use Friendica\Factory\ConfigFactory;
|
||||||
|
use Friendica\Model\Process;
|
||||||
|
use Friendica\Test\DatabaseTest;
|
||||||
|
use Friendica\Test\Util\Database\StaticDatabase;
|
||||||
|
use Friendica\Test\Util\VFSTrait;
|
||||||
|
use Friendica\Util\ConfigFileLoader;
|
||||||
|
use Friendica\Util\Profiler;
|
||||||
|
use Psr\Log\NullLogger;
|
||||||
|
|
||||||
|
class ProcessTest extends DatabaseTest
|
||||||
|
{
|
||||||
|
use VFSTrait;
|
||||||
|
|
||||||
|
/** @var StaticDatabase */
|
||||||
|
private $dba;
|
||||||
|
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
$this->setUpVfsDir();
|
||||||
|
|
||||||
|
$this->logger = new NullLogger();
|
||||||
|
|
||||||
|
$profiler = \Mockery::mock(Profiler::class);
|
||||||
|
$profiler->shouldReceive('saveTimestamp')->withAnyArgs()->andReturn(true);
|
||||||
|
|
||||||
|
// load real config to avoid mocking every config-entry which is related to the Database class
|
||||||
|
$configFactory = new ConfigFactory();
|
||||||
|
$loader = new ConfigFileLoader($this->root->url());
|
||||||
|
$configCache = $configFactory->createCache($loader);
|
||||||
|
|
||||||
|
$this->dba = new StaticDatabase($configCache, $profiler, $this->logger);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testInsertDelete()
|
||||||
|
{
|
||||||
|
$process = new Process($this->dba);
|
||||||
|
|
||||||
|
$this->assertEquals(0, $this->dba->count('process'));
|
||||||
|
$process->insert('test', 1);
|
||||||
|
$process->insert('test2', 2);
|
||||||
|
$process->insert('test3', 3);
|
||||||
|
|
||||||
|
$this->assertEquals(3, $this->dba->count('process'));
|
||||||
|
|
||||||
|
$this->assertEquals([
|
||||||
|
['command' => 'test']
|
||||||
|
], $this->dba->selectToArray('process', ['command'], ['pid' => 1]));
|
||||||
|
|
||||||
|
$process->deleteByPid(1);
|
||||||
|
|
||||||
|
$this->assertEmpty($this->dba->selectToArray('process', ['command'], ['pid' => 1]));
|
||||||
|
|
||||||
|
$this->assertEquals(2, $this->dba->count('process'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testDoubleInsert()
|
||||||
|
{
|
||||||
|
$process = new Process($this->dba);
|
||||||
|
|
||||||
|
$process->insert('test', 1);
|
||||||
|
|
||||||
|
// double insert doesn't work
|
||||||
|
$process->insert('test23', 1);
|
||||||
|
|
||||||
|
$this->assertEquals([['command' => 'test']], $this->dba->selectToArray('process', ['command'], ['pid' => 1]));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testWrongDelete()
|
||||||
|
{
|
||||||
|
$process = new Process($this->dba);
|
||||||
|
|
||||||
|
// Just ignore wrong deletes, no execution is thrown
|
||||||
|
$process->deleteByPid(-1);
|
||||||
|
}
|
||||||
|
}
|
|
@ -19,16 +19,16 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace Friendica\Testsrc\Model\User;
|
namespace Friendica\Test\src\Model\User;
|
||||||
|
|
||||||
use Friendica\App\BaseURL;
|
use Friendica\App\BaseURL;
|
||||||
use Friendica\Core\Config\IConfig;
|
use Friendica\Core\Config\IConfig;
|
||||||
use Friendica\Model\User\Cookie;
|
use Friendica\Model\User\Cookie;
|
||||||
use Friendica\Test\DatabaseTest;
|
use Friendica\Test\MockedTest;
|
||||||
use Friendica\Test\Util\StaticCookie;
|
use Friendica\Test\Util\StaticCookie;
|
||||||
use Mockery\MockInterface;
|
use Mockery\MockInterface;
|
||||||
|
|
||||||
class CookieTest extends DatabaseTest
|
class CookieTest extends MockedTest
|
||||||
{
|
{
|
||||||
/** @var MockInterface|IConfig */
|
/** @var MockInterface|IConfig */
|
||||||
private $config;
|
private $config;
|
||||||
|
|
Loading…
Reference in a new issue