]> git.mxchange.org Git - friendica.git/blob - src/Model/Process.php
Merge remote-tracking branch 'upstream/develop' into logging
[friendica.git] / src / Model / Process.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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\Model;
23
24 use Friendica\Database\Database;
25 use Friendica\Util\DateTimeFormat;
26
27 /**
28  * functions for interacting with a process
29  */
30 class Process
31 {
32         /** @var Database */
33         private $dba;
34
35         public function __construct(Database $dba)
36         {
37                 $this->dba = $dba;
38         }
39
40         /**
41          * Insert a new process row. If the pid parameter is omitted, we use the current pid
42          *
43          * @param string $command
44          * @param int $pid The process id to insert
45          * @return bool
46          * @throws \Exception
47          */
48         public function insert(string $command, int $pid)
49         {
50                 $return = true;
51
52                 $this->dba->transaction();
53
54                 if (!$this->dba->exists('process', ['pid' => $pid])) {
55                         $return = $this->dba->insert('process', ['pid' => $pid, 'command' => $command, 'created' => DateTimeFormat::utcNow()]);
56                 }
57
58                 $this->dba->commit();
59
60                 return $return;
61         }
62
63         /**
64          * Remove a process row by pid. If the pid parameter is omitted, we use the current pid
65          *
66          * @param int $pid The pid to delete
67          * @return bool
68          * @throws \Exception
69          */
70         public function deleteByPid(int $pid)
71         {
72                 return $this->dba->delete('process', ['pid' => $pid]);
73         }
74
75         /**
76          * Clean the process table of inactive physical processes
77          */
78         public function deleteInactive()
79         {
80                 $this->dba->transaction();
81
82                 $processes = $this->dba->select('process', ['pid']);
83                 while($process = $this->dba->fetch($processes)) {
84                         if (!posix_kill($process['pid'], 0)) {
85                                 $this->deleteByPid($process['pid']);
86                         }
87                 }
88                 $this->dba->close($processes);
89                 $this->dba->commit();
90         }
91 }