Spaces:
Paused
Paused
ChandimaPrabath
commited on
Commit
•
5cf4c09
1
Parent(s):
818ad36
0.2.3 V Alpha
Browse files
Instance.py
CHANGED
@@ -9,13 +9,14 @@ from tqdm import tqdm
|
|
9 |
from indexer import indexer
|
10 |
import re
|
11 |
from tvdb import fetch_and_cache_json
|
|
|
12 |
|
13 |
CACHE_DIR = os.getenv("CACHE_DIR")
|
14 |
|
15 |
download_progress = {}
|
16 |
|
17 |
class Instance:
|
18 |
-
def __init__(self, id, url, cache_dir, index_file, token, repo):
|
19 |
self.version = "0.2.3 V Alpha"
|
20 |
self.id = id
|
21 |
self.url = url
|
@@ -27,6 +28,9 @@ class Instance:
|
|
27 |
self.TV_STORE_JSON_PATH = os.path.join(cache_dir, "tv_store.json")
|
28 |
self.download_threads = {}
|
29 |
self.file_structure = None
|
|
|
|
|
|
|
30 |
|
31 |
# Ensure CACHE_DIR exists
|
32 |
if not os.path.exists(self.CACHE_DIR):
|
@@ -49,6 +53,7 @@ class Instance:
|
|
49 |
|
50 |
# Start prefetching metadata
|
51 |
thread = Thread(target=self.start_prefetching)
|
|
|
52 |
thread.daemon = True
|
53 |
thread.start()
|
54 |
|
@@ -58,7 +63,8 @@ class Instance:
|
|
58 |
cache_size = self.get_cache_size()
|
59 |
|
60 |
report = {
|
61 |
-
"
|
|
|
62 |
"film_store": self.read_json(film_store_path),
|
63 |
"tv_store": self.read_json(tv_store_path),
|
64 |
"cache_size": cache_size
|
@@ -379,4 +385,23 @@ class Instance:
|
|
379 |
|
380 |
def start_prefetching(self):
|
381 |
"""Start the metadata prefetching in a separate thread."""
|
382 |
-
self.prefetch_metadata()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
from indexer import indexer
|
10 |
import re
|
11 |
from tvdb import fetch_and_cache_json
|
12 |
+
import logging
|
13 |
|
14 |
CACHE_DIR = os.getenv("CACHE_DIR")
|
15 |
|
16 |
download_progress = {}
|
17 |
|
18 |
class Instance:
|
19 |
+
def __init__(self, id, url, cache_dir, index_file, token, repo, load_balancer_api,max_retries=5, initial_delay=1):
|
20 |
self.version = "0.2.3 V Alpha"
|
21 |
self.id = id
|
22 |
self.url = url
|
|
|
28 |
self.TV_STORE_JSON_PATH = os.path.join(cache_dir, "tv_store.json")
|
29 |
self.download_threads = {}
|
30 |
self.file_structure = None
|
31 |
+
self.load_balancer_api = load_balancer_api
|
32 |
+
self.max_retries = max_retries
|
33 |
+
self.initial_delay = initial_delay
|
34 |
|
35 |
# Ensure CACHE_DIR exists
|
36 |
if not os.path.exists(self.CACHE_DIR):
|
|
|
53 |
|
54 |
# Start prefetching metadata
|
55 |
thread = Thread(target=self.start_prefetching)
|
56 |
+
self.register_to_load_balancer()
|
57 |
thread.daemon = True
|
58 |
thread.start()
|
59 |
|
|
|
63 |
cache_size = self.get_cache_size()
|
64 |
|
65 |
report = {
|
66 |
+
"instance_id":self.id,
|
67 |
+
"instance_url": self.url,
|
68 |
"film_store": self.read_json(film_store_path),
|
69 |
"tv_store": self.read_json(tv_store_path),
|
70 |
"cache_size": cache_size
|
|
|
385 |
|
386 |
def start_prefetching(self):
|
387 |
"""Start the metadata prefetching in a separate thread."""
|
388 |
+
self.prefetch_metadata()
|
389 |
+
|
390 |
+
|
391 |
+
def register_to_load_balancer(self):
|
392 |
+
retries = 0
|
393 |
+
delay = self.initial_delay
|
394 |
+
|
395 |
+
while retries < self.max_retries:
|
396 |
+
result = self.load_balancer_api.register_instance(self.id, self.url)
|
397 |
+
if result:
|
398 |
+
logging.info(f'Successfully registered instance {self.id} to load balancer.')
|
399 |
+
return result
|
400 |
+
|
401 |
+
retries += 1
|
402 |
+
logging.warning(f'Attempt {retries} to register instance {self.id} failed. Retrying in {delay} seconds...')
|
403 |
+
time.sleep(delay)
|
404 |
+
delay *= 2 # Exponential backoff
|
405 |
+
|
406 |
+
logging.error(f'Failed to register instance {self.id} to load balancer after {self.max_retries} attempts.')
|
407 |
+
return None
|
TODO.md
CHANGED
@@ -1,5 +1,4 @@
|
|
1 |
## TODO
|
2 |
-
* Create Instance class
|
3 |
* add a method to register the instance to Load Balancer on ```[POST]<load_balancer_url>/api/register``` route
|
4 |
* add `[GET] /api/get/report` route to return a report consist of film_store.json, tv_store.json, cache_size
|
5 |
* add two api routes to delete a certain film or episode
|
|
|
1 |
## TODO
|
|
|
2 |
* add a method to register the instance to Load Balancer on ```[POST]<load_balancer_url>/api/register``` route
|
3 |
* add `[GET] /api/get/report` route to return a report consist of film_store.json, tv_store.json, cache_size
|
4 |
* add two api routes to delete a certain film or episode
|
api.py
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import logging
|
3 |
+
|
4 |
+
class LoadBalancerAPI:
|
5 |
+
def __init__(self, base_url):
|
6 |
+
self.base_url = base_url
|
7 |
+
|
8 |
+
def register_instance(self, instance_id, instance_url):
|
9 |
+
data = {
|
10 |
+
"url": instance_url
|
11 |
+
}
|
12 |
+
api_endpoint = f'{self.base_url}/api/register'
|
13 |
+
|
14 |
+
try:
|
15 |
+
response = requests.post(api_endpoint, data=data)
|
16 |
+
response.raise_for_status()
|
17 |
+
return response.json() # Assuming the API returns JSON
|
18 |
+
except requests.exceptions.RequestException as e:
|
19 |
+
logging.error(f'Failed to register instance {instance_id} to load balancer: {e}')
|
20 |
+
return None
|
app.py
CHANGED
@@ -5,6 +5,7 @@ import json
|
|
5 |
from threading import Thread
|
6 |
import urllib.parse
|
7 |
from Instance import Instance
|
|
|
8 |
|
9 |
app = Flask(__name__)
|
10 |
CORS(app)
|
@@ -16,11 +17,12 @@ TOKEN = os.getenv("TOKEN")
|
|
16 |
REPO = os.getenv("REPO")
|
17 |
ID = os.getenv("ID")
|
18 |
URL = os.getenv("URL")
|
|
|
19 |
|
20 |
-
|
|
|
21 |
|
22 |
# API Endpoints
|
23 |
-
|
24 |
@app.route('/api/film/<title>', methods=['GET'])
|
25 |
def get_movie_api(title):
|
26 |
"""Endpoint to get the movie by title."""
|
|
|
5 |
from threading import Thread
|
6 |
import urllib.parse
|
7 |
from Instance import Instance
|
8 |
+
from api import LoadBalancerAPI
|
9 |
|
10 |
app = Flask(__name__)
|
11 |
CORS(app)
|
|
|
17 |
REPO = os.getenv("REPO")
|
18 |
ID = os.getenv("ID")
|
19 |
URL = os.getenv("URL")
|
20 |
+
LOAD_BALANCER_URL = os.getenv("LOAD_BALANCER_URL")
|
21 |
|
22 |
+
load_balancer_api = LoadBalancerAPI(base_url=LOAD_BALANCER_URL)
|
23 |
+
instance = Instance(id=ID, url=URL, cache_dir=CACHE_DIR, index_file=INDEX_FILE, token=TOKEN, repo=REPO, load_balancer_api=load_balancer_api)
|
24 |
|
25 |
# API Endpoints
|
|
|
26 |
@app.route('/api/film/<title>', methods=['GET'])
|
27 |
def get_movie_api(title):
|
28 |
"""Endpoint to get the movie by title."""
|