Queue.php is now reworked

This commit is contained in:
Michael 2017-02-27 21:07:32 +00:00
parent df6913f7e1
commit 3c220dd40c

View File

@ -1,7 +1,5 @@
<?php <?php
/// @todo Rework the whole file to a single item processing
use \Friendica\Core\Config; use \Friendica\Core\Config;
require_once('include/queue_fn.php'); require_once('include/queue_fn.php');
@ -14,15 +12,11 @@ require_once('include/socgraph.php');
function queue_run(&$argv, &$argc){ function queue_run(&$argv, &$argc){
global $a; global $a;
if($argc > 1) if ($argc > 1)
$queue_id = intval($argv[1]); $queue_id = intval($argv[1]);
else else
$queue_id = 0; $queue_id = 0;
$deadguys = array();
$deadservers = array();
$serverlist = array();
if (!$queue_id) { if (!$queue_id) {
logger('queue: start'); logger('queue: start');
@ -34,7 +28,7 @@ function queue_run(&$argv, &$argc){
if ($r) { if ($r) {
foreach ($r as $rr) { foreach ($r as $rr) {
logger('queue: deliverq'); logger('queue: deliverq');
proc_run(PRIORITY_HIGH,'include/delivery.php',$rr['cmd'],$rr['item'],$rr['contact']); proc_run(PRIORITY_HIGH,'include/delivery.php', $rr['cmd'], $rr['item'], $rr['contact']);
} }
} }
@ -53,125 +47,103 @@ function queue_run(&$argv, &$argc){
// After that, we'll only attempt delivery once per hour. // After that, we'll only attempt delivery once per hour.
$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`"); $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`");
} else {
logger('queue: start for id '.$queue_id);
$r = q("SELECT `id` FROM `queue` WHERE `id` = %d LIMIT 1", call_hooks('queue_predeliver', $a, $r);
intval($queue_id)
); if (dbm::is_result) {
foreach ($r as $q_item) {
logger('Call queue for id '.$q_item['id']);
proc_run(PRIORITY_LOW, "include/queue.php", $q_item['id']);
}
} }
if (!$r){
return; return;
} }
if (!$queue_id)
call_hooks('queue_predeliver', $a, $r);
// delivering
// delivery loop
require_once('include/salmon.php'); require_once('include/salmon.php');
require_once('include/diaspora.php'); require_once('include/diaspora.php');
foreach($r as $q_item) { $r = q("SELECT * FROM `queue` WHERE `id` = %d LIMIT 1",
// queue_predeliver hooks may have changed the queue db details,
// so check again if this entry still needs processing
if ($queue_id) {
$qi = q("SELECT * FROM `queue` WHERE `id` = %d LIMIT 1",
intval($queue_id)); intval($queue_id));
} else {
logger('Call queue for id '.$q_item['id']); if (!dbm::is_result($r)) {
proc_run(PRIORITY_LOW, "include/queue.php", $q_item['id']); return;
continue;
} }
if(! count($qi)) $q_item = $r[0];
continue;
$c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1", $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
intval($qi[0]['cid']) intval($q_item['cid'])
); );
if (! dbm::is_result($c)) {
if (!dbm::is_result($c)) {
remove_queue_item($q_item['id']); remove_queue_item($q_item['id']);
continue; return;
}
if(in_array($c[0]['notify'],$deadguys)) {
logger('queue: skipping known dead url: ' . $c[0]['notify']);
update_queue_time($q_item['id']);
continue;
} }
$server = poco_detect_server($c[0]['url']); $server = poco_detect_server($c[0]['url']);
if (($server != "") AND !in_array($server, $serverlist)) { if ($server != "") {
logger("Check server ".$server." (".$c[0]["network"].")"); logger("Check server ".$server." (".$c[0]["network"].")");
if (!poco_check_server($server, $c[0]["network"], true))
$deadservers[] = $server;
$serverlist[] = $server; if (!poco_check_server($server, $c[0]["network"], true)) {
} logger('queue: skipping dead server: '.$server);
if (($server != "") AND in_array($server, $deadservers)) {
logger('queue: skipping known dead server: '.$server);
update_queue_time($q_item['id']); update_queue_time($q_item['id']);
continue; return;
}
} }
$u = q("SELECT `user`.*, `user`.`pubkey` AS `upubkey`, `user`.`prvkey` AS `uprvkey` $u = q("SELECT `user`.*, `user`.`pubkey` AS `upubkey`, `user`.`prvkey` AS `uprvkey`
FROM `user` WHERE `uid` = %d LIMIT 1", FROM `user` WHERE `uid` = %d LIMIT 1",
intval($c[0]['uid']) intval($c[0]['uid'])
); );
if (! dbm::is_result($u)) { if (!dbm::is_result($u)) {
remove_queue_item($q_item['id']); remove_queue_item($q_item['id']);
continue; return;
} }
$data = $qi[0]['content']; $data = $q_item['content'];
$public = $qi[0]['batch']; $public = $q_item['batch'];
$contact = $c[0]; $contact = $c[0];
$owner = $u[0]; $owner = $u[0];
$deliver_status = 0; $deliver_status = 0;
switch($contact['network']) { switch ($contact['network']) {
case NETWORK_DFRN: case NETWORK_DFRN:
logger('queue: dfrndelivery: item '.$q_item['id'].' for '.$contact['name'].' <'.$contact['url'].'>'); logger('queue: dfrndelivery: item '.$q_item['id'].' for '.$contact['name'].' <'.$contact['url'].'>');
$deliver_status = dfrn::deliver($owner,$contact,$data); $deliver_status = dfrn::deliver($owner, $contact, $data);
if($deliver_status == (-1)) { if ($deliver_status == (-1)) {
update_queue_time($q_item['id']); update_queue_time($q_item['id']);
$deadguys[] = $contact['notify']; } else {
} else
remove_queue_item($q_item['id']);
break;
case NETWORK_OSTATUS:
if($contact['notify']) {
logger('queue: slapdelivery: item '.$q_item['id'].' for '.$contact['name'].' <'.$contact['url'].'>');
$deliver_status = slapper($owner,$contact['notify'],$data);
if($deliver_status == (-1)) {
update_queue_time($q_item['id']);
$deadguys[] = $contact['notify'];
} else
remove_queue_item($q_item['id']); remove_queue_item($q_item['id']);
} }
break; break;
case NETWORK_DIASPORA: case NETWORK_OSTATUS:
if($contact['notify']) { if ($contact['notify']) {
logger('queue: diaspora_delivery: item '.$q_item['id'].' for '.$contact['name'].' <'.$contact['url'].'>'); logger('queue: slapdelivery: item '.$q_item['id'].' for '.$contact['name'].' <'.$contact['url'].'>');
$deliver_status = Diaspora::transmit($owner,$contact,$data,$public,true); $deliver_status = slapper($owner, $contact['notify'], $data);
if($deliver_status == (-1)) { if ($deliver_status == (-1)) {
update_queue_time($q_item['id']); update_queue_time($q_item['id']);
$deadguys[] = $contact['notify']; } else {
} else
remove_queue_item($q_item['id']); remove_queue_item($q_item['id']);
}
}
break;
case NETWORK_DIASPORA:
if ($contact['notify']) {
logger('queue: diaspora_delivery: item '.$q_item['id'].' for '.$contact['name'].' <'.$contact['url'].'>');
$deliver_status = Diaspora::transmit($owner, $contact, $data, $public, true);
if ($deliver_status == (-1)) {
update_queue_time($q_item['id']);
} else {
remove_queue_item($q_item['id']);
}
} }
break; break;
@ -179,7 +151,7 @@ function queue_run(&$argv, &$argc){
$params = array('owner' => $owner, 'contact' => $contact, 'queue' => $q_item, 'result' => false); $params = array('owner' => $owner, 'contact' => $contact, 'queue' => $q_item, 'result' => false);
call_hooks('queue_deliver', $a, $params); call_hooks('queue_deliver', $a, $params);
if($params['result']) if ($params['result'])
remove_queue_item($q_item['id']); remove_queue_item($q_item['id']);
else else
update_queue_time($q_item['id']); update_queue_time($q_item['id']);
@ -188,7 +160,6 @@ function queue_run(&$argv, &$argc){
} }
logger('Deliver status '.$deliver_status.' for item '.$q_item['id'].' to '.$contact['name'].' <'.$contact['url'].'>'); logger('Deliver status '.$deliver_status.' for item '.$q_item['id'].' to '.$contact['name'].' <'.$contact['url'].'>');
}
return; return;
} }