+ /**
+ * @brief Log active processes into the "process" table
+ */
+ function start_process() {
+ $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1);
+
+ $command = basename($trace[0]["file"]);
+
+ $this->remove_inactive_processes();
+
+ q("START TRANSACTION");
+
+ $r = q("SELECT `pid` FROM `process` WHERE `pid` = %d", intval(getmypid()));
+ if(!dbm::is_result($r)) {
+ q("INSERT INTO `process` (`pid`,`command`,`created`) VALUES (%d, '%s', '%s')",
+ intval(getmypid()),
+ dbesc($command),
+ dbesc(datetime_convert()));
+ }
+ q("COMMIT");
+ }
+
+ /**
+ * @brief Remove inactive processes
+ */
+ function remove_inactive_processes() {
+ q("START TRANSACTION");
+
+ $r = q("SELECT `pid` FROM `process`");
+ if(dbm::is_result($r)) {
+ foreach ($r AS $process) {
+ if (!posix_kill($process["pid"], 0)) {
+ q("DELETE FROM `process` WHERE `pid` = %d", intval($process["pid"]));
+ }
+ }
+ }
+ q("COMMIT");
+ }
+
+ /**
+ * @brief Remove the active process from the "process" table
+ */
+ function end_process() {
+ q("DELETE FROM `process` WHERE `pid` = %d", intval(getmypid()));
+ }
+