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/pidfile.php');
26 require_once('include/socgraph.php');
28 load_config('config');
29 load_config('system');
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) {
38 logger("queue: killed stale process");
39 // Calling a new instance
40 proc_run('php',"include/queue.php");
46 $a->set_baseurl(get_config('system','url'));
51 $queue_id = intval($argv[1]);
56 $deadservers = array();
57 $serverlist = array();
59 logger('queue: start');
61 // Handling the pubsubhubbub requests
62 proc_run('php','include/pubsubpublish.php');
64 $interval = ((get_config('system','delivery_interval') === false) ? 2 : intval(get_config('system','delivery_interval')));
66 // If we are using the worker we don't need a delivery interval
67 if (get_config("system", "worker"))
70 $r = q("select * from deliverq where 1");
73 logger('queue: deliverq');
74 proc_run('php','include/delivery.php',$rr['cmd'],$rr['item'],$rr['contact']);
76 @time_sleep_until(microtime(true) + (float) $interval);
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");
85 logger('Removing expired queue item for ' . $rr['name'] . ', uid=' . $rr['uid']);
86 logger('Expired queue data :' . $rr['content'], LOGGER_DATA);
88 q("DELETE FROM `queue` WHERE `created` < UTC_TIMESTAMP() - INTERVAL 3 DAY");
92 $r = q("SELECT `id` FROM `queue` WHERE `id` = %d LIMIT 1",
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.
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`");
108 call_hooks('queue_predeliver', $a, $r);
113 require_once('include/salmon.php');
114 require_once('include/diaspora.php');
116 foreach($r as $q_item) {
118 // queue_predeliver hooks may have changed the queue db details,
119 // so check again if this entry still needs processing
122 $qi = q("SELECT * FROM `queue` WHERE `id` = %d LIMIT 1",
127 $qi = q("SELECT * FROM `queue` WHERE `id` = %d AND `last` < UTC_TIMESTAMP() - INTERVAL 15 MINUTE ",
128 intval($q_item['id'])
135 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
136 intval($qi[0]['cid'])
139 remove_queue_item($q_item['id']);
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']);
148 $server = poco_detect_server($c[0]['url']);
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;
155 $serverlist[] = $server;
158 if (($server != "") AND in_array($server, $deadservers)) {
159 logger('queue: skipping known dead server: '.$server);
160 update_queue_time($q_item['id']);
164 $u = q("SELECT `user`.*, `user`.`pubkey` AS `upubkey`, `user`.`prvkey` AS `uprvkey`
165 FROM `user` WHERE `uid` = %d LIMIT 1",
169 remove_queue_item($q_item['id']);
173 $data = $qi[0]['content'];
174 $public = $qi[0]['batch'];
180 switch($contact['network']) {
182 logger('queue: dfrndelivery: item '.$q_item['id'].' for '.$contact['name'].' <'.$contact['url'].'>');
183 $deliver_status = dfrn::deliver($owner,$contact,$data);
185 if($deliver_status == (-1)) {
186 update_queue_time($q_item['id']);
187 $deadguys[] = $contact['notify'];
189 remove_queue_item($q_item['id']);
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);
197 if($deliver_status == (-1)) {
198 update_queue_time($q_item['id']);
199 $deadguys[] = $contact['notify'];
201 remove_queue_item($q_item['id']);
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);
209 if($deliver_status == (-1)) {
210 update_queue_time($q_item['id']);
211 $deadguys[] = $contact['notify'];
213 remove_queue_item($q_item['id']);
219 $params = array('owner' => $owner, 'contact' => $contact, 'queue' => $q_item, 'result' => false);
220 call_hooks('queue_deliver', $a, $params);
222 if($params['result'])
223 remove_queue_item($q_item['id']);
225 update_queue_time($q_item['id']);
230 logger('Deliver status '.$deliver_status.' for item '.$q_item['id'].' to '.$contact['name'].' <'.$contact['url'].'>');
237 if (array_search(__file__,get_included_files())===0){
238 queue_run($_SERVER["argv"],$_SERVER["argc"]);