More Exceptions for FeedSub doSubscribe and related functions

Now also garbageCollect will now throw exceptions of failures of all kinds
and only reply true/false on entirely successful runs of sub/unsub.
This commit is contained in:
Mikael Nordfeldth 2014-05-06 15:40:57 +02:00
parent 0fa00d5106
commit c279a33feb
7 changed files with 72 additions and 79 deletions

View File

@ -611,10 +611,7 @@ class OStatusPlugin extends Plugin
return true; return true;
} }
if (!$oprofile->subscribe()) { $oprofile->subscribe();
// TRANS: Exception thrown when setup of remote subscription fails.
throw new Exception(_m('Could not set up remote subscription.'));
}
} }
/** /**
@ -706,6 +703,7 @@ class OStatusPlugin extends Plugin
* @param Profile $profile * @param Profile $profile
* *
* @return mixed hook return value * @return mixed hook return value
* @throws Exception of various kinds, some from $oprofile->subscribe();
*/ */
function onStartJoinGroup($group, $profile) function onStartJoinGroup($group, $profile)
{ {
@ -714,10 +712,7 @@ class OStatusPlugin extends Plugin
return true; return true;
} }
if (!$oprofile->subscribe()) { $oprofile->subscribe();
// TRANS: Exception thrown when setup of remote group membership fails.
throw new Exception(_m('Could not set up remote group membership.'));
}
// NOTE: we don't use Group_member::asActivity() since that record // NOTE: we don't use Group_member::asActivity() since that record
// has not yet been created. // has not yet been created.
@ -807,6 +802,7 @@ class OStatusPlugin extends Plugin
* @param User $user * @param User $user
* *
* @return mixed hook return value * @return mixed hook return value
* @throws Exception of various kinds, some from $oprofile->subscribe();
*/ */
function onStartSubscribePeopletag($peopletag, $user) function onStartSubscribePeopletag($peopletag, $user)
@ -816,10 +812,7 @@ class OStatusPlugin extends Plugin
return true; return true;
} }
if (!$oprofile->subscribe()) { $oprofile->subscribe();
// TRANS: Exception thrown when setup of remote list subscription fails.
throw new Exception(_m('Could not set up remote list subscription.'));
}
$sub = $user->getProfile(); $sub = $user->getProfile();
$tagger = Profile::getKV($peopletag->tagger); $tagger = Profile::getKV($peopletag->tagger);
@ -942,6 +935,7 @@ class OStatusPlugin extends Plugin
* *
* @param Profile_tag $ptag the people tag that was created * @param Profile_tag $ptag the people tag that was created
* @return hook return value * @return hook return value
* @throws Exception of various kinds, some from $oprofile->subscribe();
*/ */
function onEndTagProfile($ptag) function onEndTagProfile($ptag)
{ {
@ -981,12 +975,7 @@ class OStatusPlugin extends Plugin
$oprofile->notifyDeferred($act, $tagger); $oprofile->notifyDeferred($act, $tagger);
// initiate a PuSH subscription for the person being tagged // initiate a PuSH subscription for the person being tagged
if (!$oprofile->subscribe()) { $oprofile->subscribe();
// TRANS: Exception thrown when subscribing to a remote list fails.
throw new Exception(sprintf(_m('Could not complete subscription to remote '.
'profile\'s feed. List %s could not be saved.'), $ptag->tag));
return false;
}
return true; return true;
} }

View File

