2 require_once("boot.php");
3 require_once('include/queue_fn.php');
4 require_once('include/dfrn.php');
6 function queue_run(&$argv, &$argc){
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);
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/socgraph.php');
27 load_config('config');
28 load_config('system');
30 // Don't check this stuff if the function is called by the poller
31 if (App::callstack() != "poller_run")
32 if (App::is_already_running('queue', 'include/queue.php', 540))
35 $a->set_baseurl(get_config('system','url'));
40 $queue_id = intval($argv[1]);
45 $deadservers = array();
46 $serverlist = array();
50 logger('queue: start');
52 // Handling the pubsubhubbub requests
53 proc_run(PRIORITY_HIGH,'include/pubsubpublish.php');
55 $interval = ((get_config('system','delivery_interval') === false) ? 2 : intval(get_config('system','delivery_interval')));
57 // If we are using the worker we don't need a delivery interval
58 if (get_config("system", "worker"))
61 $r = q("select * from deliverq where 1");
64 logger('queue: deliverq');
65 proc_run(PRIORITY_HIGH,'include/delivery.php',$rr['cmd'],$rr['item'],$rr['contact']);
67 @time_sleep_until(microtime(true) + (float) $interval);
71 $r = q("SELECT `queue`.*, `contact`.`name`, `contact`.`uid` FROM `queue`
72 INNER JOIN `contact` ON `queue`.`cid` = `contact`.`id`
73 WHERE `queue`.`created` < UTC_TIMESTAMP() - INTERVAL 3 DAY");
76 logger('Removing expired queue item for ' . $rr['name'] . ', uid=' . $rr['uid']);
77 logger('Expired queue data :' . $rr['content'], LOGGER_DATA);
79 q("DELETE FROM `queue` WHERE `created` < UTC_TIMESTAMP() - INTERVAL 3 DAY");
82 // For the first 12 hours we'll try to deliver every 15 minutes
83 // After that, we'll only attempt delivery once per hour.
85 $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`");
87 logger('queue: start for id '.$queue_id);
89 $r = q("SELECT `id` FROM `queue` WHERE `id` = %d LIMIT 1",
99 call_hooks('queue_predeliver', $a, $r);
104 require_once('include/salmon.php');
105 require_once('include/diaspora.php');
107 foreach($r as $q_item) {
109 // queue_predeliver hooks may have changed the queue db details,
110 // so check again if this entry still needs processing
113 $qi = q("SELECT * FROM `queue` WHERE `id` = %d LIMIT 1",
115 elseif (get_config("system", "worker")) {
116 logger('Call queue for id '.$q_item['id']);
117 proc_run(PRIORITY_LOW, "include/queue.php", $q_item['id']);
120 $qi = q("SELECT * FROM `queue` WHERE `id` = %d AND `last` < UTC_TIMESTAMP() - INTERVAL 15 MINUTE ",
121 intval($q_item['id']));
127 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
128 intval($qi[0]['cid'])
131 remove_queue_item($q_item['id']);
134 if(in_array($c[0]['notify'],$deadguys)) {
135 logger('queue: skipping known dead url: ' . $c[0]['notify']);
136 update_queue_time($q_item['id']);
140 $server = poco_detect_server($c[0]['url']);
142 if (($server != "") AND !in_array($server, $serverlist)) {
143 logger("Check server ".$server." (".$c[0]["network"].")");
144 if (!poco_check_server($server, $c[0]["network"], true))
145 $deadservers[] = $server;
147 $serverlist[] = $server;
150 if (($server != "") AND in_array($server, $deadservers)) {
151 logger('queue: skipping known dead server: '.$server);
152 update_queue_time($q_item['id']);
156 $u = q("SELECT `user`.*, `user`.`pubkey` AS `upubkey`, `user`.`prvkey` AS `uprvkey`
157 FROM `user` WHERE `uid` = %d LIMIT 1",
161 remove_queue_item($q_item['id']);
165 $data = $qi[0]['content'];
166 $public = $qi[0]['batch'];
172 switch($contact['network']) {
174 logger('queue: dfrndelivery: item '.$q_item['id'].' for '.$contact['name'].' <'.$contact['url'].'>');
175 $deliver_status = dfrn::deliver($owner,$contact,$data);
177 if($deliver_status == (-1)) {
178 update_queue_time($q_item['id']);
179 $deadguys[] = $contact['notify'];
181 remove_queue_item($q_item['id']);
184 case NETWORK_OSTATUS:
185 if($contact['notify']) {
186 logger('queue: slapdelivery: item '.$q_item['id'].' for '.$contact['name'].' <'.$contact['url'].'>');
187 $deliver_status = slapper($owner,$contact['notify'],$data);
189 if($deliver_status == (-1)) {
190 update_queue_time($q_item['id']);
191 $deadguys[] = $contact['notify'];
193 remove_queue_item($q_item['id']);
196 case NETWORK_DIASPORA:
197 if($contact['notify']) {
198 logger('queue: diaspora_delivery: item '.$q_item['id'].' for '.$contact['name'].' <'.$contact['url'].'>');
199 $deliver_status = diaspora::transmit($owner,$contact,$data,$public,true);
201 if($deliver_status == (-1)) {
202 update_queue_time($q_item['id']);
203 $deadguys[] = $contact['notify'];
205 remove_queue_item($q_item['id']);
211 $params = array('owner' => $owner, 'contact' => $contact, 'queue' => $q_item, 'result' => false);
212 call_hooks('queue_deliver', $a, $params);
214 if($params['result'])
215 remove_queue_item($q_item['id']);
217 update_queue_time($q_item['id']);
222 logger('Deliver status '.$deliver_status.' for item '.$q_item['id'].' to '.$contact['name'].' <'.$contact['url'].'>');
229 if (array_search(__file__,get_included_files())===0){
230 queue_run($_SERVER["argv"],$_SERVER["argc"]);