Spaces:
Sleeping
Sleeping
Commit
·
61c1719
1
Parent(s):
e02fa6d
unstable fix
Browse files- LoadBalancer.py +14 -6
- TODO.md +4 -0
- app.py +31 -6
LoadBalancer.py
CHANGED
|
@@ -4,6 +4,7 @@ import re
|
|
| 4 |
import urllib.parse
|
| 5 |
from tvdb import fetch_and_cache_json
|
| 6 |
from threading import Event, Thread
|
|
|
|
| 7 |
import asyncio
|
| 8 |
import time
|
| 9 |
import logging
|
|
@@ -14,7 +15,7 @@ CACHE_DIR = os.getenv("CACHE_DIR")
|
|
| 14 |
|
| 15 |
class LoadBalancer:
|
| 16 |
def __init__(self, cache_dir, token, repo, polling_interval=4, max_retries=3, initial_delay=1):
|
| 17 |
-
self.version = "0.0.0.
|
| 18 |
self.instances = []
|
| 19 |
self.instances_health = {}
|
| 20 |
self.polling_interval = polling_interval
|
|
@@ -35,7 +36,7 @@ class LoadBalancer:
|
|
| 35 |
os.makedirs(self.CACHE_DIR)
|
| 36 |
|
| 37 |
# Initialize file structure and prefetching
|
| 38 |
-
self.file_structure = indexer()
|
| 39 |
self.start_prefetching()
|
| 40 |
|
| 41 |
# Start polling and file checking in separate threads
|
|
@@ -49,8 +50,12 @@ class LoadBalancer:
|
|
| 49 |
content_check_thread.start()
|
| 50 |
|
| 51 |
def start_prefetching(self):
|
| 52 |
-
"""Start the metadata prefetching
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
async def prefetch_metadata(self):
|
| 56 |
"""Prefetch metadata for all items in the file structure."""
|
|
@@ -88,9 +93,12 @@ class LoadBalancer:
|
|
| 88 |
"""Periodically check for updates and trigger prefetching if new content is detected."""
|
| 89 |
while not self.stop_event.is_set():
|
| 90 |
time.sleep(120) # Wait for 2 minutes
|
| 91 |
-
current_file_structure = indexer()
|
| 92 |
if current_file_structure != self.previous_file_structure:
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
| 94 |
self.previous_file_structure = current_file_structure
|
| 95 |
else:
|
| 96 |
logging.info("No new content detected.")
|
|
|
|
| 4 |
import urllib.parse
|
| 5 |
from tvdb import fetch_and_cache_json
|
| 6 |
from threading import Event, Thread
|
| 7 |
+
from fastapi import BackgroundTasks
|
| 8 |
import asyncio
|
| 9 |
import time
|
| 10 |
import logging
|
|
|
|
| 15 |
|
| 16 |
class LoadBalancer:
|
| 17 |
def __init__(self, cache_dir, token, repo, polling_interval=4, max_retries=3, initial_delay=1):
|
| 18 |
+
self.version = "0.0.0.1 Alpha"
|
| 19 |
self.instances = []
|
| 20 |
self.instances_health = {}
|
| 21 |
self.polling_interval = polling_interval
|
|
|
|
| 36 |
os.makedirs(self.CACHE_DIR)
|
| 37 |
|
| 38 |
# Initialize file structure and prefetching
|
| 39 |
+
self.file_structure = self.indexer()
|
| 40 |
self.start_prefetching()
|
| 41 |
|
| 42 |
# Start polling and file checking in separate threads
|
|
|
|
| 50 |
content_check_thread.start()
|
| 51 |
|
| 52 |
def start_prefetching(self):
|
| 53 |
+
"""Start the metadata prefetching using FastAPI background tasks."""
|
| 54 |
+
# Create a FastAPI BackgroundTasks instance
|
| 55 |
+
background_tasks = BackgroundTasks()
|
| 56 |
+
# Add the prefetch_metadata coroutine to the background tasks
|
| 57 |
+
background_tasks.add_task(self.prefetch_metadata)
|
| 58 |
+
return background_tasks
|
| 59 |
|
| 60 |
async def prefetch_metadata(self):
|
| 61 |
"""Prefetch metadata for all items in the file structure."""
|
|
|
|
| 93 |
"""Periodically check for updates and trigger prefetching if new content is detected."""
|
| 94 |
while not self.stop_event.is_set():
|
| 95 |
time.sleep(120) # Wait for 2 minutes
|
| 96 |
+
current_file_structure = self.indexer()
|
| 97 |
if current_file_structure != self.previous_file_structure:
|
| 98 |
+
# Trigger prefetching using FastAPI background tasks
|
| 99 |
+
background_tasks = self.start_prefetching()
|
| 100 |
+
# Here you need to integrate background_tasks with FastAPI
|
| 101 |
+
# For example, you might need to pass it to your FastAPI route handler or another component
|
| 102 |
self.previous_file_structure = current_file_structure
|
| 103 |
else:
|
| 104 |
logging.info("No new content detected.")
|
TODO.md
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
create endpoints for
|
| 2 |
+
* /api/get/film/{title} `Endpoint to get the movie by title.`
|
| 3 |
+
|
| 4 |
+
* /api/get/tv/{title}/{season}/{episode} `Endpoint to get the episode by title, season and episode.`
|
app.py
CHANGED
|
@@ -1,9 +1,10 @@
|
|
| 1 |
-
from fastapi import FastAPI,HTTPException
|
| 2 |
from fastapi.responses import JSONResponse
|
| 3 |
from LoadBalancer import LoadBalancer
|
|
|
|
| 4 |
import os
|
| 5 |
import urllib.parse
|
| 6 |
-
from utils import read_json_file
|
| 7 |
|
| 8 |
CACHE_DIR = os.getenv("CACHE_DIR")
|
| 9 |
TOKEN = os.getenv("TOKEN")
|
|
@@ -21,9 +22,25 @@ def greet_json():
|
|
| 21 |
return {"Version": load_balancer.version}
|
| 22 |
|
| 23 |
@app.post("/api/post/register")
|
| 24 |
-
async def register_instance():
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
@app.get("/api/get/file_structure")
|
| 29 |
async def get_file_structure():
|
|
@@ -94,4 +111,12 @@ async def get_season_metadata_api(series_id: int, season: str):
|
|
| 94 |
data = await read_json_file(json_cache_path)
|
| 95 |
return JSONResponse(content=data)
|
| 96 |
|
| 97 |
-
raise HTTPException(status_code=404, detail="Metadata not found")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI,HTTPException, Request
|
| 2 |
from fastapi.responses import JSONResponse
|
| 3 |
from LoadBalancer import LoadBalancer
|
| 4 |
+
import logging
|
| 5 |
import os
|
| 6 |
import urllib.parse
|
| 7 |
+
from utils import read_json_file, is_valid_url
|
| 8 |
|
| 9 |
CACHE_DIR = os.getenv("CACHE_DIR")
|
| 10 |
TOKEN = os.getenv("TOKEN")
|
|
|
|
| 22 |
return {"Version": load_balancer.version}
|
| 23 |
|
| 24 |
@app.post("/api/post/register")
|
| 25 |
+
async def register_instance(request: Request):
|
| 26 |
+
try:
|
| 27 |
+
data = await request.json()
|
| 28 |
+
if not data or "url" not in data:
|
| 29 |
+
return JSONResponse(content={"error": "No URL provided"}, status_code=400)
|
| 30 |
+
|
| 31 |
+
url = data["url"]
|
| 32 |
+
if not is_valid_url(url):
|
| 33 |
+
return JSONResponse(content={"error": "Invalid URL"}, status_code=400)
|
| 34 |
+
|
| 35 |
+
# Register the instance
|
| 36 |
+
load_balancer.register_instance(url)
|
| 37 |
+
logging.info(f"Instance registered: {url}")
|
| 38 |
+
|
| 39 |
+
return JSONResponse(content={"message": f"Instance {url} registered successfully"}, status_code=200)
|
| 40 |
+
|
| 41 |
+
except Exception as e:
|
| 42 |
+
logging.error(f"Error registering instance: {e}")
|
| 43 |
+
return JSONResponse(content={"error": "Failed to register instance"}, status_code=500)
|
| 44 |
|
| 45 |
@app.get("/api/get/file_structure")
|
| 46 |
async def get_file_structure():
|
|
|
|
| 111 |
data = await read_json_file(json_cache_path)
|
| 112 |
return JSONResponse(content=data)
|
| 113 |
|
| 114 |
+
raise HTTPException(status_code=404, detail="Metadata not found")
|
| 115 |
+
|
| 116 |
+
@app.get('/api/get/instances')
|
| 117 |
+
def get_instances():
|
| 118 |
+
return load_balancer.instances
|
| 119 |
+
|
| 120 |
+
@app.get('/api/get/instances/health')
|
| 121 |
+
def get_instances_health():
|
| 122 |
+
return load_balancer.instances_health
|