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