1
0
Fork 0

Add hostname to Process entity

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

View file

@ -1,6 +1,6 @@
-- ------------------------------------------ -- ------------------------------------------
-- Friendica 2021.12-dev (Siberian Iris) -- Friendica 2021.12-dev (Siberian Iris)
-- DB_UPDATE_VERSION 1442 -- DB_UPDATE_VERSION 1443
-- ------------------------------------------ -- ------------------------------------------
@ -681,16 +681,6 @@ CREATE TABLE IF NOT EXISTS `hook` (
UNIQUE INDEX `hook_file_function` (`hook`,`file`,`function`) UNIQUE INDEX `hook_file_function` (`hook`,`file`,`function`)
) DEFAULT COLLATE utf8mb4_general_ci COMMENT='addon hook registry'; ) DEFAULT COLLATE utf8mb4_general_ci COMMENT='addon hook registry';
--
-- TABLE host
--
CREATE TABLE IF NOT EXISTS `host` (
`id` tinyint unsigned NOT NULL auto_increment COMMENT 'sequential ID',
`name` varchar(128) NOT NULL DEFAULT '' COMMENT 'The hostname',
PRIMARY KEY(`id`),
UNIQUE INDEX `name` (`name`)
) DEFAULT COLLATE utf8mb4_general_ci COMMENT='Hostname';
-- --
-- TABLE inbox-status -- TABLE inbox-status
-- --
@ -1325,10 +1315,11 @@ CREATE TABLE IF NOT EXISTS `post-user-notification` (
-- TABLE process -- TABLE process
-- --
CREATE TABLE IF NOT EXISTS `process` ( CREATE TABLE IF NOT EXISTS `process` (
`pid` int unsigned NOT NULL COMMENT '', `pid` int unsigned NOT NULL COMMENT 'The process ID of the current node',
`hostname` varchar(32) NOT NULL COMMENT 'The hostname of the node',
`command` varbinary(32) NOT NULL DEFAULT '' COMMENT '', `command` varbinary(32) NOT NULL DEFAULT '' COMMENT '',
`created` datetime NOT NULL DEFAULT '0001-01-01 00:00:00' COMMENT '', `created` datetime NOT NULL DEFAULT '0001-01-01 00:00:00' COMMENT '',
PRIMARY KEY(`pid`), PRIMARY KEY(`pid`, `hostname`),
INDEX `command` (`command`) INDEX `command` (`command`)
) DEFAULT COLLATE utf8mb4_general_ci COMMENT='Currently running system processes'; ) DEFAULT COLLATE utf8mb4_general_ci COMMENT='Currently running system processes';

View file

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

View file

@ -1,4 +1,23 @@
<?php <?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; namespace Friendica\Core\Worker\Exception;

View file

@ -1,4 +1,23 @@
<?php <?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; namespace Friendica\Core\Worker\Factory;
@ -8,28 +27,22 @@ use Friendica\Core\Worker\Entity;
class Process extends BaseFactory implements ICanCreateFromTableRow 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 public function createFromTableRow(array $row): Entity\Process
{ {
return new Entity\Process( return new Entity\Process(
$row['pid'], $row['pid'],
$row['command'], $row['command'],
$this->determineHost($row['hostname'] ?? null),
new \DateTime($row['created'] ?? 'now', new \DateTimeZone('UTC')) 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 class Process extends BaseRepository
{ {
const NODE_ENV = 'NODE_ENV';
protected static $table_name = 'process'; protected static $table_name = 'process';
/** @var Factory\Process */ /** @var Factory\Process */
protected $factory; 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); 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 int $pid
* @param string $command * @param string $command
@ -60,19 +67,18 @@ class Process extends BaseRepository
try { try {
$this->db->transaction(); $this->db->transaction();
$newProcess = $this->factory->create($pid, $command); if (!$this->db->exists('process', ['pid' => $pid, 'hostname' => $this->currentHost])) {
if (!$this->db->exists('process', ['pid' => $pid])) {
if (!$this->db->insert(static::$table_name, [ if (!$this->db->insert(static::$table_name, [
'pid' => $newProcess->pid, 'pid' => $pid,
'command' => $newProcess->command, 'command' => $command,
'created' => $newProcess->created->format(DateTimeFormat::MYSQL) 'hostname' => $this->currentHost,
'created' => DateTimeFormat::utcNow()
])) { ])) {
throw new ProcessPersistenceException(sprintf('The process with PID %s already exists.', $pid)); 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(); $this->db->commit();
@ -86,7 +92,8 @@ class Process extends BaseRepository
{ {
try { try {
if (!$this->db->delete(static::$table_name, [ 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)); 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(); $this->db->transaction();
try { try {
$processes = $this->db->select('process', ['pid']); $processes = $this->db->select('process', ['pid'], ['hostname' => $this->currentHost]);
while ($process = $this->db->fetch($processes)) { while ($process = $this->db->fetch($processes)) {
if (!posix_kill($process['pid'], 0)) { if (!posix_kill($process['pid'], 0)) {
$this->db->delete('process', ['pid' => $process['pid']]); $this->db->delete('process', ['pid' => $process['pid']]);

View file

@ -82,7 +82,7 @@ class DBStructure
$old_tables = ['fserver', 'gcign', 'gcontact', 'gcontact-relation', 'gfollower' ,'glink', 'item-delivery-data', $old_tables = ['fserver', 'gcign', 'gcontact', 'gcontact-relation', 'gfollower' ,'glink', 'item-delivery-data',
'item-activity', 'item-content', 'item_id', 'participation', 'poll', 'poll_result', 'queue', 'retriever_rule', 'item-activity', 'item-content', 'item_id', 'participation', 'poll', 'poll_result', 'queue', 'retriever_rule',
'deliverq', 'dsprphotoq', 'ffinder', 'sign', 'spam', 'term', 'user-item', 'thread', 'item', 'challenge', 'deliverq', 'dsprphotoq', 'ffinder', 'sign', 'spam', 'term', 'user-item', 'thread', 'item', 'challenge',
'auth_codes', 'clients', 'tokens', 'profile_check']; 'auth_codes', 'clients', 'tokens', 'profile_check', 'host'];
$tables = DBA::selectToArray(['INFORMATION_SCHEMA' => 'TABLES'], ['TABLE_NAME'], $tables = DBA::selectToArray(['INFORMATION_SCHEMA' => 'TABLES'], ['TABLE_NAME'],
['TABLE_SCHEMA' => DBA::databaseName(), 'TABLE_TYPE' => 'BASE TABLE']); ['TABLE_SCHEMA' => DBA::databaseName(), 'TABLE_TYPE' => 'BASE TABLE']);

View file

@ -1,79 +0,0 @@
<?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\Model;
use Friendica\Core\Logger;
use Friendica\Database\DBA;
class Host
{
/**
* Get the id for a given hostname
* When empty, the current hostname is used
*
* @param string $hostname
*
* @return integer host name id
* @throws \Exception
*/
public static function getId(string $hostname = '')
{
if (empty($hostname)) {
$hostname = php_uname('n');
}
$hostname = strtolower($hostname);
$host = DBA::selectFirst('host', ['id'], ['name' => $hostname]);
if (!empty($host['id'])) {
return $host['id'];
}
DBA::replace('host', ['name' => $hostname]);
$host = DBA::selectFirst('host', ['id'], ['name' => $hostname]);
if (empty($host['id'])) {
Logger::warning('Host name could not be inserted', ['name' => $hostname]);
return 0;
}
return $host['id'];
}
/**
* Get the hostname for a given id
*
* @param int $id
*
* @return string host name
* @throws \Exception
*/
public static function getName(int $id)
{
$host = DBA::selectFirst('host', ['name'], ['id' => $id]);
if (!empty($host['name'])) {
return $host['name'];
}
return '';
}
}

View file

@ -55,7 +55,7 @@
use Friendica\Database\DBA; use Friendica\Database\DBA;
if (!defined('DB_UPDATE_VERSION')) { if (!defined('DB_UPDATE_VERSION')) {
define('DB_UPDATE_VERSION', 1442); define('DB_UPDATE_VERSION', 1443);
} }
return [ return [
@ -743,17 +743,6 @@ return [
"hook_file_function" => ["UNIQUE", "hook", "file", "function"], "hook_file_function" => ["UNIQUE", "hook", "file", "function"],
] ]
], ],
"host" => [
"comment" => "Hostname",
"fields" => [
"id" => ["type" => "tinyint unsigned", "not null" => "1", "extra" => "auto_increment", "primary" => "1", "comment" => "sequential ID"],
"name" => ["type" => "varchar(128)", "not null" => "1", "default" => "", "comment" => "The hostname"],
],
"indexes" => [
"PRIMARY" => ["id"],
"name" => ["UNIQUE", "name"],
]
],
"inbox-status" => [ "inbox-status" => [
"comment" => "Status of ActivityPub inboxes", "comment" => "Status of ActivityPub inboxes",
"fields" => [ "fields" => [
@ -1343,12 +1332,13 @@ return [
"process" => [ "process" => [
"comment" => "Currently running system processes", "comment" => "Currently running system processes",
"fields" => [ "fields" => [
"pid" => ["type" => "int unsigned", "not null" => "1", "primary" => "1", "comment" => ""], "pid" => ["type" => "int unsigned", "not null" => "1", "primary" => "1", "comment" => "The process ID of the current node"],
"hostname" => ["type" => "varchar(32)", "not null" => "1", "primary" => "1", "comment" => "The hostname of the current node"],
"command" => ["type" => "varbinary(32)", "not null" => "1", "default" => "", "comment" => ""], "command" => ["type" => "varbinary(32)", "not null" => "1", "default" => "", "comment" => ""],
"created" => ["type" => "datetime", "not null" => "1", "default" => DBA::NULL_DATETIME, "comment" => ""], "created" => ["type" => "datetime", "not null" => "1", "default" => DBA::NULL_DATETIME, "comment" => ""],
], ],
"indexes" => [ "indexes" => [
"PRIMARY" => ["pid"], "PRIMARY" => ["pid", "hostname"],
"command" => ["command"], "command" => ["command"],
] ]
], ],

View file

@ -238,4 +238,9 @@ return [
[Dice::INSTANCE => Util\ReversedFileReader::class], [Dice::INSTANCE => Util\ReversedFileReader::class],
] ]
], ],
\Friendica\Core\Worker\Repository\Process::class => [
'constructParams' => [
$_SERVER
],
],
]; ];

View file

@ -0,0 +1,38 @@
<?php
namespace Friendica\Test\src\Core\Worker\Repository;
use Friendica\Core\Worker\Factory;
use Friendica\Core\Worker\Repository;
use Friendica\DI;
use Friendica\Test\FixtureTest;
use Psr\Log\NullLogger;
class ProcessTest extends FixtureTest
{
public function testStandardProcess()
{
$factory = new Factory\Process(new NullLogger());
$repository = new Repository\Process(DI::dba(), new NullLogger(), $factory, []);
$entityNew = $repository->create(getmypid(), 'test');
self::assertEquals(getmypid(), $entityNew->pid);
self::assertEquals('test', $entityNew->command);
self::assertLessThanOrEqual(new \DateTime('now', new \DateTimeZone('UTC')), $entityNew->created);
self::assertEquals(php_uname('n'), $entityNew->hostname);
}
public function testHostnameEnv()
{
$factory = new Factory\Process(new NullLogger());
$repository = new Repository\Process(DI::dba(), new NullLogger(), $factory, [Repository\Process::NODE_ENV => 'test_node']);
$entityNew = $repository->create(getmypid(), 'test');
self::assertEquals(getmypid(), $entityNew->pid);
self::assertEquals('test', $entityNew->command);
self::assertLessThanOrEqual(new \DateTime('now', new \DateTimeZone('UTC')), $entityNew->created);
self::assertEquals('test_node', $entityNew->hostname);
}
}