]> git.mxchange.org Git - friendica.git/blob - src/Model/Queue.php
Merge pull request #4254 from zeroadam/Queue-#3878
[friendica.git] / src / Model / Queue.php
1 <?php
2 /**
3  * @file src/Model/Queue.php
4  */
5 namespace Friendica\Model;
6
7 use Friendica\Core\Config;
8 use Friendica\Database\DBM;
9 use dba;
10
11 require_once 'include/dba.php';
12 require_once 'include/datetime.php';
13
14 class Queue
15 {
16         /**
17          * @param string $id id
18          */
19         public static function updateTime($id)
20         {
21                 logger('queue: requeue item ' . $id);
22                 dba::update('queue', ['last' => datetime_convert()], ['id' => $id]);
23         }
24         
25         /**
26          * @param string $id id
27          */
28         public static function removeItem($id)
29         {
30                 logger('queue: remove queue item ' . $id);
31                 dba::delete('queue', ['id' => $id]);
32         }
33         
34         /**
35          * @brief Checks if the communication with a given contact had problems recently
36          *
37          * @param int $cid Contact id
38          *
39          * @return bool The communication with this contact has currently problems
40          */
41         public static function wasDelayed($cid)
42         {
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",
46                         intval($cid)
47                 );
48         
49                 $was_delayed = DBM::is_result($r);
50         
51                 // We set "term-date" to a current date if the communication has problems.
52                 // If the communication works again we reset this value.
53                 if ($was_delayed) {
54                         $r = q("SELECT `term-date` FROM `contact` WHERE `id` = %d AND `term-date` <= '1000-01-01' LIMIT 1",
55                                 intval($cid)
56                         );
57                         $was_delayed = !DBM::is_result($r);
58                 }
59         
60                 return $was_delayed;
61         }
62         
63         /**
64          * @param string  $cid     cid
65          * @param string  $network network
66          * @param string  $msg     message
67          * @param boolean $batch   batch, default false
68          */
69         public static function add($cid, $network, $msg, $batch = false)
70         {
71         
72                 $max_queue = Config::get('system', 'max_contact_queue');
73                 if ($max_queue < 1) {
74                         $max_queue = 500;
75                 }
76         
77                 $batch_queue = Config::get('system', 'max_batch_queue');
78                 if ($batch_queue < 1) {
79                         $batch_queue = 1000;
80                 }
81         
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 ",
84                         intval($cid)
85                 );
86
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');
90                                 return;
91                         } elseif ((! $batch) && ($r[0]['total'] > $max_queue)) {
92                                 logger('add_to_queue: too many queued items for contact ' . $cid . ' - discarding message');
93                                 return;
94                         }
95                 }
96         
97                 dba::insert('queue', ['cid' => $cid, 'network' => $network, 'created' => datetime_convert(), 'last' => datetime_convert(), 'content' => $msg, 'batch' =>($batch) ? 1 : 0]);
98         }
99 }