]> git.mxchange.org Git - friendica.git/blob - src/Model/Process.php
Merge pull request #6209 from MrPetovan/task/move-config-to-php-array
[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          */
23         public static function insert($command, $pid = null)
24         {
25                 $return = true;
26
27                 if (is_null($pid)) {
28                         $pid = getmypid();
29                 }
30
31                 DBA::transaction();
32
33                 if (!DBA::exists('process', ['pid' => $pid])) {
34                         $return = DBA::insert('process', ['pid' => $pid, 'command' => $command, 'created' => DateTimeFormat::utcNow()]);
35                 }
36
37                 DBA::commit();
38
39                 return $return;
40         }
41
42         /**
43          * Remove a process row by pid. If the pid parameter is omitted, we use the current pid
44          *
45          * @param string $pid
46          * @return bool
47          */
48         public static function deleteByPid($pid = null)
49         {
50                 if ($pid === null) {
51                         $pid = getmypid();
52                 }
53
54                 return DBA::delete('process', ['pid' => $pid]);
55         }
56
57         /**
58          * Clean the process table of inactive physical processes
59          */
60         public static function deleteInactive()
61         {
62                 DBA::transaction();
63
64                 $processes = DBA::select('process', ['pid']);
65                 while($process = DBA::fetch($processes)) {
66                         if (!posix_kill($process['pid'], 0)) {
67                                 self::deleteByPid($process['pid']);
68                         }
69                 }
70
71                 DBA::commit();
72         }
73 }