ref: avoid double nodinfo retrieve

pull/39/head
db0 2023-09-26 14:13:49 +02:00
parent 9419d1e3e5
commit ee22c5d145
4 changed files with 15 additions and 6 deletions

View File

@ -35,6 +35,8 @@ class InstanceInfo():
return return
self.node_info = InstanceInfo.get_nodeinfo(domain,req_timeout=self._req_timeout) self.node_info = InstanceInfo.get_nodeinfo(domain,req_timeout=self._req_timeout)
def get_instance_info(self):
try: try:
self.parse_instance_info() self.parse_instance_info()
except Exception as err: except Exception as err:
@ -190,9 +192,10 @@ class InstanceInfo():
self.open_registrations = self.node_info.get("openRegistrations", False) self.open_registrations = self.node_info.get("openRegistrations", False)
def discover_info(self): def discover_info(self):
# Mastodon API
site = requests.get(f"https://{self.domain}/api/v1/instance",timeout=self._req_timeout,allow_redirects=False) site = requests.get(f"https://{self.domain}/api/v1/instance",timeout=self._req_timeout,allow_redirects=False)
if site.status_code != 200: if site.status_code != 200:
raise Exception(f"Unexpected status code retrieved when discovering nodeinfo: {site.status_code}") raise Exception(f"Unexpected status code retrieved when discovering instance info: {site.status_code}")
self.instance_info = site.json() self.instance_info = site.json()
self.approval_required = self.instance_info.get("approval_required") self.approval_required = self.instance_info.get("approval_required")
if self.node_info is None: if self.node_info is None:
@ -225,6 +228,8 @@ class InstanceInfo():
"mitra": self.get_firefish_info, "mitra": self.get_firefish_info,
"unknown": self.get_unknown_info, "unknown": self.get_unknown_info,
"wildcard": self.get_unknown_info, "wildcard": self.get_unknown_info,
# Instance info not supported for misskey yet
"misskey": self.get_unknown_info,
} }
if self.software not in software_map: if self.software not in software_map:
self.discover_info() self.discover_info()
@ -260,7 +265,8 @@ class InstanceInfo():
raise Exception(f"Status code unexpected for instance frontpage: {req.status_code}") raise Exception(f"Status code unexpected for instance frontpage: {req.status_code}")
# Debug # Debug
# ii = InstanceInfo("lemmy.dbzer0.com") # ii = InstanceInfo("makai.chaotic.ninja")
# ii.get_instance_info()
# logger.debug([ # logger.debug([
# ii.software, # ii.software,
# ii.open_registrations, # ii.open_registrations,

View File

@ -187,7 +187,9 @@ class ActivityPubPM:
admins = database.find_admins_by_instance(instance) admins = database.find_admins_by_instance(instance)
if not admins: if not admins:
try: try:
admins = InstanceInfo(domain).admin_usernames ii = InstanceInfo(domain)
ii.get_instance_info()
admins = ii.admin_usernames
except Exception as err: except Exception as err:
if software not in SUPPORTED_SOFTWARE: if software not in SUPPORTED_SOFTWARE:
logger.warning(f"Failed to figure out admins from {software}: {domain}") logger.warning(f"Failed to figure out admins from {software}: {domain}")

View File

@ -10,12 +10,12 @@ def ensure_instance_registered(domain, allow_unreachable=False, record_unreachab
instance = database.find_instance_by_domain(domain) instance = database.find_instance_by_domain(domain)
try: try:
instance_info = InstanceInfo(domain,allow_unreachable=allow_unreachable, req_timeout=allowed_timeout) instance_info = InstanceInfo(domain,allow_unreachable=allow_unreachable, req_timeout=allowed_timeout)
instance_info.get_instance_info()
except Exception as err: except Exception as err:
if record_unreachable and instance and instance.software != "wildcard": if record_unreachable and instance and instance.software != "wildcard":
# We only consider an instance unreachable if we can't reach its nodeinfo # We only consider an instance unreachable if we can't reach its nodeinfo
# This means that a misconfigured instance will also be considered as 'down' # This means that a misconfigured instance will also be considered as 'down'
nodeinfo = InstanceInfo.get_nodeinfo(domain,req_timeout=allowed_timeout) if instance_info.node_info is None:
if nodeinfo is None:
logger.warning(f"Recorded {domain} as unreachable.") logger.warning(f"Recorded {domain} as unreachable.")
instance.updated = datetime.utcnow() instance.updated = datetime.utcnow()
instance.poll_failures += 1 instance.poll_failures += 1

View File

@ -47,7 +47,8 @@ if __name__ == "__main__":
for instance in database.get_all_instances(0,0): for instance in database.get_all_instances(0,0):
if instance.software == 'wildcard': if instance.software == 'wildcard':
continue continue
if instance.poll_failures > int(os.getenv('FEDISEER_IGNORE_POLL_FAILS', 0)): # -1 doesn't skip anything
if os.getenv('FEDISEER_IGNORE_POLL_FAILS', -1) >= 0 and instance.poll_failures > int(os.getenv('FEDISEER_IGNORE_POLL_FAILS', 0)):
logger.debug(f"Skipped {instance.domain} due to too many poll fails.") logger.debug(f"Skipped {instance.domain} due to too many poll fails.")
continue continue
futures.append(executor.submit(refresh_info, instance.domain)) futures.append(executor.submit(refresh_info, instance.domain))