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