1
0
Fork 0

Hook calls can now be forked into a worker queue entry

This commit is contained in:
Michael 2018-02-06 22:52:36 +00:00
parent 66e3c1836e
commit 47d165cb25
9 changed files with 47 additions and 68 deletions

View file

@ -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);
/**

View 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',

View 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.
*

View 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();
}

View 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"],

View 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");

View file

@ -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;
}
}

19
src/Worker/ForkHook.php Normal file
View file

@ -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);
}
}

View 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);