Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server Timeout makes ther listener to crash #7

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
### Fixed

- Fix sources not found when setting seen URLs ([#6](https://github.com/XaviArnaus/masto-feed/pull/6))
- Server TimeOut makes the Listener to crash ([#7](https://github.com/XaviArnaus/masto-feed/pull/7))
33 changes: 24 additions & 9 deletions mastofeed/runners/listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from mastofeed.runners.runner_protocol import RunnerProtocol
from definitions import ROOT_DIR
import logging
from mastodon.Mastodon import MastodonReadTimeout


class Listener(RunnerProtocol):
Expand All @@ -32,16 +33,18 @@ def run(self) -> None:
# Set the listener for the Streaming for User stuff
mastodon_instance.stream_user(mention_listener)

except Exception as e:
if self._config.get("janitor.active", False):
remote_url = self._config.get("janitor.remote_url")
if remote_url is not None and not self._config.get("publisher.dry_run"):
app_name = self._config.get("app.name")
Janitor(remote_url).error(
message="```\n" + full_stack() + "\n```",
summary=f"MastoFeed Listener [{app_name}] failed: {e}"
)
except MastodonReadTimeout as e:
# Yeah, the servers may give a Timeout from time to time.
# How important is that?
self._notify_through_janitor(e)
# Log the error in a smaller way
self._logger.error(f"Server Timeout: {e}")
# Let's try an infinite loop
self.run()

except Exception as e:
self._notify_through_janitor(e)
# Log the exception
self._logger.exception(e)
# Let's try an infinite loop
self.run()
Expand All @@ -55,6 +58,18 @@ def _get_default_mastodon_instance(self, named_account="default"):
base_path=ROOT_DIR
)

def _notify_through_janitor(self, e):
is_janitor_active = self._config.get("janitor.active", False)
is_dry_run = self._config.get("publisher.dry_run", True)
remote_url = self._config.get("janitor.remote_url", None)

if is_janitor_active and not is_dry_run and remote_url is not None:
app_name = self._config.get("app.name")
Janitor(remote_url).error(
message="```\n" + full_stack() + "\n```",
summary=f"MastoFeed Listener [{app_name}] failed: {e}"
)


if __name__ == '__main__':
Listener().run()
22 changes: 13 additions & 9 deletions mastofeed/runners/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,17 +145,21 @@ def run(self) -> None:
self._publisher.publish_all_from_queue()

except Exception as e:
if self._config.get("janitor.active", False):
remote_url = self._config.get("janitor.remote_url")
if remote_url is not None and not self._config.get("publisher.dry_run"):
app_name = self._config.get("app.name")
Janitor(remote_url).error(
message="```\n" + full_stack() + "\n```",
summary=f"MastoFeed Main [{app_name}] failed: {e}"
)

self._notify_through_janitor(e)
self._logger.exception(e)

def _notify_through_janitor(self, e):
is_janitor_active = self._config.get("janitor.active", False)
is_dry_run = self._config.get("publisher.dry_run", True)
remote_url = self._config.get("janitor.remote_url", None)

if is_janitor_active and not is_dry_run and remote_url is not None:
app_name = self._config.get("app.name")
Janitor(remote_url).error(
message="```\n" + full_stack() + "\n```",
summary=f"MastoFeed Main [{app_name}] failed: {e}"
)

def load_active_parsers(self) -> dict:
"""Get the list of parsers that are active"""
return {
Expand Down
Loading