3 * @file src/Model/Queue.php
5 namespace Friendica\Model;
7 use Friendica\Core\Config;
8 use Friendica\Database\DBM;
9 use Friendica\Util\Temporal;
12 require_once 'include/dba.php';
13 require_once 'include/datetime.php';
18 * @param string $id id
20 public static function updateTime($id)
22 logger('queue: requeue item ' . $id);
23 dba::update('queue', ['last' => Temporal::utcNow()], ['id' => $id]);
27 * @param string $id id
29 public static function removeItem($id)
31 logger('queue: remove queue item ' . $id);
32 dba::delete('queue', ['id' => $id]);
36 * @brief Checks if the communication with a given contact had problems recently
38 * @param int $cid Contact id
40 * @return bool The communication with this contact has currently problems
42 public static function wasDelayed($cid)
44 // Are there queue entries that were recently added?
45 $r = q("SELECT `id` FROM `queue` WHERE `cid` = %d
46 AND `last` > UTC_TIMESTAMP() - INTERVAL 15 MINUTE LIMIT 1",
50 $was_delayed = DBM::is_result($r);
52 // We set "term-date" to a current date if the communication has problems.
53 // If the communication works again we reset this value.
55 $r = q("SELECT `term-date` FROM `contact` WHERE `id` = %d AND `term-date` <= '1000-01-01' LIMIT 1",
58 $was_delayed = !DBM::is_result($r);
65 * @param string $cid cid
66 * @param string $network network
67 * @param string $msg message
68 * @param boolean $batch batch, default false
70 public static function add($cid, $network, $msg, $batch = false)
73 $max_queue = Config::get('system', 'max_contact_queue');
78 $batch_queue = Config::get('system', 'max_batch_queue');
79 if ($batch_queue < 1) {
83 $r = q("SELECT COUNT(*) AS `total` FROM `queue` INNER JOIN `contact` ON `queue`.`cid` = `contact`.`id`
84 WHERE `queue`.`cid` = %d AND `contact`.`self` = 0 ",
88 if (DBM::is_result($r)) {
89 if ($batch && ($r[0]['total'] > $batch_queue)) {
90 logger('add_to_queue: too many queued items for batch server ' . $cid . ' - discarding message');
92 } elseif ((! $batch) && ($r[0]['total'] > $max_queue)) {
93 logger('add_to_queue: too many queued items for contact ' . $cid . ' - discarding message');
98 dba::insert('queue', ['cid' => $cid, 'network' => $network, 'created' => Temporal::utcNow(), 'last' => Temporal::utcNow(), 'content' => $msg, 'batch' =>($batch) ? 1 : 0]);