]> git.mxchange.org Git - friendica.git/blob - src/Model/Process.php
Use direct logic
[friendica.git] / src / Model / Process.php
1 <?php
2 /**
3  * @file src/Model/Process.php
4  */
5 namespace Friendica\Model;
6
7 use Friendica\BaseObject;
8 use Friendica\Database\DBA;
9 use Friendica\Util\DateTimeFormat;
10
11 /**
12  * @brief functions for interacting with a process
13  */
14 class Process extends BaseObject
15 {
16         /**
17          * Insert a new process row. If the pid parameter is omitted, we use the current pid
18          *
19          * @param string $command
20          * @param string $pid
21          * @return bool
22          * @throws \Exception
23          */
24         public static function insert($command, $pid = null)
25         {
26                 $return = true;
27
28                 if (is_null($pid)) {
29                         $pid = getmypid();
30                 }
31
32                 DBA::transaction();
33
34                 if (!DBA::exists('process', ['pid' => $pid])) {
35                         $return = DBA::insert('process', ['pid' => $pid, 'command' => $command, 'created' => DateTimeFormat::utcNow()]);
36                 }
37
38                 DBA::commit();
39
40                 return $return;
41         }
42
43         /**
44          * Remove a process row by pid. If the pid parameter is omitted, we use the current pid
45          *
46          * @param string $pid
47          * @return bool
48          * @throws \Exception
49          */
50         public static function deleteByPid($pid = null)
51         {
52                 if ($pid === null) {
53                         $pid = getmypid();
54                 }
55
56                 return DBA::delete('process', ['pid' => $pid]);
57         }
58
59         /**
60          * Clean the process table of inactive physical processes
61          */
62         public static function deleteInactive()
63         {
64                 DBA::transaction();
65
66                 $processes = DBA::select('process', ['pid']);
67                 while($process = DBA::fetch($processes)) {
68                         if (!posix_kill($process['pid'], 0)) {
69                                 self::deleteByPid($process['pid']);
70                         }
71                 }
72
73                 DBA::commit();
74         }
75 }