Skip to content

Commit

Permalink
Add decorators to display "unavailable" message when scraping is runn…
Browse files Browse the repository at this point in the history
…ing.
  • Loading branch information
notoraptor committed Apr 15, 2024
1 parent 0ab6a5f commit 60b8737
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 9 deletions.
12 changes: 3 additions & 9 deletions paperoni/webapp/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from ..cli_helper import search
from ..utils import keyword_decorator
from .utils import redirect_index_if_scraping, redirect_page_if_scraping
from . import filters

here = Path(__file__).parent
Expand Down Expand Up @@ -620,11 +621,6 @@ def template(path, location=None, **kw):
)


def scraper_is_running():
status = os.popen("systemctl is-active paperoni-scraper.service").read().strip()
return status


# TODO: This is a copy of grizzlaxy.index.Index to avoid updating grizzlaxy during
# my time off -- OB
class Index(LoneBear):
Expand All @@ -635,13 +631,11 @@ def __init__(self, template=here / "mila-template.html"):
self.location = template.parent if isinstance(template, Path) else None
self.template = template

@redirect_index_if_scraping
async def run(self, request):
scope = request.scope
app = scope["app"]
root_path = scope["root_path"]

print("GOTO", repr(root_path), scraper_is_running())

content = render("/", app.map, restrict=root_path)
if content is None:
content = render(
Expand Down Expand Up @@ -675,4 +669,4 @@ async def app(page):
page.print(target := H.div().autoid())
return await fn(page, page[target])

return bear(app, template_params={"title": actual_title})
return bear(redirect_page_if_scraping(app), template_params={"title": actual_title})
2 changes: 2 additions & 0 deletions paperoni/webapp/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
from starbear import simplebear, template

from .common import template
from .utils import redirect_request_if_scraping

here = Path(__file__).parent


@simplebear
@redirect_request_if_scraping
async def help(request):
"""Help."""
md = (here / "help.md").read_text()
Expand Down
67 changes: 67 additions & 0 deletions paperoni/webapp/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import os
import logging
import re
from datetime import datetime
import functools
from hrepr import H


# TODO: move this to starbear and starbear.utils.StarbearHandler
Expand Down Expand Up @@ -128,3 +131,67 @@ def paper_score(self, paper):
]
total_score = sum(x for _, x in author_scores)
return total_score, author_scores


UNAVAILABLE_TITLE = "Server temporarily unavailable, please retry later"
UNAVAILABLE_CONTENT = (
"Server is temporarily unavailable, please retry in few minutes."
)


def scraper_is_running() -> bool:
"""Return True if Paperoni scraping service is running."""
status = (
os.popen("systemctl is-active paperoni-scraper.service").read().strip()
)
return status != "inactive"


def redirect_index_if_scraping(method):
"""Decorator to display temporarily unavailable page on Index.__call__() if scraping is running."""
from .common import template

@functools.wraps(method)
async def wrapper(self, *args, **kwargs):
if scraper_is_running():
return template(
self.template,
body=H.div(UNAVAILABLE_CONTENT, id="index"),
title=UNAVAILABLE_TITLE,
)
else:
return await method(self, *args, **kwargs)

return wrapper


def redirect_request_if_scraping(fn):
"""Decorator to display temporarily unavailable page on run(request) if scraping is running."""
from .common import template, here

@functools.wraps(fn)
async def wrapper(request):
if scraper_is_running():
return template(
here / "mila-template.html",
title=UNAVAILABLE_TITLE,
body=H.div(UNAVAILABLE_CONTENT),
)
else:
return await fn(request)

return wrapper


def redirect_page_if_scraping(fn):
"""Decorator to display temporarily unavailable page on app(page, ...) if scraping is running."""

@functools.wraps(fn)
async def wrapper(page, *args, **kwargs):
if scraper_is_running():
page["head"].print(H.title(UNAVAILABLE_TITLE))
page.print(UNAVAILABLE_CONTENT)
else:
await fn(page, *args, **kwargs)

return wrapper

0 comments on commit 60b8737

Please sign in to comment.