3 * @file src/Model/Queue.php
5 namespace Friendica\Model;
7 use Friendica\Core\Config;
8 use Friendica\Core\Logger;
9 use Friendica\Database\DBA;
10 use Friendica\Util\DateTimeFormat;
15 * @param string $id id
17 public static function updateTime($id)
19 Logger::log('queue: requeue item ' . $id);
20 $queue = DBA::selectFirst('queue', ['retrial'], ['id' => $id]);
21 if (!DBA::isResult($queue)) {
25 $retrial = $queue['retrial'];
28 self::removeItem($id);
31 // Calculate the delay until the next trial
32 $delay = (($retrial + 3) ** 4) + (rand(1, 30) * ($retrial + 1));
33 $next = DateTimeFormat::utc('now + ' . $delay . ' seconds');
35 DBA::update('queue', ['last' => DateTimeFormat::utcNow(), 'retrial' => $retrial + 1, 'next' => $next], ['id' => $id]);
39 * @param string $id id
41 public static function removeItem($id)
43 Logger::log('queue: remove queue item ' . $id);
44 DBA::delete('queue', ['id' => $id]);
48 * @brief Checks if the communication with a given contact had problems recently
50 * @param int $cid Contact id
52 * @return bool The communication with this contact has currently problems
54 public static function wasDelayed($cid)
56 // Are there queue entries that were recently added?
57 $r = q("SELECT `id` FROM `queue` WHERE `cid` = %d
58 AND `last` > UTC_TIMESTAMP() - INTERVAL 15 MINUTE LIMIT 1",
62 $was_delayed = DBA::isResult($r);
64 // We set "term-date" to a current date if the communication has problems.
65 // If the communication works again we reset this value.
67 $r = q("SELECT `term-date` FROM `contact` WHERE `id` = %d AND `term-date` <= '1000-01-01' LIMIT 1",
70 $was_delayed = !DBA::isResult($r);
77 * @param string $cid cid
78 * @param string $network network
79 * @param string $msg message
80 * @param boolean $batch batch, default false
82 public static function add($cid, $network, $msg, $batch = false, $guid = '')
85 $max_queue = Config::get('system', 'max_contact_queue');
90 $batch_queue = Config::get('system', 'max_batch_queue');
91 if ($batch_queue < 1) {
95 $r = q("SELECT COUNT(*) AS `total` FROM `queue` INNER JOIN `contact` ON `queue`.`cid` = `contact`.`id`
96 WHERE `queue`.`cid` = %d AND `contact`.`self` = 0 ",
100 if (DBA::isResult($r)) {
101 if ($batch && ($r[0]['total'] > $batch_queue)) {
102 Logger::log('too many queued items for batch server ' . $cid . ' - discarding message');
104 } elseif ((! $batch) && ($r[0]['total'] > $max_queue)) {
105 Logger::log('too many queued items for contact ' . $cid . ' - discarding message');
110 DBA::insert('queue', [
112 'network' => $network,
114 'created' => DateTimeFormat::utcNow(),
115 'last' => DateTimeFormat::utcNow(),
117 'batch' =>($batch) ? 1 : 0
119 Logger::log('Added item ' . $guid . ' for ' . $cid);