3 * @file src/Model/Queue.php
5 namespace Friendica\Model;
7 use Friendica\Core\Config;
8 use Friendica\Database\DBM;
9 use Friendica\Util\DateTimeFormat;
12 require_once 'include/dba.php';
17 * @param string $id id
19 public static function updateTime($id)
21 logger('queue: requeue item ' . $id);
22 dba::update('queue', ['last' => DateTimeFormat::utcNow()], ['id' => $id]);
26 * @param string $id id
28 public static function removeItem($id)
30 logger('queue: remove queue item ' . $id);
31 dba::delete('queue', ['id' => $id]);
35 * @brief Checks if the communication with a given contact had problems recently
37 * @param int $cid Contact id
39 * @return bool The communication with this contact has currently problems
41 public static function wasDelayed($cid)
43 // Are there queue entries that were recently added?
44 $r = q("SELECT `id` FROM `queue` WHERE `cid` = %d
45 AND `last` > UTC_TIMESTAMP() - INTERVAL 15 MINUTE LIMIT 1",
49 $was_delayed = DBM::is_result($r);
51 // We set "term-date" to a current date if the communication has problems.
52 // If the communication works again we reset this value.
54 $r = q("SELECT `term-date` FROM `contact` WHERE `id` = %d AND `term-date` <= '1000-01-01' LIMIT 1",
57 $was_delayed = !DBM::is_result($r);
64 * @param string $cid cid
65 * @param string $network network
66 * @param string $msg message
67 * @param boolean $batch batch, default false
69 public static function add($cid, $network, $msg, $batch = false)
72 $max_queue = Config::get('system', 'max_contact_queue');
77 $batch_queue = Config::get('system', 'max_batch_queue');
78 if ($batch_queue < 1) {
82 $r = q("SELECT COUNT(*) AS `total` FROM `queue` INNER JOIN `contact` ON `queue`.`cid` = `contact`.`id`
83 WHERE `queue`.`cid` = %d AND `contact`.`self` = 0 ",
87 if (DBM::is_result($r)) {
88 if ($batch && ($r[0]['total'] > $batch_queue)) {
89 logger('add_to_queue: too many queued items for batch server ' . $cid . ' - discarding message');
91 } elseif ((! $batch) && ($r[0]['total'] > $max_queue)) {
92 logger('add_to_queue: too many queued items for contact ' . $cid . ' - discarding message');
97 dba::insert('queue', [
99 'network' => $network,
100 'created' => DateTimeFormat::utcNow(),
101 'last' => DateTimeFormat::utcNow(),
103 'batch' =>($batch) ? 1 : 0