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