3 /// @todo Rework the whole file to a single item processing
5 use \Friendica\Core\Config;
7 require_once('include/queue_fn.php');
8 require_once('include/dfrn.php');
9 require_once("include/datetime.php");
10 require_once('include/items.php');
11 require_once('include/bbcode.php');
12 require_once('include/socgraph.php');
14 function queue_run(&$argv, &$argc){
18 $queue_id = intval($argv[1]);
23 $deadservers = array();
24 $serverlist = array();
28 logger('queue: start');
30 // Handling the pubsubhubbub requests
31 proc_run(PRIORITY_HIGH,'include/pubsubpublish.php');
33 $r = q("select * from deliverq where 1");
36 logger('queue: deliverq');
37 proc_run(PRIORITY_HIGH,'include/delivery.php',$rr['cmd'],$rr['item'],$rr['contact']);
41 $r = q("SELECT `queue`.*, `contact`.`name`, `contact`.`uid` FROM `queue`
42 INNER JOIN `contact` ON `queue`.`cid` = `contact`.`id`
43 WHERE `queue`.`created` < UTC_TIMESTAMP() - INTERVAL 3 DAY");
46 logger('Removing expired queue item for ' . $rr['name'] . ', uid=' . $rr['uid']);
47 logger('Expired queue data :' . $rr['content'], LOGGER_DATA);
49 q("DELETE FROM `queue` WHERE `created` < UTC_TIMESTAMP() - INTERVAL 3 DAY");
52 // For the first 12 hours we'll try to deliver every 15 minutes
53 // After that, we'll only attempt delivery once per hour.
55 $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`");
57 logger('queue: start for id '.$queue_id);
59 $r = q("SELECT `id` FROM `queue` WHERE `id` = %d LIMIT 1",
69 call_hooks('queue_predeliver', $a, $r);
74 require_once('include/salmon.php');
75 require_once('include/diaspora.php');
77 foreach($r as $q_item) {
79 // queue_predeliver hooks may have changed the queue db details,
80 // so check again if this entry still needs processing
83 $qi = q("SELECT * FROM `queue` WHERE `id` = %d LIMIT 1",
86 logger('Call queue for id '.$q_item['id']);
87 proc_run(PRIORITY_LOW, "include/queue.php", $q_item['id']);
95 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
98 if (! dbm::is_result($c)) {
99 remove_queue_item($q_item['id']);
102 if(in_array($c[0]['notify'],$deadguys)) {
103 logger('queue: skipping known dead url: ' . $c[0]['notify']);
104 update_queue_time($q_item['id']);
108 $server = poco_detect_server($c[0]['url']);
110 if (($server != "") AND !in_array($server, $serverlist)) {
111 logger("Check server ".$server." (".$c[0]["network"].")");
112 if (!poco_check_server($server, $c[0]["network"], true))
113 $deadservers[] = $server;
115 $serverlist[] = $server;
118 if (($server != "") AND in_array($server, $deadservers)) {
119 logger('queue: skipping known dead server: '.$server);
120 update_queue_time($q_item['id']);
124 $u = q("SELECT `user`.*, `user`.`pubkey` AS `upubkey`, `user`.`prvkey` AS `uprvkey`
125 FROM `user` WHERE `uid` = %d LIMIT 1",
128 if (! dbm::is_result($u)) {
129 remove_queue_item($q_item['id']);
133 $data = $qi[0]['content'];
134 $public = $qi[0]['batch'];
140 switch($contact['network']) {
142 logger('queue: dfrndelivery: item '.$q_item['id'].' for '.$contact['name'].' <'.$contact['url'].'>');
143 $deliver_status = dfrn::deliver($owner,$contact,$data);
145 if($deliver_status == (-1)) {
146 update_queue_time($q_item['id']);
147 $deadguys[] = $contact['notify'];
149 remove_queue_item($q_item['id']);
152 case NETWORK_OSTATUS:
153 if($contact['notify']) {
154 logger('queue: slapdelivery: item '.$q_item['id'].' for '.$contact['name'].' <'.$contact['url'].'>');
155 $deliver_status = slapper($owner,$contact['notify'],$data);
157 if($deliver_status == (-1)) {
158 update_queue_time($q_item['id']);
159 $deadguys[] = $contact['notify'];
161 remove_queue_item($q_item['id']);
164 case NETWORK_DIASPORA:
165 if($contact['notify']) {
166 logger('queue: diaspora_delivery: item '.$q_item['id'].' for '.$contact['name'].' <'.$contact['url'].'>');
167 $deliver_status = Diaspora::transmit($owner,$contact,$data,$public,true);
169 if($deliver_status == (-1)) {
170 update_queue_time($q_item['id']);
171 $deadguys[] = $contact['notify'];
173 remove_queue_item($q_item['id']);
179 $params = array('owner' => $owner, 'contact' => $contact, 'queue' => $q_item, 'result' => false);
180 call_hooks('queue_deliver', $a, $params);
182 if($params['result'])
183 remove_queue_item($q_item['id']);
185 update_queue_time($q_item['id']);
190 logger('Deliver status '.$deliver_status.' for item '.$q_item['id'].' to '.$contact['name'].' <'.$contact['url'].'>');