]> git.mxchange.org Git - friendica.git/blob - src/Model/Queue.php
Fix mods/README.md format
[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 require_once 'include/dba.php';
13
14 class Queue
15 {
16         /**
17          * @param string $id id
18          */
19         public static function updateTime($id)
20         {
21                 Logger::log('queue: requeue item ' . $id);
22                 $queue = DBA::selectFirst('queue', ['retrial'], ['id' => $id]);
23                 if (!DBA::isResult($queue)) {
24                         return;
25                 }
26
27                 $retrial = $queue['retrial'];
28
29                 if ($retrial > 14) {
30                         self::removeItem($id);
31                 }
32
33                 // Calculate the delay until the next trial
34                 $delay = (($retrial + 3) ** 4) + (rand(1, 30) * ($retrial + 1));
35                 $next = DateTimeFormat::utc('now + ' . $delay . ' seconds');
36
37                 DBA::update('queue', ['last' => DateTimeFormat::utcNow(), 'retrial' => $retrial + 1, 'next' => $next], ['id' => $id]);
38         }
39
40         /**
41          * @param string $id id
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          */
84         public static function add($cid, $network, $msg, $batch = false, $guid = '')
85         {
86
87                 $max_queue = Config::get('system', 'max_contact_queue');
88                 if ($max_queue < 1) {
89                         $max_queue = 500;
90                 }
91
92                 $batch_queue = Config::get('system', 'max_batch_queue');
93                 if ($batch_queue < 1) {
94                         $batch_queue = 1000;
95                 }
96
97                 $r = q("SELECT COUNT(*) AS `total` FROM `queue` INNER JOIN `contact` ON `queue`.`cid` = `contact`.`id`
98                         WHERE `queue`.`cid` = %d AND `contact`.`self` = 0 ",
99                         intval($cid)
100                 );
101
102                 if (DBA::isResult($r)) {
103                         if ($batch &&  ($r[0]['total'] > $batch_queue)) {
104                                 Logger::log('too many queued items for batch server ' . $cid . ' - discarding message');
105                                 return;
106                         } elseif ((! $batch) && ($r[0]['total'] > $max_queue)) {
107                                 Logger::log('too many queued items for contact ' . $cid . ' - discarding message');
108                                 return;
109                         }
110                 }
111
112                 DBA::insert('queue', [
113                         'cid'     => $cid,
114                         'network' => $network,
115                         'guid'     => $guid,
116                         'created' => DateTimeFormat::utcNow(),
117                         'last'    => DateTimeFormat::utcNow(),
118                         'content' => $msg,
119                         'batch'   =>($batch) ? 1 : 0
120                 ]);
121                 Logger::log('Added item ' . $guid . ' for ' . $cid);
122         }
123 }