diff --git a/src/Core/Worker.php b/src/Core/Worker.php index a06a11043..76400d469 100644 --- a/src/Core/Worker.php +++ b/src/Core/Worker.php @@ -243,7 +243,9 @@ class Worker self::execFunction($queue, $include, $argv, 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()); } self::$db_duration = (microtime(true) - $stamp); @@ -1137,8 +1139,8 @@ class Worker $id = $queue['id']; if ($retrial > 14) { - logger('Id ' . $id . ' had been tried 14 times, it will be deleted now.', LOGGER_DEBUG); - DBA::delete('workerqueue', ['id' => $id]); + logger('Id ' . $id . ' had been tried 14 times. We stop now.', LOGGER_DEBUG); + return; } // Calculate the delay until the next trial diff --git a/src/Protocol/ActivityPub/Transmitter.php b/src/Protocol/ActivityPub/Transmitter.php index a0d92a571..a37d03537 100644 --- a/src/Protocol/ActivityPub/Transmitter.php +++ b/src/Protocol/ActivityPub/Transmitter.php @@ -936,6 +936,8 @@ class Transmitter * @param integer $uid User ID * @param string $inbox Target inbox * @param integer $suggestion_id Suggestion ID + * + * @return boolean was the transmission successful? */ public static function sendContactSuggestion($uid, $inbox, $suggestion_id) { @@ -957,7 +959,7 @@ class Transmitter $signed = LDSignature::sign($data, $owner); 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 string $inbox Target inbox + * + * @return boolean was the transmission successful? */ public static function sendProfileDeletion($uid, $inbox) { @@ -984,7 +988,7 @@ class Transmitter $signed = LDSignature::sign($data, $owner); 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 string $inbox Target inbox + * + * @return boolean was the transmission successful? */ public static function sendProfileUpdate($uid, $inbox) { @@ -1011,7 +1017,7 @@ class Transmitter $signed = LDSignature::sign($data, $owner); logger('Deliver profile update for user ' . $uid . ' to ' . $inbox . ' via ActivityPub', LOGGER_DEBUG); - HTTPSignature::transmit($signed, $inbox, $uid); + return HTTPSignature::transmit($signed, $inbox, $uid); } /** diff --git a/src/Util/HTTPSignature.php b/src/Util/HTTPSignature.php index 503cbff0a..1d2e7d9f7 100644 --- a/src/Util/HTTPSignature.php +++ b/src/Util/HTTPSignature.php @@ -272,9 +272,11 @@ class HTTPSignature /** * @brief Transmit given data to a target for a user * - * @param $data - * @param $target - * @param $uid + * @param array $data Data that is about to be send + * @param string $target The URL of the inbox + * @param integer $uid User id of the sender + * + * @return boolean Was the transmission successful? */ public static function transmit($data, $target, $uid) { @@ -303,8 +305,11 @@ class HTTPSignature $headers[] = 'Content-Type: application/activity+json'; $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); } /** diff --git a/src/Worker/APDelivery.php b/src/Worker/APDelivery.php index 7b1ad7605..b952249fe 100644 --- a/src/Worker/APDelivery.php +++ b/src/Worker/APDelivery.php @@ -7,6 +7,7 @@ namespace Friendica\Worker; use Friendica\BaseObject; use Friendica\Protocol\ActivityPub; use Friendica\Model\Item; +use Friendica\Core\Worker; use Friendica\Util\HTTPSignature; class APDelivery extends BaseObject @@ -23,19 +24,25 @@ class APDelivery extends BaseObject { logger('Invoked: ' . $cmd . ': ' . $item_id . ' to ' . $inbox, LOGGER_DEBUG); + $success = true; + if ($cmd == Delivery::MAIL) { } 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::REMOVAL) { - ActivityPub\Transmitter::sendProfileDeletion($uid, $inbox); + $success = ActivityPub\Transmitter::sendProfileDeletion($uid, $inbox); } elseif ($cmd == Delivery::PROFILEUPDATE) { - ActivityPub\Transmitter::sendProfileUpdate($uid, $inbox); + $success = ActivityPub\Transmitter::sendProfileUpdate($uid, $inbox); } else { $data = ActivityPub\Transmitter::createCachedActivityFromItem($item_id); if (!empty($data)) { - HTTPSignature::transmit($data, $inbox, $uid); + $success = HTTPSignature::transmit($data, $inbox, $uid); } } + + if (!$success) { + Worker::defer(); + } } }