Explorar o código

PubSubHubbub better gestion of errors

Do not assume that PubSubHubbub works until the first successul push
https://github.com/FreshRSS/FreshRSS/issues/312#issuecomment-102706500
Alexandre Alapetite %!s(int64=11) %!d(string=hai) anos
pai
achega
001c713f03
Modificáronse 3 ficheiros con 31 adicións e 11 borrados
  1. 2 2
      app/Controllers/feedController.php
  2. 22 9
      app/Models/Feed.php
  3. 7 0
      p/api/pshb.php

+ 2 - 2
app/Controllers/feedController.php

@@ -305,7 +305,7 @@ class FreshRSS_feed_Controller extends Minz_ActionController {
 			$pubSubHubbubEnabled = $feed->pubSubHubbubEnabled();
 			if ((!$simplePiePush) && (!$id) && $pubSubHubbubEnabled && ($feed->lastUpdate() > $pshbMinAge)) {
 				$text = 'Skip pull of feed using PubSubHubbub: ' . $url;
-				Minz_Log::debug($text);
+				//Minz_Log::debug($text);
 				file_put_contents(USERS_PATH . '/_/log_pshb.txt', date('c') . "\t" . $text . "\n", FILE_APPEND);
 				continue;	//When PubSubHubbub is used, do not pull refresh so often
 			}
@@ -389,7 +389,7 @@ class FreshRSS_feed_Controller extends Minz_ActionController {
 							file_put_contents(USERS_PATH . '/_/log_pshb.txt', date('c') . "\t" . $text . "\n", FILE_APPEND);
 							Minz_Log::warning($text);
 							$pubSubHubbubEnabled = false;
-							$feed->pubSubHubbubEnabled(false);	//To force the renewal of our lease
+							$feed->pubSubHubbubError(true);
 						}
 
 						if (!$entryDAO->hasTransaction()) {

+ 22 - 9
app/Models/Feed.php

@@ -359,23 +359,33 @@ class FreshRSS_Feed extends Minz_Model {
 
 	//<PubSubHubbub>
 
-	function pubSubHubbubEnabled($keep = true) {
+	function pubSubHubbubEnabled() {
 		$url = $this->selfUrl ? $this->selfUrl : $this->url;
 		$hubFilename = PSHB_PATH . '/feeds/' . base64url_encode($url) . '/!hub.json';
 		if ($hubFile = @file_get_contents($hubFilename)) {
 			$hubJson = json_decode($hubFile, true);
-			if (!$keep) {
-				$hubJson['lease_end'] = time() - 60;
-				file_put_contents($hubFilename, json_encode($hubJson));
-				file_put_contents(USERS_PATH . '/_/log_pshb.txt', date('c') . "\t"
-					. 'Force expire lease for ' . $url . "\n", FILE_APPEND);
-			} elseif ($hubJson && (empty($hubJson['lease_end']) || $hubJson['lease_end'] > time())) {
+			if ($hubJson && empty($hubJson['error']) &&
+				(empty($hubJson['lease_end']) || $hubJson['lease_end'] > time())) {
 				return true;
 			}
 		}
 		return false;
 	}
 
+	function pubSubHubbubError($error = true) {
+		$url = $this->selfUrl ? $this->selfUrl : $this->url;
+		$hubFilename = PSHB_PATH . '/feeds/' . base64url_encode($url) . '/!hub.json';
+		$hubFile = @file_get_contents($hubFilename);
+		$hubJson = $hubFile ? json_decode($hubFile, true) : array();
+		if (!isset($hubJson['error']) || $hubJson['error'] !== (bool)$error) {
+			$hubJson['error'] = (bool)$error;
+			file_put_contents($hubFilename, json_encode($hubJson));
+			file_put_contents(USERS_PATH . '/_/log_pshb.txt', date('c') . "\t"
+				. 'Set error to ' . ($error ? 1 : 0) . ' for ' . $url . "\n", FILE_APPEND);
+		}
+		return false;
+	}
+
 	function pubSubHubbubPrepare() {
 		$key = '';
 		if (FreshRSS_Context::$system_conf->base_url && $this->hubUrl && $this->selfUrl) {
@@ -389,17 +399,20 @@ class FreshRSS_Feed extends Minz_Model {
 					file_put_contents(USERS_PATH . '/_/log_pshb.txt', date('c') . "\t" . $text . "\n", FILE_APPEND);
 					return false;
 				}
-				if (empty($hubJson['lease_end']) || ($hubJson['lease_end'] <= (time() + (3600 * 24)))) {	//TODO: Make a better policy
+				if ((!empty($hubJson['lease_end'])) && ($hubJson['lease_end'] < (time() + (3600 * 23)))) {	//TODO: Make a better policy
 					$text = 'PubSubHubbub lease ends at '
 						. date('c', empty($hubJson['lease_end']) ? time() : $hubJson['lease_end'])
 						. ' and needs renewal: ' . $this->url;
 					Minz_Log::warning($text);
 					file_put_contents(USERS_PATH . '/_/log_pshb.txt', date('c') . "\t" . $text . "\n", FILE_APPEND);
 					$key = $hubJson['key'];	//To renew our lease
+				} elseif (((!empty($hubJson['error'])) || empty($hubJson['lease_end'])) &&
+					(empty($hubJson['lease_start']) || $hubJson['lease_start'] < time() - (3600 * 23))) {	//Do not renew too often
+					$key = $hubJson['key'];	//To renew our lease
 				}
 			} else {
 				@mkdir($path, 0777, true);
-				$key = sha1(FreshRSS_Context::$system_conf->salt . uniqid(mt_rand(), true));
+				$key = sha1($path . FreshRSS_Context::$system_conf->salt . uniqid(mt_rand(), true));
 				$hubJson = array(
 					'hub' => $this->hubUrl,
 					'key' => $key,

+ 7 - 0
p/api/pshb.php

@@ -60,6 +60,10 @@ if (!empty($_REQUEST['hub_mode']) && $_REQUEST['hub_mode'] === 'subscribe') {
 	} else {
 		unset($hubJson['lease_end']);
 	}
+	$hubJson['lease_start'] = time();
+	if (!isset($hubJson['error'])) {
+		$hubJson['error'] = true;	//Do not assume that PubSubHubbub works until the first successul push
+	}
 	file_put_contents('./!hub.json', json_encode($hubJson));
 	exit(isset($_REQUEST['hub_challenge']) ? $_REQUEST['hub_challenge'] : '');
 }
@@ -120,6 +124,9 @@ if ($nb === 0) {
 	header('HTTP/1.1 410 Gone');
 	logMe('Error: Nobody is subscribed to this feed anymore after all!: ' . $self);
 	die('Nobody is subscribed to this feed anymore after all!');
+} elseif (!empty($hubJson['error'])) {
+	$hubJson['error'] = false;
+	file_put_contents('./!hub.json', json_encode($hubJson));
 }
 
 logMe('PubSubHubbub ' . $self . ' done: ' . $nb);