]> git.mxchange.org Git - friendica.git/blob - src/Core/Worker/Repository/Process.php
Improved bulk delivery / don't redistribute activities
[friendica.git] / src / Core / Worker / Repository / Process.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Core\Worker\Repository;
23
24 use Friendica\BaseRepository;
25 use Friendica\Core\Worker\Exception\ProcessPersistenceException;
26 use Friendica\Database\Database;
27 use Friendica\Util\DateTimeFormat;
28 use Friendica\Core\Worker\Factory;
29 use Friendica\Core\Worker\Entity;
30 use Psr\Log\LoggerInterface;
31
32 /**
33  * functions for interacting with a process
34  */
35 class Process extends BaseRepository
36 {
37         const NODE_ENV = 'NODE_ENV';
38
39         protected static $table_name = 'process';
40
41         /** @var Factory\Process */
42         protected $factory;
43
44         /** @var string */
45         private $currentHost;
46
47         public function __construct(Database $database, LoggerInterface $logger, Factory\Process $factory, array $server)
48         {
49                 parent::__construct($database, $logger, $factory);
50
51                 $this->currentHost = $factory->determineHost($server[self::NODE_ENV] ?? null);
52         }
53
54         /**
55          * Starts and Returns the process for a given PID at the current host
56          *
57          * @param int    $pid
58          * @param string $command
59          *
60          * @return Entity\Process
61          */
62         public function create(int $pid, string $command): Entity\Process
63         {
64                 // Cleanup inactive process
65                 $this->deleteInactive();
66
67                 try {
68                         $this->db->transaction();
69
70                         if (!$this->db->exists(static::$table_name, ['pid' => $pid, 'hostname' => $this->currentHost])) {
71                                 if (!$this->db->insert(static::$table_name, [
72                                         'pid'      => $pid,
73                                         'command'  => $command,
74                                         'hostname' => $this->currentHost,
75                                         'created'  => DateTimeFormat::utcNow()
76                                 ])) {
77                                         throw new ProcessPersistenceException(sprintf('The process with PID %s already exists.', $pid));
78                                 }
79                         }
80
81                         $result = $this->_selectOne(['pid' => $pid, 'hostname' => $this->currentHost]);
82
83                         $this->db->commit();
84
85                         return $result;
86                 } catch (\Exception $exception) {
87                         throw new ProcessPersistenceException(sprintf('Cannot save process with PID %s.', $pid), $exception);
88                 }
89         }
90
91         public function delete(Entity\Process $process)
92         {
93                 try {
94                         if (!$this->db->delete(static::$table_name, [
95                                 'pid'      => $process->pid,
96                                 'hostname' => $this->currentHost,
97                         ])) {
98                                 throw new ProcessPersistenceException(sprintf('The process with PID %s doesn\'t exists.', $process->pi));
99                         }
100                 } catch (\Exception $exception) {
101                         throw new ProcessPersistenceException(sprintf('Cannot delete process with PID %s.', $process->pid), $exception);
102                 }
103         }
104
105         /**
106          * Clean the process table of inactive physical processes
107          */
108         private function deleteInactive()
109         {
110                 $this->db->transaction();
111
112                 try {
113                         $processes = $this->db->select(static::$table_name, ['pid'], ['hostname' => $this->currentHost]);
114                         while ($process = $this->db->fetch($processes)) {
115                                 if (!posix_kill($process['pid'], 0)) {
116                                         $this->db->delete(static::$table_name, ['pid' => $process['pid']]);
117                                 }
118                         }
119                         $this->db->close($processes);
120                         $this->db->commit();
121                 } catch (\Exception $exception) {
122                         $this->db->rollback();
123                         throw new ProcessPersistenceException('Cannot delete inactive process', $exception);
124                 }
125         }
126
127         /**
128          * Returns the number of processes with a given command
129          *
130          * @param string $command
131          *
132          * @return int Number of processes
133          *
134          * @throws ProcessPersistenceException
135          */
136         public function countCommand(string $command): int
137         {
138                 try {
139                         return $this->count(['command' => strtolower($command)]);
140                 } catch (\Exception $exception) {
141                         throw new ProcessPersistenceException('Cannot count ', $exception);
142                 }
143         }
144 }