Unsuccessful ActivitiyPub transmission are now deferred

This commit is contained in:
Michael 2018-10-23 03:54:18 +00:00
parent 65f29800ff
commit e3e714a45f
4 changed files with 34 additions and 14 deletions

View file

@ -243,7 +243,9 @@ class Worker
self::execFunction($queue, $include, $argv, true); self::execFunction($queue, $include, $argv, true);
$stamp = (float)microtime(true); $stamp = (float)microtime(true);
if (DBA::update('workerqueue', ['done' => true], ['id' => $queue['id']])) {
$condition = ["`id` = ? AND `next_try` < ?", $queue['id'], DateTimeFormat::utcNow()];
if (DBA::update('workerqueue', ['done' => true], $condition)) {
Config::set('system', 'last_worker_execution', DateTimeFormat::utcNow()); Config::set('system', 'last_worker_execution', DateTimeFormat::utcNow());
} }
self::$db_duration = (microtime(true) - $stamp); self::$db_duration = (microtime(true) - $stamp);
@ -1137,8 +1139,8 @@ class Worker
$id = $queue['id']; $id = $queue['id'];
if ($retrial > 14) { if ($retrial > 14) {
logger('Id ' . $id . ' had been tried 14 times, it will be deleted now.', LOGGER_DEBUG); logger('Id ' . $id . ' had been tried 14 times. We stop now.', LOGGER_DEBUG);
DBA::delete('workerqueue', ['id' => $id]); return;
} }
// Calculate the delay until the next trial // Calculate the delay until the next trial

View file

@ -936,6 +936,8 @@ class Transmitter
* @param integer $uid User ID * @param integer $uid User ID
* @param string $inbox Target inbox * @param string $inbox Target inbox
* @param integer $suggestion_id Suggestion ID * @param integer $suggestion_id Suggestion ID
*
* @return boolean was the transmission successful?
*/ */
public static function sendContactSuggestion($uid, $inbox, $suggestion_id) public static function sendContactSuggestion($uid, $inbox, $suggestion_id)
{ {
@ -957,7 +959,7 @@ class Transmitter
$signed = LDSignature::sign($data, $owner); $signed = LDSignature::sign($data, $owner);
logger('Deliver profile deletion for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', LOGGER_DEBUG); logger('Deliver profile deletion for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', LOGGER_DEBUG);
HTTPSignature::transmit($signed, $inbox, $uid); return HTTPSignature::transmit($signed, $inbox, $uid);
} }
/** /**
@ -965,6 +967,8 @@ class Transmitter
* *
* @param integer $uid User ID * @param integer $uid User ID
* @param string $inbox Target inbox * @param string $inbox Target inbox
*
* @return boolean was the transmission successful?
*/ */
public static function sendProfileDeletion($uid, $inbox) public static function sendProfileDeletion($uid, $inbox)
{ {
@ -984,7 +988,7 @@ class Transmitter
$signed = LDSignature::sign($data, $owner); $signed = LDSignature::sign($data, $owner);
logger('Deliver profile deletion for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', LOGGER_DEBUG); logger('Deliver profile deletion for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', LOGGER_DEBUG);
HTTPSignature::transmit($signed, $inbox, $uid); return HTTPSignature::transmit($signed, $inbox, $uid);
} }
/** /**
@ -992,6 +996,8 @@ class Transmitter
* *
* @param integer $uid User ID * @param integer $uid User ID
* @param string $inbox Target inbox * @param string $inbox Target inbox
*
* @return boolean was the transmission successful?
*/ */
public static function sendProfileUpdate($uid, $inbox) public static function sendProfileUpdate($uid, $inbox)
{ {
@ -1011,7 +1017,7 @@ class Transmitter
$signed = LDSignature::sign($data, $owner); $signed = LDSignature::sign($data, $owner);
logger('Deliver profile update for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', LOGGER_DEBUG); logger('Deliver profile update for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', LOGGER_DEBUG);
HTTPSignature::transmit($signed, $inbox, $uid); return HTTPSignature::transmit($signed, $inbox, $uid);
} }
/** /**

View file

@ -272,9 +272,11 @@ class HTTPSignature
/** /**
* @brief Transmit given data to a target for a user * @brief Transmit given data to a target for a user
* *
* @param $data * @param array $data Data that is about to be send
* @param $target * @param string $target The URL of the inbox
* @param $uid * @param integer $uid User id of the sender
*
* @return boolean Was the transmission successful?
*/ */
public static function transmit($data, $target, $uid) public static function transmit($data, $target, $uid)
{ {
@ -303,8 +305,11 @@ class HTTPSignature
$headers[] = 'Content-Type: application/activity+json'; $headers[] = 'Content-Type: application/activity+json';
$postResult = Network::post($target, $content, $headers); $postResult = Network::post($target, $content, $headers);
$return_code = $postResult->getReturnCode();
logger('Transmit to ' . $target . ' returned ' . $postResult->getReturnCode()); logger('Transmit to ' . $target . ' returned ' . $return_code);
return ($return_code >= 200) && ($return_code <= 299);
} }
/** /**

View file

@ -7,6 +7,7 @@ namespace Friendica\Worker;
use Friendica\BaseObject; use Friendica\BaseObject;
use Friendica\Protocol\ActivityPub; use Friendica\Protocol\ActivityPub;
use Friendica\Model\Item; use Friendica\Model\Item;
use Friendica\Core\Worker;
use Friendica\Util\HTTPSignature; use Friendica\Util\HTTPSignature;
class APDelivery extends BaseObject class APDelivery extends BaseObject
@ -23,19 +24,25 @@ class APDelivery extends BaseObject
{ {
logger('Invoked: ' . $cmd . ': ' . $item_id . ' to ' . $inbox, LOGGER_DEBUG); logger('Invoked: ' . $cmd . ': ' . $item_id . ' to ' . $inbox, LOGGER_DEBUG);
$success = true;
if ($cmd == Delivery::MAIL) { if ($cmd == Delivery::MAIL) {
} elseif ($cmd == Delivery::SUGGESTION) { } elseif ($cmd == Delivery::SUGGESTION) {
ActivityPub\Transmitter::sendContactSuggestion($uid, $inbox, $item_id); $success = ActivityPub\Transmitter::sendContactSuggestion($uid, $inbox, $item_id);
} elseif ($cmd == Delivery::RELOCATION) { } elseif ($cmd == Delivery::RELOCATION) {
} elseif ($cmd == Delivery::REMOVAL) { } elseif ($cmd == Delivery::REMOVAL) {
ActivityPub\Transmitter::sendProfileDeletion($uid, $inbox); $success = ActivityPub\Transmitter::sendProfileDeletion($uid, $inbox);
} elseif ($cmd == Delivery::PROFILEUPDATE) { } elseif ($cmd == Delivery::PROFILEUPDATE) {
ActivityPub\Transmitter::sendProfileUpdate($uid, $inbox); $success = ActivityPub\Transmitter::sendProfileUpdate($uid, $inbox);
} else { } else {
$data = ActivityPub\Transmitter::createCachedActivityFromItem($item_id); $data = ActivityPub\Transmitter::createCachedActivityFromItem($item_id);
if (!empty($data)) { if (!empty($data)) {
HTTPSignature::transmit($data, $inbox, $uid); $success = HTTPSignature::transmit($data, $inbox, $uid);
} }
} }
if (!$success) {
Worker::defer();
}
} }
} }