3 use \Friendica\Core\Config;
5 require_once("boot.php");
6 require_once('include/queue_fn.php');
7 require_once('include/dfrn.php');
9 function queue_run(&$argv, &$argc){
17 @include(".htconfig.php");
18 require_once("include/dba.php");
19 $db = new dba($db_host, $db_user, $db_pass, $db_data);
20 unset($db_host, $db_user, $db_pass, $db_data);
23 require_once("include/session.php");
24 require_once("include/datetime.php");
25 require_once('include/items.php');
26 require_once('include/bbcode.php');
27 require_once('include/socgraph.php');
31 // Don't check this stuff if the function is called by the poller
32 if (App::callstack() != "poller_run")
33 if (App::is_already_running('queue', 'include/queue.php', 540))
36 $a->set_baseurl(get_config('system','url'));
41 $queue_id = intval($argv[1]);
46 $deadservers = array();
47 $serverlist = array();
51 logger('queue: start');
53 // Handling the pubsubhubbub requests
54 proc_run(PRIORITY_HIGH,'include/pubsubpublish.php');
56 $interval = ((get_config('system','delivery_interval') === false) ? 2 : intval(get_config('system','delivery_interval')));
58 // If we are using the worker we don't need a delivery interval
59 if (get_config("system", "worker"))
62 $r = q("select * from deliverq where 1");
65 logger('queue: deliverq');
66 proc_run(PRIORITY_HIGH,'include/delivery.php',$rr['cmd'],$rr['item'],$rr['contact']);
68 time_sleep_until(microtime(true) + (float) $interval);
73 $r = q("SELECT `queue`.*, `contact`.`name`, `contact`.`uid` FROM `queue`
74 INNER JOIN `contact` ON `queue`.`cid` = `contact`.`id`
75 WHERE `queue`.`created` < UTC_TIMESTAMP() - INTERVAL 3 DAY");
78 logger('Removing expired queue item for ' . $rr['name'] . ', uid=' . $rr['uid']);
79 logger('Expired queue data :' . $rr['content'], LOGGER_DATA);
81 q("DELETE FROM `queue` WHERE `created` < UTC_TIMESTAMP() - INTERVAL 3 DAY");
84 // For the first 12 hours we'll try to deliver every 15 minutes
85 // After that, we'll only attempt delivery once per hour.
87 $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 logger('queue: start for id '.$queue_id);
91 $r = q("SELECT `id` FROM `queue` WHERE `id` = %d LIMIT 1",
101 call_hooks('queue_predeliver', $a, $r);
106 require_once('include/salmon.php');
107 require_once('include/diaspora.php');
109 foreach($r as $q_item) {
111 // queue_predeliver hooks may have changed the queue db details,
112 // so check again if this entry still needs processing
115 $qi = q("SELECT * FROM `queue` WHERE `id` = %d LIMIT 1",
117 elseif (get_config("system", "worker")) {
118 logger('Call queue for id '.$q_item['id']);
119 proc_run(PRIORITY_LOW, "include/queue.php", $q_item['id']);
122 $qi = q("SELECT * FROM `queue` WHERE `id` = %d AND `last` < UTC_TIMESTAMP() - INTERVAL 15 MINUTE ",
123 intval($q_item['id']));
129 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
130 intval($qi[0]['cid'])
132 if (! dbm::is_result($c)) {
133 remove_queue_item($q_item['id']);
136 if(in_array($c[0]['notify'],$deadguys)) {
137 logger('queue: skipping known dead url: ' . $c[0]['notify']);
138 update_queue_time($q_item['id']);
142 $server = poco_detect_server($c[0]['url']);
144 if (($server != "") AND !in_array($server, $serverlist)) {
145 logger("Check server ".$server." (".$c[0]["network"].")");
146 if (!poco_check_server($server, $c[0]["network"], true))
147 $deadservers[] = $server;
149 $serverlist[] = $server;
152 if (($server != "") AND in_array($server, $deadservers)) {
153 logger('queue: skipping known dead server: '.$server);
154 update_queue_time($q_item['id']);
158 $u = q("SELECT `user`.*, `user`.`pubkey` AS `upubkey`, `user`.`prvkey` AS `uprvkey`
159 FROM `user` WHERE `uid` = %d LIMIT 1",
162 if (! dbm::is_result($u)) {
163 remove_queue_item($q_item['id']);
167 $data = $qi[0]['content'];
168 $public = $qi[0]['batch'];
174 switch($contact['network']) {
176 logger('queue: dfrndelivery: item '.$q_item['id'].' for '.$contact['name'].' <'.$contact['url'].'>');
177 $deliver_status = dfrn::deliver($owner,$contact,$data);
179 if($deliver_status == (-1)) {
180 update_queue_time($q_item['id']);
181 $deadguys[] = $contact['notify'];
183 remove_queue_item($q_item['id']);
186 case NETWORK_OSTATUS:
187 if($contact['notify']) {
188 logger('queue: slapdelivery: item '.$q_item['id'].' for '.$contact['name'].' <'.$contact['url'].'>');
189 $deliver_status = slapper($owner,$contact['notify'],$data);
191 if($deliver_status == (-1)) {
192 update_queue_time($q_item['id']);
193 $deadguys[] = $contact['notify'];
195 remove_queue_item($q_item['id']);
198 case NETWORK_DIASPORA:
199 if($contact['notify']) {
200 logger('queue: diaspora_delivery: item '.$q_item['id'].' for '.$contact['name'].' <'.$contact['url'].'>');
201 $deliver_status = Diaspora::transmit($owner,$contact,$data,$public,true);
203 if($deliver_status == (-1)) {
204 update_queue_time($q_item['id']);
205 $deadguys[] = $contact['notify'];
207 remove_queue_item($q_item['id']);
213 $params = array('owner' => $owner, 'contact' => $contact, 'queue' => $q_item, 'result' => false);
214 call_hooks('queue_deliver', $a, $params);
216 if($params['result'])
217 remove_queue_item($q_item['id']);
219 update_queue_time($q_item['id']);
224 logger('Deliver status '.$deliver_status.' for item '.$q_item['id'].' to '.$contact['name'].' <'.$contact['url'].'>');
231 if (array_search(__file__,get_included_files())===0){
232 queue_run($_SERVER["argv"],$_SERVER["argc"]);