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

Fail gracefully when getting a page during a bork #111

Merged
merged 4 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion notifier/newposts.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ def fetch_posts_with_context(
"post_id": post_id,
},
)
raise RuntimeError("Requested post missing from downloaded thread")

# For each kind of context, check if we already have it, and if not, fetch it

Expand Down
51 changes: 47 additions & 4 deletions notifier/wikidot.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def module(
cookies={"wikidot_token7": token7},
)
except ConnectionError as error:
will_retry = attempt_count > self.MODULE_ATTEMPT_LIMIT
will_retry = attempt_count < self.MODULE_ATTEMPT_LIMIT
logger.debug(
"Module connection failed %s",
{
Expand Down Expand Up @@ -189,7 +189,7 @@ def module(
and response["message"]
== "An error occurred while processing the request."
):
will_retry = attempt_count > self.MODULE_ATTEMPT_LIMIT
will_retry = attempt_count < self.MODULE_ATTEMPT_LIMIT
if will_retry:
logger.warning(
"Wikidot internal failure, retrying in 10 seconds %s",
Expand Down Expand Up @@ -454,9 +454,52 @@ def get_page_id(self, wiki_id: str, slug: str) -> int:
"s" if wiki["secure"] else "", wiki_id, slug
)
)
page = self._session.get(page_url).text

page_text = None
for attempt_count in range(self.MODULE_ATTEMPT_LIMIT):
attempt_delay = 2**attempt_count * self.PAGINATION_DELAY_S
will_retry = attempt_count < self.MODULE_ATTEMPT_LIMIT
time.sleep(attempt_delay)
response = self._session.get(page_url)

if response.status_code == 500:
logger.warning(
"Wikibork when getting page %s",
{
"url": page_url,
"attempt_number": attempt_count + 1,
"attempt_delay_s": attempt_delay,
"max_attempts": self.MODULE_ATTEMPT_LIMIT,
"will_retry": will_retry,
},
)
if will_retry:
continue
raise Wikibork

if response.status_code != 200:
logger.warning(
"Failed to get page %s",
{
"url": page_url,
"status_code": response.status_code,
"attempt_number": attempt_count + 1,
"attempt_delay_s": attempt_delay,
"max_attempts": self.MODULE_ATTEMPT_LIMIT,
"will_retry": will_retry,
},
)
if will_retry:
continue
raise OngoingConnectionError

page_text = response.text
assert page_text is not None

return int(
cast(Match[str], re.search(r"pageId = ([0-9]+);", page)).group(1)
cast(
Match[str], re.search(r"pageId = ([0-9]+);", page_text)
).group(1)
)

def rename_page(self, wiki_id: str, from_slug: str, to_slug: str) -> None:
Expand Down