]> git.mxchange.org Git - friendica.git/blob - src/Model/Queue.php
0284d72b7d670fda2421e707666caf3d0a649a23
[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\DBA;
9 use Friendica\Util\DateTimeFormat;
10
11 require_once 'include/dba.php';
12
13 class Queue
14 {
15         /**
16          * @param string $id id
17          */
18         public static function updateTime($id)
19         {
20                 logger('queue: requeue item ' . $id);
21                 $queue = DBA::selectFirst('queue', ['retrial'], ['id' => $id]);
22                 if (!DBA::isResult($queue)) {
23                         return;
24                 }
25
26                 $retrial = $queue['retrial'];
27
28                 if ($retrial > 14) {
29                         self::removeItem($id);
30                 }
31
32                 // Calculate the delay until the next trial
33                 $delay = (($retrial + 3) ** 4) + (rand(1, 30) * ($retrial + 1));
34                 $next = DateTimeFormat::utc('now + ' . $delay . ' seconds');
35
36                 DBA::update('queue', ['last' => DateTimeFormat::utcNow(), 'retrial' => $retrial + 1, 'next' => $next], ['id' => $id]);
37         }
38
39         /**
40          * @param string $id id
41          */
42         public static function removeItem($id)
43         {
44                 logger('queue: remove queue item ' . $id);
45                 DBA::delete('queue', ['id' => $id]);
46         }
47
48         /**
49          * @brief Checks if the communication with a given contact had problems recently
50          *
51          * @param int $cid Contact id
52          *
53          * @return bool The communication with this contact has currently problems
54          */
55         public static function wasDelayed($cid)
56         {
57                 // Are there queue entries that were recently added?
58                 $r = q("SELECT `id` FROM `queue` WHERE `cid` = %d
59                         AND `last` > UTC_TIMESTAMP() - INTERVAL 15 MINUTE LIMIT 1",
60                         intval($cid)
61                 );
62
63                 $was_delayed = DBA::isResult($r);
64
65                 // We set "term-date" to a current date if the communication has problems.
66                 // If the communication works again we reset this value.
67                 if ($was_delayed) {
68                         $r = q("SELECT `term-date` FROM `contact` WHERE `id` = %d AND `term-date` <= '1000-01-01' LIMIT 1",
69                                 intval($cid)
70                         );
71                         $was_delayed = !DBA::isResult($r);
72                 }
73
74                 return $was_delayed;
75         }
76
77         /**
78          * @param string  $cid     cid
79          * @param string  $network network
80          * @param string  $msg     message
81          * @param boolean $batch   batch, default false
82          */
83         public static function add($cid, $network, $msg, $batch = false, $guid = '')
84         {
85
86                 $max_queue = Config::get('system', 'max_contact_queue');
87                 if ($max_queue < 1) {
88                         $max_queue = 500;
89                 }
90
91                 $batch_queue = Config::get('system', 'max_batch_queue');
92                 if ($batch_queue < 1) {
93                         $batch_queue = 1000;
94                 }
95
96                 $r = q("SELECT COUNT(*) AS `total` FROM `queue` INNER JOIN `contact` ON `queue`.`cid` = `contact`.`id`
97                         WHERE `queue`.`cid` = %d AND `contact`.`self` = 0 ",
98                         intval($cid)
99                 );
100
101                 if (DBA::isResult($r)) {
102                         if ($batch &&  ($r[0]['total'] > $batch_queue)) {
103                                 logger('too many queued items for batch server ' . $cid . ' - discarding message');
104                                 return;
105                         } elseif ((! $batch) && ($r[0]['total'] > $max_queue)) {
106                                 logger('too many queued items for contact ' . $cid . ' - discarding message');
107                                 return;
108                         }
109                 }
110
111                 DBA::insert('queue', [
112                         'cid'     => $cid,
113                         'network' => $network,
114                         'guid'     => $guid,
115                         'created' => DateTimeFormat::utcNow(),
116                         'last'    => DateTimeFormat::utcNow(),
117                         'content' => $msg,
118                         'batch'   =>($batch) ? 1 : 0
119                 ]);
120                 logger('Added item ' . $guid . ' for ' . $cid);
121         }
122 }