3 use \Friendica\Core\Config;
5 require_once("boot.php");
6 require_once('include/queue_fn.php');
7 require_once('include/dfrn.php');
8 require_once('include/cache.php');
10 function queue_run(&$argv, &$argc){
18 @include(".htconfig.php");
19 require_once("include/dba.php");
20 $db = new dba($db_host, $db_user, $db_pass, $db_data);
21 unset($db_host, $db_user, $db_pass, $db_data);
24 require_once("include/session.php");
25 require_once("include/datetime.php");
26 require_once('include/items.php');
27 require_once('include/bbcode.php');
28 require_once('include/socgraph.php');
32 // Don't check this stuff if the function is called by the poller
33 if (App::callstack() != "poller_run")
34 if (App::is_already_running('queue', 'include/queue.php', 540))
37 $a->set_baseurl(get_config('system','url'));
42 $queue_id = intval($argv[1]);
47 $cachekey_deadguy = 'queue_run:deadguy:';
48 $cachekey_server = 'queue_run:server:';
52 logger('queue: start');
54 // Handling the pubsubhubbub requests
55 proc_run(PRIORITY_HIGH,'include/pubsubpublish.php');
57 $interval = ((get_config('system','delivery_interval') === false) ? 2 : intval(get_config('system','delivery_interval')));
59 // If we are using the worker we don't need a delivery interval
60 if (get_config("system", "worker"))
63 $r = q("select * from deliverq where 1");
66 logger('queue: deliverq');
67 proc_run(PRIORITY_HIGH,'include/delivery.php',$rr['cmd'],$rr['item'],$rr['contact']);
69 time_sleep_until(microtime(true) + (float) $interval);
74 $r = q("SELECT `queue`.*, `contact`.`name`, `contact`.`uid` FROM `queue`
75 INNER JOIN `contact` ON `queue`.`cid` = `contact`.`id`
76 WHERE `queue`.`created` < UTC_TIMESTAMP() - INTERVAL 3 DAY");
79 logger('Removing expired queue item for ' . $rr['name'] . ', uid=' . $rr['uid']);
80 logger('Expired queue data :' . $rr['content'], LOGGER_DATA);
82 q("DELETE FROM `queue` WHERE `created` < UTC_TIMESTAMP() - INTERVAL 3 DAY");
85 // For the first 12 hours we'll try to deliver every 15 minutes
86 // After that, we'll only attempt delivery once per hour.
88 $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`");
90 logger('queue: start for id '.$queue_id);
92 $r = q("SELECT `id` FROM `queue` WHERE `id` = %d LIMIT 1",
102 call_hooks('queue_predeliver', $a, $r);
107 require_once('include/salmon.php');
108 require_once('include/diaspora.php');
110 foreach($r as $q_item) {
112 // queue_predeliver hooks may have changed the queue db details,
113 // so check again if this entry still needs processing
116 $qi = q("SELECT * FROM `queue` WHERE `id` = %d LIMIT 1",
118 } elseif (get_config("system", "worker")) {
119 logger('Call queue for id '.$q_item['id']);
120 proc_run(PRIORITY_LOW, "include/queue.php", $q_item['id']);
123 $qi = q("SELECT * FROM `queue` WHERE `id` = %d AND `last` < UTC_TIMESTAMP() - INTERVAL 15 MINUTE ",
124 intval($q_item['id']));
126 if (!dbm::is_result($qi)) {
131 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
132 intval($qi[0]['cid'])
134 if (! dbm::is_result($c)) {
135 remove_queue_item($q_item['id']);
139 $dead = Cache::get($cachekey_deadguy.$c[0]['notify']);
141 if (!is_null($dead) AND $dead) {
142 logger('queue: skipping known dead url: '.$c[0]['notify']);
143 update_queue_time($q_item['id']);
147 $server = poco_detect_server($c[0]['url']);
150 $vital = Cache::get($cachekey_server.$server);
152 if (is_null($vital)) {
153 logger("Check server ".$server." (".$c[0]["network"].")");
155 $vital = poco_check_server($server, $c[0]["network"], true);
156 Cache::set($cachekey_server.$server, $vital, CACHE_QUARTER_HOUR);
159 if (!is_null($vital) AND !$vital) {
160 logger('queue: skipping dead server: '.$server);
161 update_queue_time($q_item['id']);
166 $u = q("SELECT `user`.*, `user`.`pubkey` AS `upubkey`, `user`.`prvkey` AS `uprvkey`
167 FROM `user` WHERE `uid` = %d LIMIT 1",
170 if (! dbm::is_result($u)) {
171 remove_queue_item($q_item['id']);
175 $data = $qi[0]['content'];
176 $public = $qi[0]['batch'];
182 switch($contact['network']) {
184 logger('queue: dfrndelivery: item '.$q_item['id'].' for '.$contact['name'].' <'.$contact['url'].'>');
185 $deliver_status = dfrn::deliver($owner,$contact,$data);
187 if ($deliver_status < 0) {
188 update_queue_time($q_item['id']);
189 Cache::set($cachekey_deadguy.$contact['notify'], true, CACHE_QUARTER_HOUR);
191 remove_queue_item($q_item['id']);
194 case NETWORK_OSTATUS:
195 if ($contact['notify']) {
196 logger('queue: slapdelivery: item '.$q_item['id'].' for '.$contact['name'].' <'.$contact['url'].'>');
197 $deliver_status = slapper($owner,$contact['notify'],$data);
199 if ($deliver_status == (-1)) {
200 update_queue_time($q_item['id']);
201 Cache::set($cachekey_deadguy.$contact['notify'], true, CACHE_QUARTER_HOUR);
203 remove_queue_item($q_item['id']);
207 case NETWORK_DIASPORA:
208 if ($contact['notify']) {
209 logger('queue: diaspora_delivery: item '.$q_item['id'].' for '.$contact['name'].' <'.$contact['url'].'>');
210 $deliver_status = Diaspora::transmit($owner,$contact,$data,$public,true);
212 if ($deliver_status == (-1)) {
213 update_queue_time($q_item['id']);
214 Cache::set($cachekey_deadguy.$contact['notify'], true, CACHE_QUARTER_HOUR);
216 remove_queue_item($q_item['id']);
222 $params = array('owner' => $owner, 'contact' => $contact, 'queue' => $q_item, 'result' => false);
223 call_hooks('queue_deliver', $a, $params);
225 if ($params['result']) {
226 remove_queue_item($q_item['id']);
228 update_queue_time($q_item['id']);
233 logger('Deliver status '.(int)$deliver_status.' for item '.$q_item['id'].' to '.$contact['name'].' <'.$contact['url'].'>');
240 if (array_search(__file__,get_included_files())===0){
241 queue_run($_SERVER["argv"],$_SERVER["argc"]);