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

View File

@ -192,7 +192,7 @@ class FeedSub extends Managed_DataObject
* Send a subscription request to the hub for this feed.
* 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
*/
public function subscribe()
@ -203,7 +203,7 @@ class FeedSub extends Managed_DataObject
if (!Event::handle('FeedSubscribe', array($this))) {
// A plugin handled it
return true;
return;
}
if (empty($this->huburi)) {
@ -213,14 +213,14 @@ class FeedSub extends Managed_DataObject
} else if (common_config('feedsub', 'nohub')) {
// Fake it! We're just testing remote feeds w/o hubs.
// We'll never actually get updates in this mode.
return true;
return;
} else {
// TRANS: Server exception.
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,
* which confirms there's no uses left.
*
* @return bool true on success, false on failure
* @throws ServerException if feed state is not valid
*/
public function unsubscribe() {
@ -240,7 +239,7 @@ class FeedSub extends Managed_DataObject
if (!Event::handle('FeedUnsubscribe', array($this))) {
// A plugin handled it
return true;
return;
}
if (empty($this->huburi)) {
@ -250,14 +249,14 @@ class FeedSub extends Managed_DataObject
} else if (common_config('feedsub', 'nohub')) {
// Fake it! We're just testing remote feeds w/o hubs.
// We'll never actually get updates in this mode.
return true;
return;
} else {
// TRANS: Server exception.
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.
*
* @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()
{
@ -277,12 +277,14 @@ class FeedSub extends Managed_DataObject
$count = 0;
Event::handle('FeedSubSubscriberCount', array($this, &$count));
if ($count) {
if ($count > 0) {
common_log(LOG_INFO, __METHOD__ . ': ok, ' . $count . ' user(s) left for ' . $this->getUri());
return false;
} else {
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
* 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)
{
@ -344,29 +347,32 @@ class FeedSub extends Managed_DataObject
$client->setAuth($u, $p);
}
} else {
throw new FeedSubException('WTF?');
throw new FeedSubException('Server could not find a usable PuSH hub.');
}
}
$response = $client->post($hub, $headers, $post);
$status = $response->getStatus();
// PuSH specificed response status code
if ($status == 202) {
common_log(LOG_INFO, __METHOD__ . ': sub req ok, awaiting verification callback');
return true;
return;
} else if ($status >= 200 && $status < 300) {
common_log(LOG_ERR, __METHOD__ . ": sub req returned unexpected HTTP $status: " . $response->getBody());
} else {
common_log(LOG_ERR, __METHOD__ . ": sub req failed with HTTP $status: " . $response->getBody());
}
} 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);
$this->sub_state = 'inactive';
$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.
* The hub will later send us a confirmation POST to /main/push/callback.
*
* @return bool true on success, false on failure
* @throws ServerException if feed state is not valid
* @return void
* @throws ServerException if feed state is not valid or subscription fails.
*/
public function subscribe()
{
$feedsub = FeedSub::ensureFeed($this->feeduri);
if ($feedsub->sub_state == 'active') {
// Active subscription, we don't need to do anything.
return true;
} else {
// Inactive or we got left in an inconsistent state.
// Run a subscription request to make sure we're current!
return $feedsub->subscribe();
return;
}
// 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
* 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() {
$this->garbageCollect();
return $this->garbageCollect();
}
/**
* Check if this remote profile has any active local subscriptions, and
* 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()
{

View File

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

View File

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

View File

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

View File

@ -77,11 +77,7 @@ class AddMirrorAction extends BaseMirrorAction
protected function saveMirror()
{
if ($this->oprofile->subscribe()) {
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.'));
}
$this->oprofile->subscribe();
SubMirror::saveMirror($this->user, $this->profile);
}
}