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