1
0
Fork 0

Add hostname to Process entity

This commit is contained in:
Philipp Holzer 2021-11-06 20:21:01 +01:00
commit 5350e0852d
Signed by: nupplaPhil
GPG key ID: 24A7501396EB5432
10 changed files with 148 additions and 140 deletions

View file

@ -1,4 +1,23 @@
<?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\Entity;
@ -8,6 +27,7 @@ use Friendica\BaseEntity;
/**
* @property-read int $pid
* @property-read string $command
* @property-read string $hostname
* @property-read DateTime $created
*/
class Process extends BaseEntity
@ -16,18 +36,22 @@ class Process extends BaseEntity
protected $pid;
/** @var string */
protected $command;
/** @var string */
protected $hostname;
/** @var DateTime */
protected $created;
/**
* @param int $pid
* @param string $command
* @param int $pid
* @param string $command
* @param string $hostname
* @param DateTime $created
*/
public function __construct(int $pid, string $command, DateTime $created)
public function __construct(int $pid, string $command, string $hostname, DateTime $created)
{
$this->pid = $pid;
$this->command = $command;
$this->created = $created;
$this->pid = $pid;
$this->command = $command;
$this->hostname = $hostname;
$this->created = $created;
}
}

View file

@ -1,4 +1,23 @@
<?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\Exception;

View file

@ -1,4 +1,23 @@
<?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\Factory;
@ -8,28 +27,22 @@ use Friendica\Core\Worker\Entity;
class Process extends BaseFactory implements ICanCreateFromTableRow
{
public function determineHost(?string $hostname = null): string
{
if (empty($hostname)) {
$hostname = php_uname('n');
}
return strtolower($hostname);
}
public function createFromTableRow(array $row): Entity\Process
{
return new Entity\Process(
$row['pid'],
$row['command'],
$this->determineHost($row['hostname'] ?? null),
new \DateTime($row['created'] ?? 'now', new \DateTimeZone('UTC'))
);
}
/**
* Creates a new process entry for a given PID
*
* @param int $pid
* @param string $command
*
* @return Entity\Process
*/
public function create(int $pid, string $command): Entity\Process
{
return $this->createFromTableRow([
'pid' => $pid,
'command' => $command,
]);
}
}

View file

@ -34,18 +34,25 @@ use Psr\Log\LoggerInterface;
*/
class Process extends BaseRepository
{
const NODE_ENV = 'NODE_ENV';
protected static $table_name = 'process';
/** @var Factory\Process */
protected $factory;
public function __construct(Database $database, LoggerInterface $logger, Factory\Process $factory)
/** @var string */
private $currentHost;
public function __construct(Database $database, LoggerInterface $logger, Factory\Process $factory, array $server)
{
parent::__construct($database, $logger, $factory);
$this->currentHost = $factory->determineHost($server[self::NODE_ENV] ?? '');
}
/**
* Starts and Returns the process for a given PID
* Starts and Returns the process for a given PID at the current host
*
* @param int $pid
* @param string $command
@ -60,19 +67,18 @@ class Process extends BaseRepository
try {
$this->db->transaction();
$newProcess = $this->factory->create($pid, $command);
if (!$this->db->exists('process', ['pid' => $pid])) {
if (!$this->db->exists('process', ['pid' => $pid, 'hostname' => $this->currentHost])) {
if (!$this->db->insert(static::$table_name, [
'pid' => $newProcess->pid,
'command' => $newProcess->command,
'created' => $newProcess->created->format(DateTimeFormat::MYSQL)
'pid' => $pid,
'command' => $command,
'hostname' => $this->currentHost,
'created' => DateTimeFormat::utcNow()
])) {
throw new ProcessPersistenceException(sprintf('The process with PID %s already exists.', $pid));
}
}
$result = $this->_selectOne(['pid' => $pid]);
$result = $this->_selectOne(['pid' => $pid, 'hostname' => $this->currentHost]);
$this->db->commit();
@ -86,7 +92,8 @@ class Process extends BaseRepository
{
try {
if (!$this->db->delete(static::$table_name, [
'pid' => $process->pid
'pid' => $process->pid,
'hostname' => $this->currentHost,
])) {
throw new ProcessPersistenceException(sprintf('The process with PID %s doesn\'t exists.', $process->pi));
}
@ -103,7 +110,7 @@ class Process extends BaseRepository
$this->db->transaction();
try {
$processes = $this->db->select('process', ['pid']);
$processes = $this->db->select('process', ['pid'], ['hostname' => $this->currentHost]);
while ($process = $this->db->fetch($processes)) {
if (!posix_kill($process['pid'], 0)) {
$this->db->delete('process', ['pid' => $process['pid']]);