Skip to content

Commit

Permalink
Add Timeout Exception Fix: Workers keep getting killed #16
Browse files Browse the repository at this point in the history
  • Loading branch information
LimeDrive committed Jun 15, 2024
1 parent ec569cb commit 1122619
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 4 deletions.
12 changes: 11 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ flask-session = "^0.8.0"
flask-sqlalchemy = "^3.1.1"
orjson = "^3.10.4"
stackprinter = "^0.2.12"
timeout-decorator = "^0.5.0"


[build-system]
Expand Down
6 changes: 5 additions & 1 deletion ygg_rss_proxy/app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from flask import Flask, request, jsonify, Response
from flask_session import Session
from flask_sqlalchemy import SQLAlchemy
from timeout_decorator import TimeoutError
from ygg_rss_proxy.rss import get_rss_feed, replace_torrent_links
from ygg_rss_proxy.settings import settings
from ygg_rss_proxy.logging_config import logger
Expand Down Expand Up @@ -40,7 +41,10 @@ def proxy_rss():
query_params = request.query_string.decode("utf-8")
ygg_session = get_session()

response = get_rss_feed(query_params, requests_session=ygg_session)
try:
response = get_rss_feed(query_params, requests_session=ygg_session)
except TimeoutError as e:
logger.error(f"Timeout Err: {e}")

if response.status_code in [401, 403, 307, 301]:
# Session may have expired, re-authenticate and retry the request
Expand Down
3 changes: 2 additions & 1 deletion ygg_rss_proxy/auth.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import requests
import timeout_decorator
from ygg_rss_proxy.fspy import FlareSolverr
from ygg_rss_proxy.settings import settings
from ygg_rss_proxy.logging_config import logger
Expand Down Expand Up @@ -126,7 +127,7 @@ def ygg_cloudflare_login(
)
raise Exception("Failed to authenticate to YGG")


@timeout_decorator.timeout(90, exception_message=f"Timeout after 90 seconds")
def ygg_login(
session=requests.Session(), ygg_playload: dict = ygg_playload
) -> requests.Session:
Expand Down
3 changes: 3 additions & 0 deletions ygg_rss_proxy/rss.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@
from lxml import etree
from ygg_rss_proxy.settings import settings
import requests
import timeout_decorator

# URLs
URL_RSS: str = f"{settings.ygg_url}/rss"
URL_TORRENTS: str = f"{settings.ygg_url}/rss/download"
URL_PROXY = f"{settings.rss_shema}://{settings.rss_host}:{settings.rss_port}"


@timeout_decorator.timeout(30, exception_message=f"Timeout after 30 seconds")
def get_rss_feed(query_params, requests_session: requests.Session) -> requests.Response:
rss_url_with_params = f"{URL_RSS}?{query_params}"
response = requests_session.get(rss_url_with_params)
return response


@timeout_decorator.timeout(30, exception_message=f"Timeout after 30 seconds")
def replace_torrent_links(rss_content) -> Any:
parser = etree.XMLParser(recover=True)
tree = etree.fromstring(rss_content, parser)
Expand Down
4 changes: 3 additions & 1 deletion ygg_rss_proxy/session_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
import pickle
from flask import session
from sqlalchemy import text
import timeout_decorator
from requests.utils import dict_from_cookiejar, cookiejar_from_dict
from ygg_rss_proxy.auth import ygg_login
from ygg_rss_proxy.logging_config import logger

@timeout_decorator.timeout(3, exception_message=f"Timeout after 3 seconds")
def check_database_connection():
from ygg_rss_proxy.app import db
try:
Expand Down Expand Up @@ -44,7 +46,7 @@ def init_session() -> None:
if "session_data" not in session:
new_session()


@timeout_decorator.timeout(90, exception_message=f"Timeout after 90 seconds")
def get_session() -> requests.Session:
"""
This function retrieves a session by checking if session data exists.
Expand Down

0 comments on commit 1122619

Please sign in to comment.