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 Oct 27, 2023
1 parent 42a24ea commit af869e4
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 @@ -16,6 +16,7 @@
from ..cli_helper import search
from ..config import config as config_var, load_config
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 @@ -595,11 +596,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 @@ -610,13 +606,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 @@ -651,4 +645,4 @@ async def app(page):
page.print(target := H.div().autoid())
return await fn(page, page[target])

return app
return redirect_page_if_scraping(app)
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 af869e4

Please sign in to comment.