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

Pap 138 - Désactiver le serveur web pendant le scraping #47

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion 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 @@ -630,6 +631,7 @@ 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"]
Expand Down Expand Up @@ -667,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