]> git.mxchange.org Git - friendica.git/blob - src/Model/Process.php
a960777129813f25a83254b1a758ae46bfb1bda9
[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 require_once 'include/datetime.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                 dba::transaction();
30
31                 if (!dba::exists('process', ['pid' => getmypid()])) {
32                         $return = dba::insert('process', ['pid' => $pid, 'command' => $command, 'created' => datetime_convert()]);
33                 }
34
35                 dba::commit();
36
37                 return $return;
38         }
39
40         /**
41          * Remove a process row by pid. If the pid parameter is omitted, we use the current pid
42          *
43          * @param string $pid
44          * @return bool
45          */
46         public static function deleteByPid($pid = null)
47         {
48                 if ($pid === null) {
49                         $pid = getmypid();
50                 }
51
52                 return dba::delete('process', ['pid' => $pid]);
53         }
54
55         /**
56          * Clean the process table of inactive physical processes
57          */
58         public static function deleteInactive()
59         {
60                 dba::transaction();
61
62                 $processes = dba::select('process', ['pid']);
63                 while($process = dba::fetch($processes)) {
64                         if (!posix_kill($process['pid'], 0)) {
65                                 self::deleteByPid($process['pid']);
66                         }
67                 }
68
69                 dba::commit();
70         }
71 }