]> git.mxchange.org Git - friendica.git/blob - include/queue_fn.php
Issue-#3873
[friendica.git] / include / queue_fn.php
1 <?php
2
3 use Friendica\Core\Config;
4
5 function update_queue_time($id) {
6         logger('queue: requeue item ' . $id);
7         q("UPDATE `queue` SET `last` = '%s' WHERE `id` = %d",
8                 dbesc(datetime_convert()),
9                 intval($id)
10         );
11 }
12
13 function remove_queue_item($id) {
14         logger('queue: remove queue item ' . $id);
15         q("DELETE FROM `queue` WHERE `id` = %d",
16                 intval($id)
17         );
18 }
19
20 /**
21  * @brief Checks if the communication with a given contact had problems recently
22  *
23  * @param int $cid Contact id
24  *
25  * @return bool The communication with this contact has currently problems
26  */
27 function was_recently_delayed($cid) {
28         $was_delayed = false;
29
30         // Are there queue entries that were recently added?
31         $r = q("SELECT `id` FROM `queue` WHERE `cid` = %d
32                 AND `last` > UTC_TIMESTAMP() - INTERVAL 15 MINUTE LIMIT 1",
33                 intval($cid)
34         );
35
36         $was_delayed = dbm::is_result($r);
37
38         // We set "term-date" to a current date if the communication has problems.
39         // If the communication works again we reset this value.
40         if ($was_delayed) {
41                 $r = q("SELECT `term-date` FROM `contact` WHERE `id` = %d AND `term-date` <= '1000-01-01' LIMIT 1",
42                         intval($cid)
43                 );
44                 $was_delayed = !dbm::is_result($r);
45         }
46
47         return $was_delayed;
48 }
49
50
51 function add_to_queue($cid,$network,$msg,$batch = false) {
52
53         $max_queue = Config::get('system','max_contact_queue');
54         if ($max_queue < 1) {
55                 $max_queue = 500;
56         }
57
58         $batch_queue = Config::get('system','max_batch_queue');
59         if ($batch_queue < 1) {
60                 $batch_queue = 1000;
61         }
62
63         $r = q("SELECT COUNT(*) AS `total` FROM `queue` INNER JOIN `contact` ON `queue`.`cid` = `contact`.`id` 
64                 WHERE `queue`.`cid` = %d AND `contact`.`self` = 0 ",
65                 intval($cid)
66         );
67         if (dbm::is_result($r)) {
68                 if ($batch &&  ($r[0]['total'] > $batch_queue)) {
69                         logger('add_to_queue: too many queued items for batch server ' . $cid . ' - discarding message');
70                         return;
71                 } elseif ((! $batch) && ($r[0]['total'] > $max_queue)) {
72                         logger('add_to_queue: too many queued items for contact ' . $cid . ' - discarding message');
73                         return;
74                 }
75         }
76
77         q("INSERT INTO `queue` ( `cid`, `network`, `created`, `last`, `content`, `batch`)
78                 VALUES ( %d, '%s', '%s', '%s', '%s', %d) ",
79                 intval($cid),
80                 dbesc($network),
81                 dbesc(datetime_convert()),
82                 dbesc(datetime_convert()),
83                 dbesc($msg),
84                 intval(($batch) ? 1: 0)
85         );
86
87 }