]> git.mxchange.org Git - friendica.git/blob - src/Model/Process.php
Fix mods/README.md format
[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 require_once 'include/dba.php';
12
13 /**
14  * @brief functions for interacting with a process
15  */
16 class Process extends BaseObject
17 {
18         /**
19          * Insert a new process row. If the pid parameter is omitted, we use the current pid
20          *
21          * @param string $command
22          * @param string $pid
23          * @return bool
24          */
25         public static function insert($command, $pid = null)
26         {
27                 $return = true;
28
29                 if (is_null($pid)) {
30                         $pid = getmypid();
31                 }
32
33                 DBA::transaction();
34
35                 if (!DBA::exists('process', ['pid' => $pid])) {
36                         $return = DBA::insert('process', ['pid' => $pid, 'command' => $command, 'created' => DateTimeFormat::utcNow()]);
37                 }
38
39                 DBA::commit();
40
41                 return $return;
42         }
43
44         /**
45          * Remove a process row by pid. If the pid parameter is omitted, we use the current pid
46          *
47          * @param string $pid
48          * @return bool
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 }