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