]> git.mxchange.org Git - friendica.git/commitdiff
Hook calls can now be forked into a worker queue entry
authorMichael <heluecht@pirati.ca>
Tue, 6 Feb 2018 22:52:36 +0000 (22:52 +0000)
committerMichael <heluecht@pirati.ca>
Tue, 6 Feb 2018 22:52:36 +0000 (22:52 +0000)
boot.php
database.sql
src/Core/Addon.php
src/Core/Worker.php
src/Database/DBStructure.php
src/Worker/Cron.php
src/Worker/CronHooks.php [deleted file]
src/Worker/ForkHook.php [new file with mode: 0644]
src/Worker/Notifier.php

index 004ebf3adecde4a4563a754dccf46153a66d1c5e..116c712a83c223100a5cf7fe386a50b149392919 100644 (file)
--- a/boot.php
+++ b/boot.php
@@ -37,7 +37,7 @@ define('FRIENDICA_PLATFORM',     'Friendica');
 define('FRIENDICA_CODENAME',     'Asparagus');
 define('FRIENDICA_VERSION',      '3.6-dev');
 define('DFRN_PROTOCOL_VERSION',  '2.23');
-define('DB_UPDATE_VERSION',      1248);
+define('DB_UPDATE_VERSION',      1249);
 define('NEW_UPDATE_ROUTINE_VERSION', 1170);
 
 /**
index 7433213107192607eda5c10a964d2954750d3dee..85cd811c1b0ac15cf766b466f0e6f39d2aa775a7 100644 (file)
@@ -1065,7 +1065,7 @@ CREATE TABLE IF NOT EXISTS `userd` (
 --
 CREATE TABLE IF NOT EXISTS `workerqueue` (
        `id` int NOT NULL auto_increment COMMENT 'Auto incremented worker task id',
-       `parameter` text COMMENT 'Task command',
+       `parameter` mediumtext COMMENT 'Task command',
        `priority` tinyint NOT NULL DEFAULT 0 COMMENT 'Task priority',
        `created` datetime NOT NULL DEFAULT '0001-01-01 00:00:00' COMMENT 'Creation date',
        `pid` int NOT NULL DEFAULT 0 COMMENT 'Process id of the worker',
index a721bd67eaba16443a509de64186ea9af594aa87..394dbe56236771c70ffcca10b92ee1e1588f477c 100644 (file)
@@ -6,6 +6,7 @@ namespace Friendica\Core;
 
 use Friendica\Core\Config;
 use Friendica\Database\DBM;
+use Friendica\Core\Worker;
 
 use dba;
 
@@ -185,6 +186,25 @@ class Addon
                dba::close($r);
        }
 
+       /**
+        * @brief Forks a hook.
+        *
+        * Use this function when you want to fork a hook via the worker.
+        *
+        * @param string $name of the hook to call
+        * @param string|array $data to transmit to the callback handler
+        */
+       public static function ForkHooks($priority, $name, $data = null)
+       {
+               $a = get_app();
+
+               if (is_array($a->hooks) && array_key_exists($name, $a->hooks)) {
+                       foreach ($a->hooks[$name] as $hook) {
+                               Worker::add($priority, 'ForkHook', $name, json_encode($hook), json_encode($data));
+                       }
+               }
+       }
+
        /**
         * @brief Calls a hook.
         *
index fd5a0bf4c3a3db97dae2c1aff4926df552b355f7..57d2fa971176880811ea1c517f1d9fe874cd1f7b 100644 (file)
@@ -970,9 +970,6 @@ class Worker
                // Run the cron job that calls all other jobs
                self::add(PRIORITY_MEDIUM, "Cron");
 
-               // Run the cronhooks job separately from cron for being able to use a different timing
-               self::add(PRIORITY_MEDIUM, "CronHooks");
-
                // Cleaning dead processes
                self::killStaleWorkers();
        }
index 6c4ceec48ede5247c9e857ee555624d52ab3893c..4c05782f178b118ef94d69fc5b65439f2ed49bcd 100644 (file)
@@ -1771,7 +1771,7 @@ class DBStructure
                                "comment" => "Background tasks queue entries",
                                "fields" => [
                                                "id" => ["type" => "int", "not null" => "1", "extra" => "auto_increment", "primary" => "1", "comment" => "Auto incremented worker task id"],
-                                               "parameter" => ["type" => "text", "comment" => "Task command"],
+                                               "parameter" => ["type" => "mediumtext", "comment" => "Task command"],
                                                "priority" => ["type" => "tinyint", "not null" => "1", "default" => "0", "comment" => "Task priority"],
                                                "created" => ["type" => "datetime", "not null" => "1", "default" => NULL_DATE, "comment" => "Creation date"],
                                                "pid" => ["type" => "int", "not null" => "1", "default" => "0", "comment" => "Process id of the worker"],
index ad7b4b7f1a5fa8bdd3b41745255c0a36cf41b2d5..6f4c20a8e869385b0d0c18bce3e1d1844b1d35d7 100644 (file)
@@ -40,6 +40,9 @@ Class Cron {
 
                logger('cron: start');
 
+               // Fork the cron jobs in separate parts to avoid problems when one of them is crashing
+               Addon::ForkHooks($a->queue['priority'], "cron");
+
                // run queue delivery process in the background
                Worker::add(PRIORITY_NEGLIGIBLE, "Queue");
 
diff --git a/src/Worker/CronHooks.php b/src/Worker/CronHooks.php
deleted file mode 100644 (file)
index 2bbe529..0000000
+++ /dev/null
@@ -1,61 +0,0 @@
-<?php
-/**
- * @file src/Worker/CronHooks.php
- */
-
-namespace Friendica\Worker;
-
-use Friendica\Core\Addon;
-use Friendica\Core\Config;
-use Friendica\Core\Worker;
-use Friendica\Util\DateTimeFormat;
-
-Class CronHooks {
-       public static function execute($hook = '') {
-               global $a;
-
-               if (($hook != '') && is_array($a->hooks) && array_key_exists("cron", $a->hooks)) {
-                       foreach ($a->hooks["cron"] as $single_hook) {
-                               if ($single_hook[1] == $hook) {
-                                       logger("Calling cron hook '" . $hook . "'", LOGGER_DEBUG);
-                                       Addon::callSingleHook($a, $hook, $single_hook);
-                               }
-                       }
-                       return;
-               }
-
-               $last = Config::get('system', 'last_cronhook');
-
-               $poll_interval = intval(Config::get('system', 'cronhook_interval'));
-               if (!$poll_interval) {
-                       $poll_interval = 9;
-               }
-
-               if ($last) {
-                       $next = $last + ($poll_interval * 60);
-                       if ($next > time()) {
-                               logger('cronhook intervall not reached');
-                               return;
-                       }
-               }
-
-               $a->set_baseurl(Config::get('system', 'url'));
-
-               logger('cronhooks: start');
-
-               $d = DateTimeFormat::utcNow();
-
-               if (is_array($a->hooks) && array_key_exists("cron", $a->hooks)) {
-                       foreach ($a->hooks["cron"] as $hook) {
-                               logger("Calling cronhooks for '" . $hook[1] . "'", LOGGER_DEBUG);
-                               Worker::add(PRIORITY_MEDIUM, "CronHooks", $hook[1]);
-                       }
-               }
-
-               logger('cronhooks: end');
-
-               Config::set('system', 'last_cronhook', time());
-
-               return;
-       }
-}
diff --git a/src/Worker/ForkHook.php b/src/Worker/ForkHook.php
new file mode 100644 (file)
index 0000000..1cd83bb
--- /dev/null
@@ -0,0 +1,19 @@
+<?php
+/**
+ * @file src/Worker/ForkHook.php
+ */
+
+namespace Friendica\Worker;
+
+use Friendica\Core\Addon;
+
+Class ForkHook {
+       public static function execute($name, $hook_json, $data_json) {
+               global $a;
+
+               $hook = json_decode($hook_json, true);
+               $data = json_decode($data_json, true);
+
+               Addon::callSingleHook($a, $name, $hook, $data);
+       }
+}
index 2dc5dca581605be853513ac98a1917c1fd8b506c..24ba77d6979803d45f72b0281e66d76aa15ba569 100644 (file)
@@ -553,7 +553,8 @@ class Notifier {
                logger('notifier: calling hooks', LOGGER_DEBUG);
 
                if ($normal_mode) {
-                       Addon::callHooks('notifier_normal',$target_item);
+                       Addon::ForkHooks($a->queue['priority'], 'notifier_normal', $target_item);
+                       //Addon::callHooks('notifier_normal',$target_item);
                }
 
                Addon::callHooks('notifier_end',$target_item);