1
0
Fork 0

Refactor Process for new paradigm

This commit is contained in:
Philipp Holzer 2021-10-24 20:43:59 +02:00
commit 38f70cc55a
Signed by: nupplaPhil
GPG key ID: 24A7501396EB5432
14 changed files with 456 additions and 491 deletions

View file

@ -0,0 +1,46 @@
<?php
namespace Friendica\Core\Worker\Entity;
use DateTime;
use Friendica\BaseEntity;
/**
* @property-read int $pid
* @property-read string $command
* @property-read DateTime $created
*/
class Process extends BaseEntity
{
/** @var int */
protected $pid;
/** @var string */
protected $command;
/** @var DateTime */
protected $created;
/**
* @param int $pid
* @param string $command
* @param DateTime $created
*/
public function __construct(int $pid, string $command, DateTime $created)
{
$this->pid = $pid;
$this->command = $command;
$this->created = $created;
}
/**
* Returns a new Process with the given PID
*
* @param int $pid
*
* @return $this
* @throws \Exception
*/
public function withPid(int $pid): Process
{
return new static($pid, $this->command, new DateTime('now', new \DateTimeZone('URC')));
}
}

View file

@ -0,0 +1,13 @@
<?php
namespace Friendica\Core\Worker\Exception;
use Throwable;
class ProcessPersistenceException extends \RuntimeException
{
public function __construct($message = "", Throwable $previous = null)
{
parent::__construct($message, 500, $previous);
}
}

View file

@ -0,0 +1,38 @@
<?php
namespace Friendica\Core\Worker\Factory;
use Friendica\BaseFactory;
use Friendica\Capabilities\ICanCreateFromTableRow;
use Friendica\Core\Worker\Entity;
class Process extends BaseFactory implements ICanCreateFromTableRow
{
public function createFromTableRow(array $row): Entity\Process
{
return new Entity\Process(
$row['pid'],
$row['command'],
new \DateTime($row['created'] ?? 'now', new \DateTimeZone('UTC'))
);
}
/**
* Creates a new process entry for a given PID
*
* @param int $pid
*
* @return Entity\Process
*/
public function create(int $pid): Entity\Process
{
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1);
$command = basename($trace[0]['file']);
return $this->createFromTableRow([
'pid' => $pid,
'command' => $command,
]);
}
}

View file

@ -0,0 +1,118 @@
<?php
/**
* @copyright Copyright (C) 2010-2021, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Core\Worker\Repository;
use Friendica\BaseRepository;
use Friendica\Core\Worker\Exception\ProcessPersistenceException;
use Friendica\Database\Database;
use Friendica\Util\DateTimeFormat;
use Friendica\Core\Worker\Factory;
use Friendica\Core\Worker\Entity;
use Psr\Log\LoggerInterface;
/**
* functions for interacting with a process
*/
class Process extends BaseRepository
{
protected static $table_name = 'process';
/** @var Factory\Process */
protected $factory;
public function __construct(Database $database, LoggerInterface $logger, Factory\Process $factory)
{
parent::__construct($database, $logger, $factory);
}
/**
* Starts and Returns the process for a given PID
*
* @param int $pid
*
* @return Entity\Process
*/
public function create(int $pid): Entity\Process
{
// Cleanup inactive process
$this->deleteInactive();
try {
$this->db->transaction();
$newProcess = $this->factory->create($pid);
if (!$this->db->exists('process', ['pid' => $pid])) {
if (!$this->db->insert(static::$table_name, [
'pid' => $newProcess->pid,
'command' => $newProcess->command,
'created' => $newProcess->created->format(DateTimeFormat::MYSQL)
])) {
throw new ProcessPersistenceException(sprintf('The process with PID %s already exists.', $pid));
}
}
$result = $this->_selectOne(['pid' => $pid]);
$this->db->commit();
return $result;
} catch (\Exception $exception) {
throw new ProcessPersistenceException(sprintf('Cannot save process with PID %s.', $pid), $exception);
}
}
public function delete(Entity\Process $process)
{
try {
if (!$this->db->delete(static::$table_name, [
'pid' => $process->pid
])) {
throw new ProcessPersistenceException(sprintf('The process with PID %s doesn\'t exists.', $process->pi));
}
} catch (\Exception $exception) {
throw new ProcessPersistenceException(sprintf('Cannot delete process with PID %s.', $process->pid), $exception);
}
}
/**
* Clean the process table of inactive physical processes
*/
private function deleteInactive()
{
$this->db->transaction();
try {
$processes = $this->db->select('process', ['pid']);
while ($process = $this->db->fetch($processes)) {
if (!posix_kill($process['pid'], 0)) {
$this->db->delete('process', ['pid' => $process['pid']]);
}
}
$this->db->close($processes);
} catch (\Exception $exception) {
throw new ProcessPersistenceException('Cannot delete inactive process', $exception);
} finally {
$this->db->commit();
}
}
}