Merge pull request #4423 from annando/requeue

We now incrementally calculate the queue delivery time
This commit is contained in:
Hypolite Petovan 2018-02-08 15:11:05 -05:00 committed by GitHub
commit e651af2a0f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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_VERSION', '3.6-dev');
define('DFRN_PROTOCOL_VERSION', '2.23');
define('DB_UPDATE_VERSION', 1250);
define('DB_UPDATE_VERSION', 1251);
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` (
`id` int NOT NULL auto_increment COMMENT '',
`cid` int NOT NULL DEFAULT 0 COMMENT '',
`network` varchar(32) NOT NULL DEFAULT '' COMMENT '',
`guid` varchar(255) NOT NULL DEFAULT '' COMMENT '',
`created` datetime NOT NULL DEFAULT '0001-01-01 00:00:00' COMMENT '',
`last` datetime NOT NULL DEFAULT '0001-01-01 00:00:00' COMMENT '',
`cid` int NOT NULL DEFAULT 0 COMMENT 'Message receiver',
`network` varchar(32) NOT NULL DEFAULT '' COMMENT 'Receiver\'s network',
`guid` varchar(255) NOT NULL DEFAULT '' COMMENT 'Unique GUID of the message',
`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 '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 '',
`batch` boolean NOT NULL DEFAULT '0' COMMENT '',
PRIMARY KEY(`id`),
INDEX `cid` (`cid`),
INDEX `created` (`created`),
INDEX `last` (`last`),
INDEX `network` (`network`),
INDEX `batch` (`batch`)
INDEX `next` (`next`)
) 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"],
"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"],
"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" => ""],
"batch" => ["type" => "boolean", "not null" => "1", "default" => "0", "comment" => ""],
],
"indexes" => [
"PRIMARY" => ["id"],
"cid" => ["cid"],
"created" => ["created"],
"last" => ["last"],
"network" => ["network"],
"batch" => ["batch"],
"next" => ["next"],
]
];
$database["register"] = [

View file

@ -19,7 +19,22 @@ class Queue
public static function updateTime($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('now + ' . $delay . ' seconds');
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);
if (!$queue_id) {
logger('queue: start');
logger('filling queue jobs - start');
// Handling the pubsubhubbub requests
Worker::add(['priority' => PRIORITY_HIGH, 'dont_fork' => true], 'PubSubPublish');
$r = q(
"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`");
$r = dba::inArray(dba::p("SELECT `id` FROM `queue` WHERE `next` < UTC_TIMESTAMP()"));
Addon::callHooks('queue_predeliver', $r);
@ -65,6 +47,7 @@ class Queue
Worker::add(['priority' => PRIORITY_LOW, 'dont_fork' => true], "Queue", (int) $q_item['id']);
}
}
logger('filling queue jobs - end');
return;
}