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