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
18 public static function updateTime($id)
20 Logger::log('queue: requeue item ' . $id);
21 $queue = DBA::selectFirst('queue', ['retrial'], ['id' => $id]);
22 if (!DBA::isResult($queue)) {
26 $retrial = $queue['retrial'];
29 self::removeItem($id);
32 // Calculate the delay until the next trial
33 $delay = (($retrial + 3) ** 4) + (rand(1, 30) * ($retrial + 1));
34 $next = DateTimeFormat::utc('now + ' . $delay . ' seconds');
36 DBA::update('queue', ['last' => DateTimeFormat::utcNow(), 'retrial' => $retrial + 1, 'next' => $next], ['id' => $id]);
40 * @param string $id id
43 public static function removeItem($id)
45 Logger::log('queue: remove queue item ' . $id);
46 DBA::delete('queue', ['id' => $id]);
50 * @brief Checks if the communication with a given contact had problems recently
52 * @param int $cid Contact id
54 * @return bool The communication with this contact has currently problems
57 public static function wasDelayed($cid)
59 // Are there queue entries that were recently added?
60 $r = q("SELECT `id` FROM `queue` WHERE `cid` = %d
61 AND `last` > UTC_TIMESTAMP() - INTERVAL 15 MINUTE LIMIT 1",
65 $was_delayed = DBA::isResult($r);
67 // We set "term-date" to a current date if the communication has problems.
68 // If the communication works again we reset this value.
70 $r = q("SELECT `term-date` FROM `contact` WHERE `id` = %d AND `term-date` <= '1000-01-01' LIMIT 1",
73 $was_delayed = !DBA::isResult($r);
80 * @param string $cid cid
81 * @param string $network network
82 * @param string $msg message
83 * @param boolean $batch batch, default false
85 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
87 public static function add($cid, $network, $msg, $batch = false, $guid = '')
90 $max_queue = Config::get('system', 'max_contact_queue');
95 $batch_queue = Config::get('system', 'max_batch_queue');
96 if ($batch_queue < 1) {
100 $r = q("SELECT COUNT(*) AS `total` FROM `queue` INNER JOIN `contact` ON `queue`.`cid` = `contact`.`id`
101 WHERE `queue`.`cid` = %d AND `contact`.`self` = 0 ",
105 if (DBA::isResult($r)) {
106 if ($batch && ($r[0]['total'] > $batch_queue)) {
107 Logger::log('too many queued items for batch server ' . $cid . ' - discarding message');
109 } elseif ((! $batch) && ($r[0]['total'] > $max_queue)) {
110 Logger::log('too many queued items for contact ' . $cid . ' - discarding message');
115 DBA::insert('queue', [
117 'network' => $network,
119 'created' => DateTimeFormat::utcNow(),
120 'last' => DateTimeFormat::utcNow(),
122 'batch' =>($batch) ? 1 : 0
124 Logger::log('Added item ' . $guid . ' for ' . $cid);