]> git.mxchange.org Git - friendica.git/commitdiff
New process table for a better detection of running workers
authorMichael Vogel <ike@pirati.ca>
Fri, 9 Sep 2016 20:33:54 +0000 (20:33 +0000)
committerMichael Vogel <ike@pirati.ca>
Fri, 9 Sep 2016 20:33:54 +0000 (20:33 +0000)
23 files changed:
boot.php
include/auth_ejabberd.php
include/cli_startup.php
include/cron.php
include/cronhooks.php
include/cronjobs.php
include/dbstructure.php
include/dbupdate.php
include/delivery.php
include/directory.php
include/discover_poco.php
include/expire.php
include/gprobe.php
include/notifier.php
include/onepoll.php
include/poller.php
include/pubsubpublish.php
include/queue.php
include/shadowupdate.php
include/tagupdate.php
include/threadupdate.php
include/update_gcontact.php
update.php

index d5b8439333c72216e39d400dc76ff1564adef9a6..7097f3f9833c0a01c2b773e52e628d10c5863a4d 100644 (file)
--- a/boot.php
+++ b/boot.php
@@ -38,7 +38,7 @@ define ( 'FRIENDICA_PLATFORM',     'Friendica');
 define ( 'FRIENDICA_CODENAME',     'Asparagus');
 define ( 'FRIENDICA_VERSION',      '3.5-dev' );
 define ( 'DFRN_PROTOCOL_VERSION',  '2.23'    );
