Added filter for only show cached stream, Redis lock for prevent concurrent live scrape, bug fixes & improvements (#325)

* cleanup lifespan vars

* Show API process time in logs

* store the cache validation of full URL for live sports event

* Add support for filter only show cached streams

* Update default configs

* properly handle prowlarr exception on MaxProcessLimitReached

* Add redis lock mechanism to prevent concurrent live scrapers from multiple instances of stremio addon installation users
This commit is contained in:
Mohamed Zumair
2024-10-15 18:43:03 +05:30
committed by GitHub
parent e7b252177b
commit ecaf7e5cf1
9 changed files with 71 additions and 27 deletions
+11 -12
View File
@@ -58,32 +58,31 @@ async def lifespan(fastapi_app: FastAPI):
# Startup logic
await database.init()
await torrent.init_best_trackers()
scheduler = None
scheduler_lock = None
if not settings.disable_all_scheduler:
acquired, lock = await acquire_scheduler_lock()
acquired, scheduler_lock = await acquire_scheduler_lock()
if acquired:
try:
scheduler = AsyncIOScheduler()
setup_scheduler(scheduler)
scheduler.start()
fastapi_app.state.scheduler = scheduler
fastapi_app.state.scheduler_lock = lock
await asyncio.create_task(maintain_heartbeat())
except Exception as e:
await release_scheduler_lock(lock)
await release_scheduler_lock(scheduler_lock)
raise e
yield
# Shutdown logic
if hasattr(fastapi_app.state, "scheduler"):
fastapi_app.state.scheduler.shutdown(wait=False)
if (
hasattr(fastapi_app.state, "scheduler_lock")
and fastapi_app.state.scheduler_lock
):
await release_scheduler_lock(fastapi_app.state.scheduler_lock)
if scheduler:
try:
scheduler.shutdown(wait=False)
except Exception as e:
logging.exception("Error shutting down scheduler")
finally:
await release_scheduler_lock(scheduler_lock)
await REDIS_ASYNC_CLIENT.aclose()
+3 -2
View File
@@ -42,12 +42,13 @@ class SecureLoggingMiddleware(BaseHTTPMiddleware):
async def custom_log(request: Request, response: Response):
ip = get_client_ip(request)
url_path = str(request.url)
process_time = response.headers.get("X-Process-Time", "")
if request.path_params.get("secret_str"):
url_path = url_path.replace(
request.path_params.get("secret_str"), "***MASKED***"
request.path_params.get("secret_str"), "*MASKED*"
)
logging.info(
f'{ip} - "{request.method} {url_path} HTTP/1.1" {response.status_code}'
f'{ip} - "{request.method} {url_path} HTTP/1.1" {response.status_code} {process_time}'
)