We now incrementally calculate the queue delivery time

This commit is contained in:
Michael 2018-02-08 18:39:48 +00:00
parent 06bce464ba
commit d70a20a466
5 changed files with 32 additions and 36 deletions

View File

@ -39,7 +39,7 @@ define('FRIENDICA_PLATFORM', 'Friendica');
define('FRIENDICA_CODENAME', 'Asparagus'); define('FRIENDICA_CODENAME', 'Asparagus');
define('FRIENDICA_VERSION', '3.6-dev'); define('FRIENDICA_VERSION', '3.6-dev');
define('DFRN_PROTOCOL_VERSION', '2.23'); define('DFRN_PROTOCOL_VERSION', '2.23');
define('DB_UPDATE_VERSION', 1250); define('DB_UPDATE_VERSION', 1251);
define('NEW_UPDATE_ROUTINE_VERSION', 1170); define('NEW_UPDATE_ROUTINE_VERSION', 1170);
/** /**

View File

@ -853,19 +853,18 @@ CREATE TABLE IF NOT EXISTS `push_subscriber` (
-- --
CREATE TABLE IF NOT EXISTS `queue` ( CREATE TABLE IF NOT EXISTS `queue` (
`id` int NOT NULL auto_increment COMMENT '', `id` int NOT NULL auto_increment COMMENT '',
`cid` int NOT NULL DEFAULT 0 COMMENT '', `cid` int NOT NULL DEFAULT 0 COMMENT 'Message receiver',
`network` varchar(32) NOT NULL DEFAULT '' COMMENT '', `network` varchar(32) NOT NULL DEFAULT '' COMMENT 'Receiver\'s network',
`guid` varchar(255) NOT NULL DEFAULT '' COMMENT '', `guid` varchar(255) NOT NULL DEFAULT '' COMMENT 'Unique GUID of the message',
`created` datetime NOT NULL DEFAULT '0001-01-01 00:00:00' COMMENT '', `created` datetime NOT NULL DEFAULT '0001-01-01 00:00:00' COMMENT 'Date, when the message was created',
`last` datetime NOT NULL DEFAULT '0001-01-01 00:00:00' COMMENT '', `last` datetime NOT NULL DEFAULT '0001-01-01 00:00:00' COMMENT 'Date of last trial',
`next` datetime NOT NULL DEFAULT '0001-01-01 00:00:00' COMMENT 'Next retrial date',
`retrial` tinyint NOT NULL DEFAULT 0 COMMENT 'Retrial counter',
`content` mediumtext COMMENT '', `content` mediumtext COMMENT '',
`batch` boolean NOT NULL DEFAULT '0' COMMENT '', `batch` boolean NOT NULL DEFAULT '0' COMMENT '',
PRIMARY KEY(`id`), PRIMARY KEY(`id`),
INDEX `cid` (`cid`),
INDEX `created` (`created`),
INDEX `last` (`last`), INDEX `last` (`last`),
INDEX `network` (`network`), INDEX `next` (`next`)
INDEX `batch` (`batch`)
) DEFAULT COLLATE utf8mb4_general_ci; ) DEFAULT COLLATE utf8mb4_general_ci;
-- --

View File

@ -1553,17 +1553,16 @@ class DBStructure
"network" => ["type" => "varchar(32)", "not null" => "1", "default" => "", "comment" => "Receiver's network"], "network" => ["type" => "varchar(32)", "not null" => "1", "default" => "", "comment" => "Receiver's network"],
"guid" => ["type" => "varchar(255)", "not null" => "1", "default" => "", "comment" => "Unique GUID of the message"], "guid" => ["type" => "varchar(255)", "not null" => "1", "default" => "", "comment" => "Unique GUID of the message"],
"created" => ["type" => "datetime", "not null" => "1", "default" => NULL_DATE, "comment" => "Date, when the message was created"], "created" => ["type" => "datetime", "not null" => "1", "default" => NULL_DATE, "comment" => "Date, when the message was created"],
"last" => ["type" => "datetime", "not null" => "1", "default" => NULL_DATE, "comment" => ""], "last" => ["type" => "datetime", "not null" => "1", "default" => NULL_DATE, "comment" => "Date of last trial"],
"next" => ["type" => "datetime", "not null" => "1", "default" => NULL_DATE, "comment" => "Next retrial date"],
"retrial" => ["type" => "tinyint", "not null" => "1", "default" => "0", "comment" => "Retrial counter"],
"content" => ["type" => "mediumtext", "comment" => ""], "content" => ["type" => "mediumtext", "comment" => ""],
"batch" => ["type" => "boolean", "not null" => "1", "default" => "0", "comment" => ""], "batch" => ["type" => "boolean", "not null" => "1", "default" => "0", "comment" => ""],
], ],
"indexes" => [ "indexes" => [
"PRIMARY" => ["id"], "PRIMARY" => ["id"],
"cid" => ["cid"],
"created" => ["created"],
"last" => ["last"], "last" => ["last"],
"network" => ["network"], "next" => ["next"],
"batch" => ["batch"],
] ]
]; ];
$database["register"] = [ $database["register"] = [

View File

@ -19,7 +19,22 @@ class Queue
public static function updateTime($id) public static function updateTime($id)
{ {
logger('queue: requeue item ' . $id); logger('queue: requeue item ' . $id);
dba::update('queue', ['last' => DateTimeFormat::utcNow()], ['id' => $id]); $queue = dba::selectFirst('queue', ['retrial'], ['id' => $id]);
if (!DBM::is_result($queue)) {
return;
}
$retrial = $queue['retrial'];
if ($retrial > 14) {
self::removeItem($id);
}
// Calculate the delay until the next trial
$delay = (($retrial + 3) ** 4) + (rand(1, 30) * ($retrial + 1));
$next = DateTimeFormat::utc(date('c', time() + $delay));
dba::update('queue', ['last' => DateTimeFormat::utcNow(), 'retrial' => $retrial + 1, 'next' => $next], ['id' => $id]);
} }
/** /**

View File

@ -32,30 +32,12 @@ class Queue
$no_dead_check = Config::get('system', 'queue_no_dead_check', false); $no_dead_check = Config::get('system', 'queue_no_dead_check', false);
if (!$queue_id) { if (!$queue_id) {
logger('queue: start'); logger('filling queue jobs - start');
// Handling the pubsubhubbub requests // Handling the pubsubhubbub requests
Worker::add(['priority' => PRIORITY_HIGH, 'dont_fork' => true], 'PubSubPublish'); Worker::add(['priority' => PRIORITY_HIGH, 'dont_fork' => true], 'PubSubPublish');
$r = q( $r = dba::inArray(dba::p("SELECT `id` FROM `queue` WHERE `next` < UTC_TIMESTAMP()"));
"SELECT `queue`.*, `contact`.`name`, `contact`.`uid` FROM `queue`
INNER JOIN `contact` ON `queue`.`cid` = `contact`.`id`
WHERE `queue`.`created` < UTC_TIMESTAMP() - INTERVAL 3 DAY"
);
if (DBM::is_result($r)) {
foreach ($r as $rr) {
logger('Removing expired queue item for ' . $rr['name'] . ', uid=' . $rr['uid']);
logger('Expired queue data: ' . $rr['content'], LOGGER_DATA);
}
q("DELETE FROM `queue` WHERE `created` < UTC_TIMESTAMP() - INTERVAL 3 DAY");
}
/*
* For the first 12 hours we'll try to deliver every 15 minutes
* After that, we'll only attempt delivery once per hour.
*/
$r = q("SELECT `id` FROM `queue` WHERE ((`created` > UTC_TIMESTAMP() - INTERVAL 12 HOUR AND `last` < UTC_TIMESTAMP() - INTERVAL 15 MINUTE) OR (`last` < UTC_TIMESTAMP() - INTERVAL 1 HOUR)) ORDER BY `cid`, `created`");
Addon::callHooks('queue_predeliver', $r); Addon::callHooks('queue_predeliver', $r);
@ -65,6 +47,7 @@ class Queue
Worker::add(['priority' => PRIORITY_LOW, 'dont_fork' => true], "Queue", (int) $q_item['id']); Worker::add(['priority' => PRIORITY_LOW, 'dont_fork' => true], "Queue", (int) $q_item['id']);
} }
} }
logger('filling queue jobs - end');
return; return;
} }