From: Michael Date: Tue, 6 Feb 2018 22:52:36 +0000 (+0000) Subject: Hook calls can now be forked into a worker queue entry X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=47d165cb2572077579b10cc3249d3cd6443eb94f;p=friendica.git Hook calls can now be forked into a worker queue entry --- diff --git a/boot.php b/boot.php index 004ebf3ade..116c712a83 100644 --- 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); /** diff --git a/database.sql b/database.sql index 7433213107..85cd811c1b 100644 --- a/database.sql +++ b/database.sql @@ -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', diff --git a/src/Core/Addon.php b/src/Core/Addon.php index a721bd67ea..394dbe5623 100644 --- a/src/Core/Addon.php +++ b/src/Core/Addon.php @@ -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. * diff --git a/src/Core/Worker.php b/src/Core/Worker.php index fd5a0bf4c3..57d2fa9711 100644 --- a/src/Core/Worker.php +++ b/src/Core/Worker.php @@ -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(); } diff --git a/src/Database/DBStructure.php b/src/Database/DBStructure.php index 6c4ceec48e..4c05782f17 100644 --- a/src/Database/DBStructure.php +++ b/src/Database/DBStructure.php @@ -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"], diff --git a/src/Worker/Cron.php b/src/Worker/Cron.php index ad7b4b7f1a..6f4c20a8e8 100644 --- a/src/Worker/Cron.php +++ b/src/Worker/Cron.php @@ -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 index 2bbe529ba9..0000000000 --- a/src/Worker/CronHooks.php +++ /dev/null @@ -1,61 +0,0 @@ -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 index 0000000000..1cd83bb3b3 --- /dev/null +++ b/src/Worker/ForkHook.php @@ -0,0 +1,19 @@ +queue['priority'], 'notifier_normal', $target_item); + //Addon::callHooks('notifier_normal',$target_item); } Addon::callHooks('notifier_end',$target_item);