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