]> git.mxchange.org Git - friendica.git/blob - include/queue.php
Optimized query for nodeinfo, better way to analyze posting problems with DFRN
[friendica.git] / include / queue.php
1 <?php
2
3 use \Friendica\Core\Config;
4
5 require_once("boot.php");
6 require_once('include/queue_fn.php');
7 require_once('include/dfrn.php');
8 require_once('include/cache.php');
9
10 function queue_run(&$argv, &$argc){
11         global $a, $db;
12
13         if (is_null($a)){
14                 $a = new App;
15         }
16
17         if (is_null($db)){
18                 @include(".htconfig.php");
19                 require_once("include/dba.php");
20                 $db = new dba($db_host, $db_user, $db_pass, $db_data);
21                 unset($db_host, $db_user, $db_pass, $db_data);
22         };
23
24         require_once("include/session.php");
25         require_once("include/datetime.php");
26         require_once('include/items.php');
27         require_once('include/bbcode.php');
28         require_once('include/socgraph.php');
29
30         Config::load();
31
32         // Don't check this stuff if the function is called by the poller
33         if (App::callstack() != "poller_run")
34                 if (App::is_already_running('queue', 'include/queue.php', 540))
35                         return;
36
37         $a->set_baseurl(get_config('system','url'));
38
39         load_hooks();
40
41         if ($argc > 1) {
42                 $queue_id = intval($argv[1]);
43         } else {
44                 $queue_id = 0;
45         }
46
47         $cachekey_deadguy = 'queue_run:deadguy:';
48         $cachekey_server = 'queue_run:server:';
49
50         if (!$queue_id) {
51
52                 logger('queue: start');
53
54                 // Handling the pubsubhubbub requests
55                 proc_run(PRIORITY_HIGH,'include/pubsubpublish.php');
56
57                 $interval = ((get_config('system','delivery_interval') === false) ? 2 : intval(get_config('system','delivery_interval')));
58
59                 // If we are using the worker we don't need a delivery interval
60                 if (get_config("system", "worker"))
61                         $interval = false;
62
63                 $r = q("select * from deliverq where 1");
64                 if ($r) {
65                         foreach ($r as $rr) {
66                                 logger('queue: deliverq');
67                                 proc_run(PRIORITY_HIGH,'include/delivery.php',$rr['cmd'],$rr['item'],$rr['contact']);
68                                 if ($interval) {
69                                         time_sleep_until(microtime(true) + (float) $interval);
70                                 }
71                         }
72                 }
73
74                 $r = q("SELECT `queue`.*, `contact`.`name`, `contact`.`uid` FROM `queue`
75                         INNER JOIN `contact` ON `queue`.`cid` = `contact`.`id`
76                         WHERE `queue`.`created` < UTC_TIMESTAMP() - INTERVAL 3 DAY");
77                 if ($r) {
78                         foreach ($r as $rr) {
79                                 logger('Removing expired queue item for ' . $rr['name'] . ', uid=' . $rr['uid']);
80                                 logger('Expired queue data :' . $rr['content'], LOGGER_DATA);
81                         }
82                         q("DELETE FROM `queue` WHERE `created` < UTC_TIMESTAMP() - INTERVAL 3 DAY");
83                 }
84
85                 // For the first 12 hours we'll try to deliver every 15 minutes
86                 // After that, we'll only attempt delivery once per hour.
87
88                 $r = q("SELECT `id` FROM `queue` WHERE ((`created` > UTC_TIMESTAMP() - INTERVAL 12 HOUR && `last` < UTC_TIMESTAMP() - INTERVAL 15 MINUTE) OR (`last` < UTC_TIMESTAMP() - INTERVAL 1 HOUR)) ORDER BY `cid`, `created`");
89         } else {
90                 logger('queue: start for id '.$queue_id);
91
92                 $r = q("SELECT `id` FROM `queue` WHERE `id` = %d LIMIT 1",
93                         intval($queue_id)
94                 );
95         }
96
97         if (!$r){
98                 return;
99         }
100
101         if (!$queue_id)
102                 call_hooks('queue_predeliver', $a, $r);
103
104
105         // delivery loop
106
107         require_once('include/salmon.php');
108         require_once('include/diaspora.php');
109
110         foreach($r as $q_item) {
111
112                 // queue_predeliver hooks may have changed the queue db details,
113                 // so check again if this entry still needs processing
114
115                 if ($queue_id) {
116                         $qi = q("SELECT * FROM `queue` WHERE `id` = %d LIMIT 1",
117                                 intval($queue_id));
118                 } elseif (get_config("system", "worker")) {
119                         logger('Call queue for id '.$q_item['id']);
120                         proc_run(PRIORITY_LOW, "include/queue.php", $q_item['id']);
121                         continue;
122                 } else
123                         $qi = q("SELECT * FROM `queue` WHERE `id` = %d AND `last` < UTC_TIMESTAMP() - INTERVAL 15 MINUTE ",
124                                 intval($q_item['id']));
125
126                 if (!dbm::is_result($qi)) {
127                         continue;
128                 }
129
130
131                 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
132                         intval($qi[0]['cid'])
133                 );
134                 if (! dbm::is_result($c)) {
135                         remove_queue_item($q_item['id']);
136                         continue;
137                 }
138
139                 $dead = Cache::get($cachekey_deadguy.$c[0]['notify']);
140
141                 if (!is_null($dead) AND $dead) {
142                         logger('queue: skipping known dead url: '.$c[0]['notify']);
143                         update_queue_time($q_item['id']);
144                         continue;
145                 }
146
147                 $server = poco_detect_server($c[0]['url']);
148
149                 if ($server != "") {
150                         $vital = Cache::get($cachekey_server.$server);
151
152                         if (is_null($vital)) {
153                                 logger("Check server ".$server." (".$c[0]["network"].")");
154
155                                 $vital = poco_check_server($server, $c[0]["network"], true);
156                                 Cache::set($cachekey_server.$server, $vital, CACHE_QUARTER_HOUR);
157                         }
158
159                         if (!is_null($vital) AND !$vital) {
160                                 logger('queue: skipping dead server: '.$server);
161                                 update_queue_time($q_item['id']);
162                                 continue;
163                         }
164                 }
165
166                 $u = q("SELECT `user`.*, `user`.`pubkey` AS `upubkey`, `user`.`prvkey` AS `uprvkey`
167                         FROM `user` WHERE `uid` = %d LIMIT 1",
168                         intval($c[0]['uid'])
169                 );
170                 if (! dbm::is_result($u)) {
171                         remove_queue_item($q_item['id']);
172                         continue;
173                 }
174
175                 $data      = $qi[0]['content'];
176                 $public    = $qi[0]['batch'];
177                 $contact   = $c[0];
178                 $owner     = $u[0];
179
180                 $deliver_status = 0;
181
182                 switch($contact['network']) {
183                         case NETWORK_DFRN:
184                                 logger('queue: dfrndelivery: item '.$q_item['id'].' for '.$contact['name'].' <'.$contact['url'].'>');
185                                 $deliver_status = dfrn::deliver($owner,$contact,$data);
186
187                                 if ($deliver_status < 0) {
188                                         update_queue_time($q_item['id']);
189                                         Cache::set($cachekey_deadguy.$contact['notify'], true, CACHE_QUARTER_HOUR);
190                                 } else {
191                                         remove_queue_item($q_item['id']);
192                                 }
193                                 break;
194                         case NETWORK_OSTATUS:
195                                 if ($contact['notify']) {
196                                         logger('queue: slapdelivery: item '.$q_item['id'].' for '.$contact['name'].' <'.$contact['url'].'>');
197                                         $deliver_status = slapper($owner,$contact['notify'],$data);
198
199                                         if ($deliver_status == (-1)) {
200                                                 update_queue_time($q_item['id']);
201                                                 Cache::set($cachekey_deadguy.$contact['notify'], true, CACHE_QUARTER_HOUR);
202                                         } else {
203                                                 remove_queue_item($q_item['id']);
204                                         }
205                                 }
206                                 break;
207                         case NETWORK_DIASPORA:
208                                 if ($contact['notify']) {
209                                         logger('queue: diaspora_delivery: item '.$q_item['id'].' for '.$contact['name'].' <'.$contact['url'].'>');
210                                         $deliver_status = Diaspora::transmit($owner,$contact,$data,$public,true);
211
212                                         if ($deliver_status == (-1)) {
213                                                 update_queue_time($q_item['id']);
214                                                 Cache::set($cachekey_deadguy.$contact['notify'], true, CACHE_QUARTER_HOUR);
215                                         } else {
216                                                 remove_queue_item($q_item['id']);
217                                         }
218                                 }
219                                 break;
220
221                         default:
222                                 $params = array('owner' => $owner, 'contact' => $contact, 'queue' => $q_item, 'result' => false);
223                                 call_hooks('queue_deliver', $a, $params);
224
225                                 if ($params['result']) {
226                                         remove_queue_item($q_item['id']);
227                                 } else {
228                                         update_queue_time($q_item['id']);
229                                 }
230                                 break;
231
232                 }
233                 logger('Deliver status '.(int)$deliver_status.' for item '.$q_item['id'].' to '.$contact['name'].' <'.$contact['url'].'>');
234         }
235
236         return;
237
238 }
239
240 if (array_search(__file__,get_included_files())===0){
241   queue_run($_SERVER["argv"],$_SERVER["argc"]);
242   killme();
243 }