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