-define ( 'DB_UPDATE_VERSION',      1201      );
+define ( 'DB_UPDATE_VERSION',      1202      );
 
 /**
  * @brief Constant with a HTML line break.
@@ -1099,6 +1099,42 @@ class App {
 
        }
 
+       /**
+        * @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();
+
+               $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()));
+       }
+
+       /**
+        * @brief Remove inactive processes
+        */
+       function remove_inactive_processes() {
+               $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"]));
+       }
+
+       /**
+        * @brief Remove the active process from the "process" table
+        */
+       function end_process() {
+               q("DELETE FROM `process` WHERE `pid` = %d", intval(getmypid()));
+       }
+
        /**
         * @brief Returns a string with a callstack. Can be used for logging.
         *
@@ -1699,6 +1735,9 @@ function login($register = false, $hiddens=false) {
  * @brief Used to end the current process, after saving session state.
  */
 function killme() {
+
+       get_app()->end_process();
+
        if (!get_app()->is_backend())
                session_write_close();
 
index 9a9d9accad83ef002b3f398f701b4e95ac3508da..d7e91a5b2c4d141147fb5417a22f478fbb58d782 100755 (executable)
@@ -58,6 +58,8 @@ if(is_null($db)) {
        unset($db_host, $db_user, $db_pass, $db_data);
 };
 
+$a->start_process();
+
 // the logfile to which to write, should be writeable by the user which is running the server
 $sLogFile = get_config('jabber','logfile');
 
index d43bc1c947b4a70ebc591c0151e0260bccdfea7a..7b15fbfb30857520a64700d8f3882d416ed5edc7 100644 (file)
@@ -11,7 +11,7 @@ function cli_startup() {
        if(is_null($a)) {
                $a = new App;
        }
-  
+
        if(is_null($db)) {
            @include(".htconfig.php");
        require_once("dba.php");
@@ -19,6 +19,8 @@ function cli_startup() {
        unset($db_host, $db_user, $db_pass, $db_data);
        };
 
+       $a->start_process();
+
        require_once('include/session.php');
 
        load_config('config');
index 3f28e493cfe32cd57c0da7ac97f1d2d76d38764d..dde458a4a64c03ba890413f32dd087bee40df8d7 100644 (file)
@@ -27,6 +27,7 @@ function cron_run(&$argv, &$argc){
                unset($db_host, $db_user, $db_pass, $db_data);
        };
 
+       $a->start_process();
 
        require_once('include/session.php');
        require_once('include/datetime.php');
index 4bb1e5f65960b3b3c1bbad1077d34628388040b0..986a86dfbb8b3b7a219727c83a72a4ca1210916a 100644 (file)
@@ -17,6 +17,8 @@ function cronhooks_run(&$argv, &$argc){
                unset($db_host, $db_user, $db_pass, $db_data);
        };
 
+       $a->start_process();
+
        require_once('include/session.php');
        require_once('include/datetime.php');
 
index 3a6df698e1ff94f727ab184c27bc2a09b29e73da..6df2dcd035ec86ac9decf5853c52e21ce78153c5 100644 (file)
@@ -27,6 +27,7 @@ function cronjobs_run(&$argv, &$argc){
                unset($db_host, $db_user, $db_pass, $db_data);
        };
 
+       $a->start_process();
 
        require_once('include/session.php');
        require_once('include/datetime.php');
index 53b94b46a807eb74f50adf3cb59f5a59d3b07bd6..4530a78599c5d7472376077e4c7204527c4c48a4 100644 (file)
@@ -1114,6 +1114,17 @@ function db_definition() {
                                        "choice" => array("choice"),
                                        )
                        );
+       $database["process"] = array(
+                       "fields" => array(
+                                       "pid" => array("type" => "int(10) unsigned", "not null" => "1", "primary" => "1"),
+                                       "command" => array("type" => "varchar(32)", "not null" => "1", "default" => ""),
+                                       "created" => array("type" => "datetime", "not null" => "1", "default" => "0000-00-00 00:00:00"),
+                                       ),
+                       "indexes" => array(
+                                       "PRIMARY" => array("pid"),
+                                       "command" => array("command"),
+                                       )
+                       );
        $database["profile"] = array(
                        "fields" => array(
                                        "id" => array("type" => "int(11)", "not null" => "1", "extra" => "auto_increment", "primary" => "1"),
@@ -1456,6 +1467,8 @@ function dbstructure_run(&$argv, &$argc) {
                        unset($db_host, $db_user, $db_pass, $db_data);
        }
 
+       $a->start_process();
+
        if ($argc==2) {
                switch ($argv[1]) {
                        case "update":
index 28f1de340b91ca5fb0b8c62d9ab56ca0af87ec49..898f459630a1326b198472616fc35719cdfd8f5f 100644 (file)
@@ -16,6 +16,8 @@ function dbupdate_run(&$argv, &$argc) {
                        unset($db_host, $db_user, $db_pass, $db_data);
        }
 
+       $a->start_process();
+
        load_config('config');
        load_config('system');
 
index fe337743820dcb85940262fefdb4abe099c4d530..59cab43714a4e1c083983deab6b4da84b0fe5803 100644 (file)
@@ -21,6 +21,8 @@ function delivery_run(&$argv, &$argc){
                unset($db_host, $db_user, $db_pass, $db_data);
        }
 
+       $a->start_process();
+
        require_once("include/session.php");
        require_once("include/datetime.php");
        require_once('include/items.php');
index 85476bd5fdf498eaed07787c6c8e695732c2bd1f..381020c8a5642f09d568c26b681e5ee744878400 100644 (file)
@@ -15,6 +15,8 @@ function directory_run(&$argv, &$argc){
                                unset($db_host, $db_user, $db_pass, $db_data);
        };
 
+       $a->start_process();
+
        load_config('config');
        load_config('system');
 
index 0b468faea189e522505ed5c686facd22be905406..9c9be8209cadf1d47605f67b3c83b467bb802ff3 100644 (file)
@@ -18,6 +18,8 @@ function discover_poco_run(&$argv, &$argc){
        unset($db_host, $db_user, $db_pass, $db_data);
        };
 
+       $a->start_process();
+
        require_once('include/session.php');
        require_once('include/datetime.php');
 
index 873c594e8499ea93aeaa55727abeb09f34e81e28..cfd38d3dcd254f42608e3882edb42aa33eb4cbe5 100644 (file)
@@ -16,6 +16,8 @@ function expire_run(&$argv, &$argc){
        unset($db_host, $db_user, $db_pass, $db_data);
        };
 
+       $a->start_process();
+
        require_once('include/session.php');
        require_once('include/datetime.php');
        require_once('include/items.php');
index 91b9b16e3306077ba2f0c574e6c46e0ce5fe066a..d3ab028f1e24d911eacfad584f1467f49337c65a 100644 (file)
@@ -18,6 +18,8 @@ function gprobe_run(&$argv, &$argc){
        unset($db_host, $db_user, $db_pass, $db_data);
        };
 
+       $a->start_process();
+
        require_once('include/session.php');
        require_once('include/datetime.php');
 
index 0610a4e398df5a9baba95ba58e4e9df7a6927b03..5f700ee71632b96975f058f53d8534d18dc59ca3 100644 (file)
@@ -54,6 +54,8 @@ function notifier_run(&$argv, &$argc){
                        unset($db_host, $db_user, $db_pass, $db_data);
        }
 
+       $a->start_process();
+
        require_once("include/session.php");
        require_once("include/datetime.php");
        require_once('include/items.php');
index eb1045de142957dcab4e08d8b66ff8aa3ecb4750..c89e5b257c2164c2dba09edaf0bca4313d4961a2 100644 (file)
@@ -24,6 +24,7 @@ function onepoll_run(&$argv, &$argc){
                unset($db_host, $db_user, $db_pass, $db_data);
        };
 
+       $a->start_process();
 
        require_once('include/session.php');
        require_once('include/datetime.php');
index 0835528717cbe81ab56b26d599e410a6f5a4756b..83ecb2939533cf7b5ff1d943504d2e8272c9b091 100644 (file)
@@ -29,6 +29,10 @@ function poller_run(&$argv, &$argc){
                unset($db_host, $db_user, $db_pass, $db_data);
        };
 
+       $a->start_process();
+
+       $mypid = getmypid();
+
        if ($a->max_processes_reached())
                return;
 
@@ -81,15 +85,19 @@ function poller_run(&$argv, &$argc){
 
                q("UPDATE `workerqueue` SET `executed` = '%s', `pid` = %d WHERE `id` = %d AND `executed` = '0000-00-00 00:00:00'",
                        dbesc(datetime_convert()),
-                       intval(getmypid()),
+                       intval($mypid),
                        intval($r[0]["id"]));
 
                // Assure that there are no tasks executed twice
-               $id = q("SELECT `id` FROM `workerqueue` WHERE `id` = %d AND `pid` = %d",
-                       intval($r[0]["id"]),
-                       intval(getmypid()));
+               $id = q("SELECT `pid`, `executed` FROM `workerqueue` WHERE `id` = %d", intval($r[0]["id"]));
                if (!$id) {
-                       logger("Queue item ".$r[0]["id"]." was executed multiple times - skip this execution", LOGGER_DEBUG);
+                       logger("Queue item ".$r[0]["id"]." vanished - skip this execution", LOGGER_DEBUG);
+                       continue;
+               } elseif ((strtotime($id[0]["executed"]) <= 0) OR ($id[0]["pid"] == 0)) {
+                       logger("Entry for queue item ".$r[0]["id"]." wasn't stored - we better stop here", LOGGER_DEBUG);
+                       return;
+               } elseif ($id[0]["pid"] != $mypid) {
+                       logger("Queue item ".$r[0]["id"]." is to be executed by process ".$id[0]["pid"]." and not by me (".$mypid.") - skip this execution", LOGGER_DEBUG);
                        continue;
                }
 
@@ -111,15 +119,15 @@ function poller_run(&$argv, &$argc){
                $funcname = str_replace(".php", "", basename($argv[0]))."_run";
 
                if (function_exists($funcname)) {
-                       logger("Process ".getmypid()." - Prio ".$r[0]["priority"]." - ID ".$r[0]["id"].": ".$funcname." ".$r[0]["parameter"]);
+                       logger("Process ".$mypid." - Prio ".$r[0]["priority"]." - ID ".$r[0]["id"].": ".$funcname." ".$r[0]["parameter"]);
                        $funcname($argv, $argc);
 
                        if ($cooldown > 0) {
-                               logger("Process ".getmypid()." - Prio ".$r[0]["priority"]." - ID ".$r[0]["id"].": ".$funcname." - in cooldown for ".$cooldown." seconds");
+                               logger("Process ".$mypid." - Prio ".$r[0]["priority"]." - ID ".$r[0]["id"].": ".$funcname." - in cooldown for ".$cooldown." seconds");
                                sleep($cooldown);
                        }
 
-                       logger("Process ".getmypid()." - Prio ".$r[0]["priority"]." - ID ".$r[0]["id"].": ".$funcname." - done");
+                       logger("Process ".$mypid." - Prio ".$r[0]["priority"]." - ID ".$r[0]["id"].": ".$funcname." - done");
 
                        q("DELETE FROM `workerqueue` WHERE `id` = %d", intval($r[0]["id"]));
                } else
@@ -319,9 +327,9 @@ function poller_too_much_workers() {
 }
 
 function poller_active_workers() {
-       $workers = q("SELECT COUNT(*) AS `workers` FROM `workerqueue` WHERE `executed` != '0000-00-00 00:00:00'");
+       $workers = q("SELECT COUNT(*) AS `processes` FROM `process` WHERE `command` = 'poller.php'");
 
-       return($workers[0]["workers"]);
+       return($workers[0]["processes"]);
 }
 
 if (array_search(__file__,get_included_files())===0){
index 85637facb23b275eac29a46c0d61c89aa35621b9..3fff746cb258deb485fb1795a8f857ca4e54e3f3 100644 (file)
@@ -70,6 +70,8 @@ function pubsubpublish_run(&$argv, &$argc){
                unset($db_host, $db_user, $db_pass, $db_data);
        };
 
+       $a->start_process();
+
        require_once('include/items.php');
 
        load_config('config');
index 47a3596bdb46c31724724a766cc81c890a118d87..6dcdfc89601306d2869c1014e2ec43fccaa34314 100644 (file)
@@ -17,6 +17,7 @@ function queue_run(&$argv, &$argc){
                unset($db_host, $db_user, $db_pass, $db_data);
        };
 
+       $a->start_process();
 
        require_once("include/session.php");
        require_once("include/datetime.php");
index 74c2a43ebd792fc37cbe1d9c239e71690d53a31e..e003edea1701bd29eeb0995c3b68cd41038df6a7 100644 (file)
@@ -14,6 +14,8 @@ if(is_null($db)) {
        unset($db_host, $db_user, $db_pass, $db_data);
 }
 
+$a->start_process();
+
 load_config('config');
 load_config('system');
 
index b12e809772060b7287166c0d03dadcecf3eecf7d..f962a8f3ac8b20acd91086c052927f0aee84e267 100644 (file)
@@ -14,6 +14,8 @@ if(is_null($db)) {
        unset($db_host, $db_user, $db_pass, $db_data);
 }
 
+$a->start_process();
+
 load_config('config');
 load_config('system');
 
index e9d9bf6e6b518d99683f52190e7b30e7c4de81ae..454a4680af2a712b99524f20580608895a573b87 100644 (file)
@@ -14,6 +14,8 @@ if(is_null($db)) {
        unset($db_host, $db_user, $db_pass, $db_data);
 }
 
+$a->start_process();
+
 load_config('config');
 load_config('system');
 
index 88e1817f0b991ff6a0749c2d794b71510ab62b22..ba0993d14d01882bd7bcbf598da6a3b372c24d4c 100644 (file)
@@ -16,6 +16,8 @@ function update_gcontact_run(&$argv, &$argc){
                unset($db_host, $db_user, $db_pass, $db_data);
        };
 
+       $a->start_process();
+
        require_once('include/Scrape.php');
        require_once("include/socgraph.php");
 
index 80ce96c382d183ffefc73a9ba7ccf9e6b33cbcea..f1981f730f28117d85bfbaeac66cb47610fd0059 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 
-define('UPDATE_VERSION' , 1201);
+define('UPDATE_VERSION' , 1202);
 
 /**
  *