Merge pull request #4407 from annando/addon-unix
Hook calls can now be forked into a worker queue entry
This commit is contained in:
commit
c21a0042ad
2
boot.php
2
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);
|
||||
|
||||
/**
|
||||
|
|
|
@ -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',
|
||||
|
|
|
@ -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.
|
||||
*
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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"],
|
||||
|
|
|
@ -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");
|
||||
|
||||
|
|
|
@ -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
19
src/Worker/ForkHook.php
Normal 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);
|
||||
}
|
||||
}
|
|
@ -553,7 +553,7 @@ 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_end',$target_item);
|
||||
|
|
Loading…
Reference in a new issue