@ -192,7 +192,7 @@ class FeedSub extends Managed_DataObject
* Send a subscription request to the hub for this feed. * Send a subscription request to the hub for this feed.
* The hub will later send us a confirmation POST to /main/push/callback. * The hub will later send us a confirmation POST to /main/push/callback.
* *
* @return bool true on success, false on failure * @return void
* @throws ServerException if feed state is not valid * @throws ServerException if feed state is not valid
*/ */
public function subscribe() public function subscribe()
@ -203,7 +203,7 @@ class FeedSub extends Managed_DataObject
if (!Event::handle('FeedSubscribe', array($this))) { if (!Event::handle('FeedSubscribe', array($this))) {
// A plugin handled it // A plugin handled it
return true; return;
} }
if (empty($this->huburi)) { if (empty($this->huburi)) {
@ -213,14 +213,14 @@ class FeedSub extends Managed_DataObject
} else if (common_config('feedsub', 'nohub')) { } else if (common_config('feedsub', 'nohub')) {
// Fake it! We're just testing remote feeds w/o hubs. // Fake it! We're just testing remote feeds w/o hubs.
// We'll never actually get updates in this mode. // We'll never actually get updates in this mode.
return true; return;
} else { } else {
// TRANS: Server exception. // TRANS: Server exception.
throw new ServerException(_m('Attempting to start PuSH subscription for feed with no hub.')); throw new ServerException(_m('Attempting to start PuSH subscription for feed with no hub.'));
} }
} }
return $this->doSubscribe('subscribe'); $this->doSubscribe('subscribe');
} }
/** /**
@ -230,7 +230,6 @@ class FeedSub extends Managed_DataObject
* the system is using it. Most callers will want garbageCollect() instead, * the system is using it. Most callers will want garbageCollect() instead,
* which confirms there's no uses left. * which confirms there's no uses left.
* *
* @return bool true on success, false on failure
* @throws ServerException if feed state is not valid * @throws ServerException if feed state is not valid
*/ */
public function unsubscribe() { public function unsubscribe() {
@ -240,7 +239,7 @@ class FeedSub extends Managed_DataObject
if (!Event::handle('FeedUnsubscribe', array($this))) { if (!Event::handle('FeedUnsubscribe', array($this))) {
// A plugin handled it // A plugin handled it
return true; return;
} }
if (empty($this->huburi)) { if (empty($this->huburi)) {
@ -250,14 +249,14 @@ class FeedSub extends Managed_DataObject
} else if (common_config('feedsub', 'nohub')) { } else if (common_config('feedsub', 'nohub')) {
// Fake it! We're just testing remote feeds w/o hubs. // Fake it! We're just testing remote feeds w/o hubs.
// We'll never actually get updates in this mode. // We'll never actually get updates in this mode.
return true; return;
} else { } else {
// TRANS: Server exception. // TRANS: Server exception.
throw new ServerException(_m('Attempting to end PuSH subscription for feed with no hub.')); throw new ServerException(_m('Attempting to end PuSH subscription for feed with no hub.'));
} }
} }
return $this->doSubscribe('unsubscribe'); $this->doSubscribe('unsubscribe');
} }
/** /**
@ -265,6 +264,7 @@ class FeedSub extends Managed_DataObject
* make sure it's inactive, unsubscribing if necessary. * make sure it's inactive, unsubscribing if necessary.
* *
* @return boolean true if the subscription is now inactive, false if still active. * @return boolean true if the subscription is now inactive, false if still active.
* @throws Exception if something goes wrong in unsubscribe() method
*/ */
public function garbageCollect() public function garbageCollect()
{ {
@ -277,12 +277,14 @@ class FeedSub extends Managed_DataObject
$count = 0; $count = 0;
Event::handle('FeedSubSubscriberCount', array($this, &$count)); Event::handle('FeedSubSubscriberCount', array($this, &$count));
if ($count) { if ($count > 0) {
common_log(LOG_INFO, __METHOD__ . ': ok, ' . $count . ' user(s) left for ' . $this->getUri()); common_log(LOG_INFO, __METHOD__ . ': ok, ' . $count . ' user(s) left for ' . $this->getUri());
return false; return false;
} else { } else {
common_log(LOG_INFO, __METHOD__ . ': unsubscribing, no users left for ' . $this->getUri()); common_log(LOG_INFO, __METHOD__ . ': unsubscribing, no users left for ' . $this->getUri());
return $this->unsubscribe(); // Unsubscribe throws various Exceptions on failure
$this->unsubscribe();
return true;
} }
} }
} }
@ -310,7 +312,8 @@ class FeedSub extends Managed_DataObject
* the lookup _while_ we're POSTing data, which means the transaction * the lookup _while_ we're POSTing data, which means the transaction
* never completes (PushcallbackAction gets an 'inactive' state). * never completes (PushcallbackAction gets an 'inactive' state).
* *
* @return boolean true on successful sub/unsub, false on failure * @return boolean true when everything is ok (throws Exception on fail)
* @throws Exception on failure, can be HTTPClient's or our own.
*/ */
protected function doSubscribe($mode) protected function doSubscribe($mode)
{ {
@ -344,29 +347,32 @@ class FeedSub extends Managed_DataObject
$client->setAuth($u, $p); $client->setAuth($u, $p);
} }
} else { } else {
throw new FeedSubException('WTF?'); throw new FeedSubException('Server could not find a usable PuSH hub.');
} }
} }
$response = $client->post($hub, $headers, $post); $response = $client->post($hub, $headers, $post);
$status = $response->getStatus(); $status = $response->getStatus();
// PuSH specificed response status code
if ($status == 202) { if ($status == 202) {
common_log(LOG_INFO, __METHOD__ . ': sub req ok, awaiting verification callback'); common_log(LOG_INFO, __METHOD__ . ': sub req ok, awaiting verification callback');
return true; return;
} else if ($status >= 200 && $status < 300) { } else if ($status >= 200 && $status < 300) {
common_log(LOG_ERR, __METHOD__ . ": sub req returned unexpected HTTP $status: " . $response->getBody()); common_log(LOG_ERR, __METHOD__ . ": sub req returned unexpected HTTP $status: " . $response->getBody());
} else { } else {
common_log(LOG_ERR, __METHOD__ . ": sub req failed with HTTP $status: " . $response->getBody()); common_log(LOG_ERR, __METHOD__ . ": sub req failed with HTTP $status: " . $response->getBody());
} }
} catch (Exception $e) { } catch (Exception $e) {
// wtf! common_log(LOG_ERR, __METHOD__ . ": error \"{$e->getMessage()}\" hitting hub {$this->huburi} subscribing to {$this->getUri()}");
common_log(LOG_ERR, __METHOD__ . ": error \"{$e->getMessage()}\" hitting hub $this->huburi subscribing to " . $this->getUri());
// Reset the subscription state.
$orig = clone($this); $orig = clone($this);
$this->sub_state = 'inactive'; $this->sub_state = 'inactive';
$this->update($orig); $this->update($orig);
unset($orig);
// Throw the Exception again.
throw $e;
} }
return false; throw ServerException("{$mode} request failed.");
} }
/** /**

View File

@ -199,37 +199,39 @@ class Ostatus_profile extends Managed_DataObject
* Send a subscription request to the hub for this feed. * Send a subscription request to the hub for this feed.
* The hub will later send us a confirmation POST to /main/push/callback. * The hub will later send us a confirmation POST to /main/push/callback.
* *
* @return bool true on success, false on failure * @return void
* @throws ServerException if feed state is not valid * @throws ServerException if feed state is not valid or subscription fails.
*/ */
public function subscribe() public function subscribe()
{ {
$feedsub = FeedSub::ensureFeed($this->feeduri); $feedsub = FeedSub::ensureFeed($this->feeduri);
if ($feedsub->sub_state == 'active') { if ($feedsub->sub_state == 'active') {
// Active subscription, we don't need to do anything. // Active subscription, we don't need to do anything.
return true; return;
} else {
// Inactive or we got left in an inconsistent state.
// Run a subscription request to make sure we're current!
return $feedsub->subscribe();
} }
// Inactive or we got left in an inconsistent state.
// Run a subscription request to make sure we're current!
return $feedsub->subscribe();
} }
/** /**
* Check if this remote profile has any active local subscriptions, and * Check if this remote profile has any active local subscriptions, and
* if not drop the PuSH subscription feed. * if not drop the PuSH subscription feed.
* *
* @return bool true on success, false on failure * @return boolean true if subscription is removed, false if there are still subscribers to the feed
* @throws Exception of various kinds on failure.
*/ */
public function unsubscribe() { public function unsubscribe() {
$this->garbageCollect(); return $this->garbageCollect();
} }
/** /**
* Check if this remote profile has any active local subscriptions, and * Check if this remote profile has any active local subscriptions, and
* if not drop the PuSH subscription feed. * if not drop the PuSH subscription feed.
* *
* @return boolean * @return boolean true if subscription is removed, false if there are still subscribers to the feed
* @throws Exception of various kinds on failure.
*/ */
public function garbageCollect() public function garbageCollect()
{ {

View File

@ -31,11 +31,14 @@ require_once INSTALLDIR.'/scripts/commandline.inc';
$feedsub = new FeedSub(); $feedsub = new FeedSub();
while ($feedsub->fetch()) { while ($feedsub->fetch()) {
print $feedsub->uri . "(" . $feedsub->sub_state . ")"; echo "{$feedsub->uri} ({$feedsub->sub_state})";
$result = $feedsub->garbageCollect(); try {
if ($result) { if ($feedsub->garbageCollect()) {
print " INACTIVE\n"; echo " INACTIVE\n";
} else { } else {
print " ACTIVE\n"; echo " ACTIVE\n";
}
} catch (Exception $e) {
echo " ERROR: {$e->getMessage()}\n";
} }
} }

View File

@ -53,20 +53,18 @@ if (!$sub) {
print "Old state:\n"; print "Old state:\n";
showSub($sub); showSub($sub);
print "\n"; try {
echo "\n";
if (have_option('u') || have_option('--unsub')) { if (have_option('u') || have_option('--unsub')) {
print "Pinging hub $sub->huburi with unsubscription for $sub->uri\n"; echo "Pinging hub {$sub->huburi} with unsubscription for {$sub->uri}\n";
$ok = $sub->unsubscribe(); $sub->unsubscribe();
} else { } else {
print "Pinging hub $sub->huburi with new subscription for $sub->uri\n"; echo "Pinging hub {$sub->huburi} with new subscription for {$sub->uri}\n";
$ok = $sub->subscribe(); $sub->subscribe();
} }
echo "ok\n";
if ($ok) { } catch (Exception $e) {
print "ok\n"; echo 'Could not confirm. '.get_class($e).': '.$e->getMessage()."\n";
} else {
print "Could not confirm.\n";
} }
$sub2 = FeedSub::getKV('uri', $feedurl); $sub2 = FeedSub::getKV('uri', $feedurl);

View File

@ -105,14 +105,13 @@ if ($huburi != $sub->huburi) {
print "Feed record ok, not changing.\n\n"; print "Feed record ok, not changing.\n\n";
} }
print "\n"; echo "\n";
print "Pinging hub $sub->huburi with new subscription for $sub->uri\n"; echo "Pinging hub {$sub->huburi} with new subscription for {$sub->uri}\n";
$ok = $sub->subscribe(); try {
$sub->subscribe();
if ($ok) { echo "ok\n";
print "ok\n"; } catch (Exception $e) {
} else { echo 'Could not confirm. '.get_class($e).': '.$e->getMessage()."\n";
print "Could not confirm.\n";
} }
$o2 = Ostatus_profile::getKV('uri', $uri); $o2 = Ostatus_profile::getKV('uri', $uri);

View File

@ -77,11 +77,7 @@ class AddMirrorAction extends BaseMirrorAction
protected function saveMirror() protected function saveMirror()
{ {
if ($this->oprofile->subscribe()) { $this->oprofile->subscribe();
SubMirror::saveMirror($this->user, $this->profile); SubMirror::saveMirror($this->user, $this->profile);
} else {
// TRANS: Exception thrown when a subscribing to a feed fails.
$this->serverError(_m('Could not subscribe to feed.'));
}
} }
} }