use Dice\Dice;
use Friendica\App;
+use Friendica\Core\Process;
use Friendica\Core\Update;
use Friendica\Core\Worker;
use Friendica\DI;
Worker::unclaimProcess();
-Worker::endProcess();
+DI::process()->end();
use Friendica\App;
use Friendica\Core\Config\IConfig;
+use Friendica\Model;
use Psr\Log\LoggerInterface;
/**
*/
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)
{
- $this->logger = $logger;
- $this->mode = $mode;
- $this->config = $config;
+ $this->logger = $logger;
+ $this->mode = $mode;
+ $this->config = $config;
$this->basePath = $basepath;
+ $this->processModel = $processModel;
+ $this->pid = getmypid();
+ }
+
+ /**
+ * 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);
}
/**
namespace Friendica\Core;
use Friendica\Core;
+use Friendica\Core\Process as ProcessAlias;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Model\Process;
}
// 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
$last_cleanup = DI::config()->get('system', 'worker_last_cleaned', 0);
if (self::tooMuchWorkers()) {
// Cleaning dead processes
self::killStaleWorkers();
- Process::deleteInactive();
+ DI::modelProcess()->deleteInactive();
return;
}
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
*
//
// "Model" namespace instances
//
+ /**
+ * @return Model\Process
+ */
+ public static function modelProcess()
+ {
+ return self::$dice->create(Model\Process::class);
+ }
/**
* @return Model\User\Cookie
namespace Friendica\Model;
-use Friendica\Database\DBA;
+use Friendica\Database\Database;
use Friendica\Util\DateTimeFormat;
/**
*/
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
*
* @param string $command
- * @param string $pid
+ * @param int $pid The process id to insert
* @return bool
* @throws \Exception
*/
- public static function insert($command, $pid = null)
+ public function insert(string $command, int $pid)
{
$return = true;
- if (is_null($pid)) {
- $pid = getmypid();
- }
-
- DBA::transaction();
+ $this->dba->transaction();
- if (!DBA::exists('process', ['pid' => $pid])) {
- $return = DBA::insert('process', ['pid' => $pid, 'command' => $command, 'created' => DateTimeFormat::utcNow()]);
+ if (!$this->dba->exists('process', ['pid' => $pid])) {
+ $return = $this->dba->insert('process', ['pid' => $pid, 'command' => $command, 'created' => DateTimeFormat::utcNow()]);
}
- DBA::commit();
+ $this->dba->commit();
return $return;
}
/**
* 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
* @throws \Exception
*/
- public static function deleteByPid($pid = null)
+ public function deleteByPid(int $pid)
{
- if ($pid === null) {
- $pid = getmypid();
- }
-
- return DBA::delete('process', ['pid' => $pid]);
+ return $this->dba->delete('process', ['pid' => $pid]);
}
/**
* 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']);
- while($process = DBA::fetch($processes)) {
+ $processes = $this->dba->select('process', ['pid']);
+ while($process = $this->dba->fetch($processes)) {
if (!posix_kill($process['pid'], 0)) {
- self::deleteByPid($process['pid']);
+ $this->deleteByPid($process['pid']);
}
}
- DBA::close($processes);
- DBA::commit();
+ $this->dba->close($processes);
+ $this->dba->commit();
}
}
namespace Friendica\Module;
use Friendica\BaseModule;
+use Friendica\Core\Process;
use Friendica\Core\System;
use Friendica\Core\Worker as WorkerCore;
use Friendica\Database\DBA;
return;
}
- WorkerCore::startProcess();
+ DI::process()->start();
DI::logger()->notice('Front end worker started.', ['pid' => getmypid()]);
WorkerCore::unclaimProcess();
- WorkerCore::endProcess();
+ DI::process()->end();
System::httpExit(200, 'Frontend worker stopped.');
}