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