Spaces:
Build error
Build error
Commit ·
b844929
1
Parent(s): 4a5b11a
try fix
Browse files- LoadBalancer.py +44 -46
LoadBalancer.py
CHANGED
|
@@ -37,30 +37,41 @@ class LoadBalancer:
|
|
| 37 |
self.file_structure = None
|
| 38 |
|
| 39 |
# Ensure CACHE_DIR exists
|
| 40 |
-
|
| 41 |
-
os.makedirs(self.CACHE_DIR)
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
json.dump({}, json_file)
|
| 47 |
|
| 48 |
# Index the file structure
|
| 49 |
indexer()
|
| 50 |
|
| 51 |
# Load the file structure JSON
|
| 52 |
-
|
| 53 |
-
raise FileNotFoundError(f"{self.INDEX_FILE} not found. Please make sure the file exists.")
|
| 54 |
-
|
| 55 |
-
with open(self.INDEX_FILE, 'r') as f:
|
| 56 |
-
self.file_structure = json.load(f)
|
| 57 |
|
|
|
|
| 58 |
prefetch_thread = Thread(target=self.start_prefetching)
|
| 59 |
prefetch_thread.daemon = True
|
| 60 |
prefetch_thread.start()
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
def register_instance(self, instance_url):
|
|
|
|
| 64 |
if instance_url not in self.instances:
|
| 65 |
self.instances.append(instance_url)
|
| 66 |
logging.info(f"Registered instance {instance_url}")
|
|
@@ -68,6 +79,7 @@ class LoadBalancer:
|
|
| 68 |
logging.info(f"Instance {instance_url} is already registered.")
|
| 69 |
|
| 70 |
def remove_instance(self, instance_url):
|
|
|
|
| 71 |
if instance_url in self.instances:
|
| 72 |
self.instances.remove(instance_url)
|
| 73 |
self.instances_health.pop(instance_url, None)
|
|
@@ -78,13 +90,14 @@ class LoadBalancer:
|
|
| 78 |
logging.info(f"Instance {instance_url} not found for removal.")
|
| 79 |
|
| 80 |
def get_reports(self):
|
|
|
|
| 81 |
reports = self.instances_api.fetch_reports()
|
| 82 |
temp_film_store_path = 'temp_film_store.json'
|
| 83 |
temp_tv_store_path = 'temp_tv_store.json'
|
| 84 |
|
| 85 |
-
#
|
| 86 |
-
|
| 87 |
-
|
| 88 |
|
| 89 |
for instance_url in self.instances[:]:
|
| 90 |
if instance_url in reports:
|
|
@@ -100,6 +113,7 @@ class LoadBalancer:
|
|
| 100 |
self.replace_json_files(temp_tv_store_path, self.TV_STORE_JSON_PATH)
|
| 101 |
|
| 102 |
def process_report(self, instance_url, report, temp_film_store_path, temp_tv_store_path):
|
|
|
|
| 103 |
film_store = report.get('film_store', {})
|
| 104 |
tv_store = report.get('tv_store', {})
|
| 105 |
cache_size = report.get('cache_size')
|
|
@@ -122,14 +136,7 @@ class LoadBalancer:
|
|
| 122 |
self.update_instances_health(instance=instance_url, cache_size=cache_size)
|
| 123 |
|
| 124 |
def update_film_store_json(self, title, url, temp_film_store_path):
|
| 125 |
-
"""
|
| 126 |
-
Updates the temporary film store JSON with the new file.
|
| 127 |
-
|
| 128 |
-
Args:
|
| 129 |
-
title (str): The title of the film.
|
| 130 |
-
url (str): The url.
|
| 131 |
-
temp_film_store_path (str): Path to the temporary film store JSON.
|
| 132 |
-
"""
|
| 133 |
film_store_data = {}
|
| 134 |
if os.path.exists(temp_film_store_path):
|
| 135 |
with open(temp_film_store_path, 'r') as json_file:
|
|
@@ -140,19 +147,10 @@ class LoadBalancer:
|
|
| 140 |
with open(temp_film_store_path, 'w') as json_file:
|
| 141 |
json.dump(film_store_data, json_file, indent=2)
|
| 142 |
|
| 143 |
-
|
| 144 |
|
| 145 |
def update_tv_store_json(self, title, season, episode, url, temp_tv_store_path):
|
| 146 |
-
"""
|
| 147 |
-
Updates the temporary TV store JSON with the new file, organizing by title, season, and episode.
|
| 148 |
-
|
| 149 |
-
Args:
|
| 150 |
-
title (str): The title of the TV show.
|
| 151 |
-
season (str): The season of the TV show.
|
| 152 |
-
episode (str): The episode of the TV show.
|
| 153 |
-
url (str): The url.
|
| 154 |
-
temp_tv_store_path (str): Path to the temporary TV store JSON.
|
| 155 |
-
"""
|
| 156 |
tv_store_data = {}
|
| 157 |
if os.path.exists(temp_tv_store_path):
|
| 158 |
with open(temp_tv_store_path, 'r') as json_file:
|
|
@@ -169,23 +167,22 @@ class LoadBalancer:
|
|
| 169 |
with open(temp_tv_store_path, 'w') as json_file:
|
| 170 |
json.dump(tv_store_data, json_file, indent=2)
|
| 171 |
|
| 172 |
-
|
| 173 |
|
| 174 |
def replace_json_files(self, temp_file_path, actual_file_path):
|
| 175 |
-
"""
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
print(f'Updated {actual_file_path} with new data.')
|
| 186 |
-
|
| 187 |
|
| 188 |
def start_polling(self):
|
|
|
|
| 189 |
logging.info("Starting polling.")
|
| 190 |
while not self.stop_event.is_set():
|
| 191 |
self.get_reports()
|
|
@@ -193,6 +190,7 @@ class LoadBalancer:
|
|
| 193 |
logging.info("Polling stopped.")
|
| 194 |
|
| 195 |
def stop_polling(self):
|
|
|
|
| 196 |
logging.info("Stopping polling.")
|
| 197 |
self.stop_event.set()
|
| 198 |
|
|
|
|
| 37 |
self.file_structure = None
|
| 38 |
|
| 39 |
# Ensure CACHE_DIR exists
|
| 40 |
+
os.makedirs(self.CACHE_DIR, exist_ok=True)
|
|
|
|
| 41 |
|
| 42 |
+
# Initialize JSON files
|
| 43 |
+
self.initialize_json_file(self.FILM_STORE_JSON_PATH)
|
| 44 |
+
self.initialize_json_file(self.TV_STORE_JSON_PATH)
|
|
|
|
| 45 |
|
| 46 |
# Index the file structure
|
| 47 |
indexer()
|
| 48 |
|
| 49 |
# Load the file structure JSON
|
| 50 |
+
self.load_file_structure()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
+
# Start prefetching
|
| 53 |
prefetch_thread = Thread(target=self.start_prefetching)
|
| 54 |
prefetch_thread.daemon = True
|
| 55 |
prefetch_thread.start()
|
| 56 |
|
| 57 |
+
def initialize_json_file(self, path):
|
| 58 |
+
"""Initialize a JSON file with an empty object if it does not exist."""
|
| 59 |
+
if not os.path.exists(path):
|
| 60 |
+
with open(path, 'w') as json_file:
|
| 61 |
+
json.dump({}, json_file)
|
| 62 |
+
logging.info(f"Created JSON file: {path}")
|
| 63 |
+
|
| 64 |
+
def load_file_structure(self):
|
| 65 |
+
"""Load the file structure from the index file."""
|
| 66 |
+
if not os.path.exists(self.INDEX_FILE):
|
| 67 |
+
raise FileNotFoundError(f"{self.INDEX_FILE} not found. Please make sure the file exists.")
|
| 68 |
+
|
| 69 |
+
with open(self.INDEX_FILE, 'r') as f:
|
| 70 |
+
self.file_structure = json.load(f)
|
| 71 |
+
logging.info(f"Loaded file structure from {self.INDEX_FILE}")
|
| 72 |
|
| 73 |
def register_instance(self, instance_url):
|
| 74 |
+
"""Register a new instance."""
|
| 75 |
if instance_url not in self.instances:
|
| 76 |
self.instances.append(instance_url)
|
| 77 |
logging.info(f"Registered instance {instance_url}")
|
|
|
|
| 79 |
logging.info(f"Instance {instance_url} is already registered.")
|
| 80 |
|
| 81 |
def remove_instance(self, instance_url):
|
| 82 |
+
"""Remove an instance."""
|
| 83 |
if instance_url in self.instances:
|
| 84 |
self.instances.remove(instance_url)
|
| 85 |
self.instances_health.pop(instance_url, None)
|
|
|
|
| 90 |
logging.info(f"Instance {instance_url} not found for removal.")
|
| 91 |
|
| 92 |
def get_reports(self):
|
| 93 |
+
"""Fetch reports from instances and update JSON files."""
|
| 94 |
reports = self.instances_api.fetch_reports()
|
| 95 |
temp_film_store_path = 'temp_film_store.json'
|
| 96 |
temp_tv_store_path = 'temp_tv_store.json'
|
| 97 |
|
| 98 |
+
# Initialize temporary JSON files
|
| 99 |
+
self.initialize_json_file(temp_film_store_path)
|
| 100 |
+
self.initialize_json_file(temp_tv_store_path)
|
| 101 |
|
| 102 |
for instance_url in self.instances[:]:
|
| 103 |
if instance_url in reports:
|
|
|
|
| 113 |
self.replace_json_files(temp_tv_store_path, self.TV_STORE_JSON_PATH)
|
| 114 |
|
| 115 |
def process_report(self, instance_url, report, temp_film_store_path, temp_tv_store_path):
|
| 116 |
+
"""Process the report from an instance and update temporary JSON files."""
|
| 117 |
film_store = report.get('film_store', {})
|
| 118 |
tv_store = report.get('tv_store', {})
|
| 119 |
cache_size = report.get('cache_size')
|
|
|
|
| 136 |
self.update_instances_health(instance=instance_url, cache_size=cache_size)
|
| 137 |
|
| 138 |
def update_film_store_json(self, title, url, temp_film_store_path):
|
| 139 |
+
"""Update the temporary film store JSON with the new file."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
film_store_data = {}
|
| 141 |
if os.path.exists(temp_film_store_path):
|
| 142 |
with open(temp_film_store_path, 'r') as json_file:
|
|
|
|
| 147 |
with open(temp_film_store_path, 'w') as json_file:
|
| 148 |
json.dump(film_store_data, json_file, indent=2)
|
| 149 |
|
| 150 |
+
logging.info(f'Film store updated with {title}. Content: {film_store_data}')
|
| 151 |
|
| 152 |
def update_tv_store_json(self, title, season, episode, url, temp_tv_store_path):
|
| 153 |
+
"""Update the temporary TV store JSON with the new file."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
tv_store_data = {}
|
| 155 |
if os.path.exists(temp_tv_store_path):
|
| 156 |
with open(temp_tv_store_path, 'r') as json_file:
|
|
|
|
| 167 |
with open(temp_tv_store_path, 'w') as json_file:
|
| 168 |
json.dump(tv_store_data, json_file, indent=2)
|
| 169 |
|
| 170 |
+
logging.info(f'TV store updated with {title}, {season}, {episode}. Content: {tv_store_data}')
|
| 171 |
|
| 172 |
def replace_json_files(self, temp_file_path, actual_file_path):
|
| 173 |
+
"""Replace the actual JSON file with the temporary JSON file."""
|
| 174 |
+
try:
|
| 175 |
+
if os.path.exists(temp_file_path):
|
| 176 |
+
shutil.copy(temp_file_path, actual_file_path)
|
| 177 |
+
os.remove(temp_file_path)
|
| 178 |
+
logging.info(f'Updated {actual_file_path} with new data.')
|
| 179 |
+
else:
|
| 180 |
+
logging.error(f'Temporary file {temp_file_path} does not exist.')
|
| 181 |
+
except Exception as e:
|
| 182 |
+
logging.error(f'Error replacing JSON files: {e}')
|
|
|
|
|
|
|
| 183 |
|
| 184 |
def start_polling(self):
|
| 185 |
+
"""Start polling for reports."""
|
| 186 |
logging.info("Starting polling.")
|
| 187 |
while not self.stop_event.is_set():
|
| 188 |
self.get_reports()
|
|
|
|
| 190 |
logging.info("Polling stopped.")
|
| 191 |
|
| 192 |
def stop_polling(self):
|
| 193 |
+
"""Stop polling."""
|
| 194 |
logging.info("Stopping polling.")
|
| 195 |
self.stop_event.set()
|
| 196 |
|