]> git.mxchange.org Git - friendica.git/commitdiff
Move process functions to Model\Process
authorHypolite Petovan <mrpetovan@gmail.com>
Tue, 16 Jan 2018 00:08:28 +0000 (19:08 -0500)
committerHypolite Petovan <mrpetovan@gmail.com>
Wed, 17 Jan 2018 05:17:58 +0000 (00:17 -0500)
- Add start|endProcess functions to Core\Worker

mod/worker.php
scripts/worker.php
src/App.php
src/Core/Worker.php
src/Model/Process.php [new file with mode: 0644]

index d6ecbecc264935036eb1427e72f65f9c4da3ad62..056416e799d4022d2e203d709fe2643ddf915564 100644 (file)
@@ -29,7 +29,7 @@ function worker_init($a){
                return;
        }
 
-       $a->start_process();
+       Worker::startProcess();
 
        logger("Front end worker started: ".getmypid());
 
@@ -50,7 +50,7 @@ function worker_init($a){
 
        Worker::unclaimProcess();
 
-       $a->end_process();
+       Worker::endProcess();
 
        logger("Front end worker ended: ".getmypid());
 
index a3738038b1a93944fb83c7d5fb5bd4436d3471ad..d69b089aecb0b904616e5886a49c6ef2b601d0d8 100755 (executable)
@@ -57,7 +57,7 @@ Worker::processQueue($run_cron);
 
 Worker::unclaimProcess();
 
-$a->end_process();
+Worker::endProcess();
 
 killme();
 
index fdd928ba1a1c7b64a226301fd8ca1a5fdfa78d79..62b5f12e2cffeff3f8db6c867c0c067fc90f071b 100644 (file)
@@ -2,20 +2,17 @@
 
 namespace Friendica;
 
-use Friendica\Core\System;
 use Friendica\Core\Cache;
 use Friendica\Core\Config;
 use Friendica\Core\PConfig;
-use Friendica\Database\DBM;
-
-use dba;
+use Friendica\Core\System;
 
 use Detection\MobileDetect;
 
 use Exception;
 
 require_once 'boot.php';
-require_once 'include/dba.php';
+require_once 'include/text.php';
 
 /**
  *
@@ -695,49 +692,6 @@ class App {
                $this->callstack[$value][$callstack] += (float) $duration;
        }
 
-       /**
-        * @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();
-
-               dba::transaction();
-
-               $r = q('SELECT `pid` FROM `process` WHERE `pid` = %d', intval(getmypid()));
-               if (!DBM::is_result($r)) {
-                       dba::insert('process', ['pid' => getmypid(), 'command' => $command, 'created' => datetime_convert()]);
-               }
-               dba::commit();
-       }
-
-       /**
-        * @brief Remove inactive processes
-        */
-       function remove_inactive_processes() {
-               dba::transaction();
-
-               $r = q('SELECT `pid` FROM `process`');
-               if (DBM::is_result($r)) {
-                       foreach ($r AS $process) {
-                               if (!posix_kill($process['pid'], 0)) {
-                                       dba::delete('process', ['pid' => $process['pid']]);
-                               }
-                       }
-               }
-               dba::commit();
-       }
-
-       /**
-        * @brief Remove the active process from the "process" table
-        */
-       function end_process() {
-               dba::delete('process', ['pid' => getmypid()]);
-       }
-
        function get_useragent() {
                return
                        FRIENDICA_PLATFORM . " '" .
index da7425a61682ae22d315e99f9567450baadf5798..478f01a9674ed6c8263fa8a75d6a2aa6331c3fb0 100644 (file)
@@ -4,11 +4,10 @@
  */
 namespace Friendica\Core;
 
-use Friendica\App;
-use Friendica\Core\System;
 use Friendica\Core\Config;
-use Friendica\Core\Worker;
+use Friendica\Core\System;
 use Friendica\Database\DBM;
+use Friendica\Model\Process;
 use Friendica\Util\Lock;
 
 use dba;
@@ -50,7 +49,7 @@ class Worker
                }
 
                // We now start the process. This is done after the load check since this could increase the load.
-               $a->start_process();
+               self::startProcess();
 
                // Kill stale processes every 5 minutes
                $last_cleanup = Config::get('system', 'poller_last_cleaned', 0);
@@ -915,7 +914,7 @@ class Worker
                        if (self::tooMuchWorkers()) {
                                // Cleaning dead processes
                                self::killStaleWorkers();
-                               get_app()->remove_inactive_processes();
+                               Process::deleteInactive();
 
                                return;
                        }
@@ -1092,4 +1091,31 @@ class Worker
 
                return true;
        }
+
+       /**
+        * Log active processes into the "process" table
+        *
+        * @brief Log active processes into the "process" table
+        */
+       public static function startProcess()
+       {
+               $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1);
+
+               $command = basename($trace[0]['file']);
+
+               Process::deleteInactive();
+
+               Process::insert($command);
+       }
+
+       /**
+        * Remove the active process from the "process" table
+        *
+        * @brief Remove the active process from the "process" table
+        * @return bool
+        */
+       public static function endProcess()
+       {
+               return Process::deleteByPid();
+       }
 }
diff --git a/src/Model/Process.php b/src/Model/Process.php
new file mode 100644 (file)
index 0000000..6b2d2fb
--- /dev/null
@@ -0,0 +1,68 @@
+<?php
+/**
+ * @file src/Model/Process.php
+ */
+namespace Friendica\Model;
+
+use Friendica\BaseObject;
+use dba;
+
+require_once 'include/dba.php';
+
+/**
+ * @brief functions for interacting with a process
+ */
+class Process extends BaseObject
+{
+       /**
+        * Insert a new process row. If the pid parameter is omitted, we use the current pid
+        *
+        * @param string $command
+        * @param string $pid
+        * @return bool
+        */
+       public static function insert($command, $pid = null)
+       {
+               dba::transaction();
+
+               if (!dba::exists('process', ['pid' => getmypid()])) {
+                       $return = dba::insert('process', ['pid' => $pid, 'command' => $command, 'created' => datetime_convert()]);
+               }
+
+               dba::commit();
+
+               return $return;
+       }
+
+       /**
+        * Remove a process row by pid. If the pid parameter is omitted, we use the current pid
+        *
+        * @param string $pid
+        * @return bool
+        */
+       public static function deleteByPid($pid = null)
+       {
+               if ($pid === null) {
+                       $pid = getmypid();
+               }
+
+               return dba::delete('process', ['pid' => $pid]);
+       }
+
+       /**
+        * Clean the process table of inactive physical processes
+        */
+       public static function deleteInactive()
+       {
+               dba::transaction();
+
+               $processes = dba::select('process', ['pid']);
+               while($process = dba::fetch($processes)) {
+                       if (!posix_kill($process['pid'], 0)) {
+                               self::deleteByPid($process['pid']);
+                       }
+               }
+
+               dba::commit();
+       }
+}