ChandimaPrabath commited on
Commit
aa7ee0b
·
1 Parent(s): 83371d8
Files changed (1) hide show
  1. LoadBalancer.py +29 -8
LoadBalancer.py CHANGED
@@ -119,34 +119,55 @@ class LoadBalancer:
119
  logging.info(f"Updated instance {instance} with cache size {cache_size}")
120
 
121
  def download_music_to_best_instance(self, file_name):
122
- """Downloads a music file to the first instance that has more free space on the self.instance_health list variable."""
123
  best_instance = None
124
  max_free_space = -1
125
 
 
126
  for instance_url, space_info in self.instances_health.items():
127
- total_space = convert_to_gb(space_info['total'])
128
- used_space = convert_to_gb(space_info['used'])
129
  free_space = total_space - used_space
130
 
131
  if free_space > max_free_space:
132
  max_free_space = free_space
133
  best_instance = instance_url
134
 
135
- if best_instance:
 
 
 
 
 
136
  result = self.instances_api.download_music(best_instance, file_name)
 
 
 
 
 
 
 
 
 
 
137
  music_id = result["music_id"]
138
  status = result["status"]
139
  progress_url = f'{best_instance}/api/get/progress/{music_id}'
 
140
  response = {
141
  "music_id": music_id,
142
  "status": status,
143
  "progress_url": progress_url
144
  }
145
-
146
  return response
147
- else:
148
- logging.error("No suitable instance found for downloading the music.")
149
- return {"error": "No suitable instance found for downloading the music."}
 
 
 
 
 
150
 
151
  def find_music_path(self, title):
152
  """Find the path of the music in the indexed data based on the title."""
 
119
  logging.info(f"Updated instance {instance} with cache size {cache_size}")
120
 
121
  def download_music_to_best_instance(self, file_name):
122
+ """Downloads a music file to the instance with the most free space in self.instance_health."""
123
  best_instance = None
124
  max_free_space = -1
125
 
126
+ # Determine the instance with the most free space
127
  for instance_url, space_info in self.instances_health.items():
128
+ total_space = convert_to_gb(space_info.get('total', 0))
129
+ used_space = convert_to_gb(space_info.get('used', 0))
130
  free_space = total_space - used_space
131
 
132
  if free_space > max_free_space:
133
  max_free_space = free_space
134
  best_instance = instance_url
135
 
136
+ if not best_instance:
137
+ logging.error("No suitable instance found for downloading the music.")
138
+ return {"error": "No suitable instance found for downloading the music."}
139
+
140
+ # Attempt to download music to the best instance
141
+ try:
142
  result = self.instances_api.download_music(best_instance, file_name)
143
+
144
+ # Check if the response is as expected
145
+ if not result or "music_id" not in result or "status" not in result:
146
+ logging.error(f"Unexpected response from instance {best_instance}: {result}")
147
+ return {
148
+ "error": "Failed to retrieve valid download data from the instance.",
149
+ "details": result if result else "Empty response"
150
+ }
151
+
152
+ # Prepare response with download progress URL
153
  music_id = result["music_id"]
154
  status = result["status"]
155
  progress_url = f'{best_instance}/api/get/progress/{music_id}'
156
+
157
  response = {
158
  "music_id": music_id,
159
  "status": status,
160
  "progress_url": progress_url
161
  }
 
162
  return response
163
+
164
+ except Exception as e:
165
+ # Log network or API call-related errors
166
+ logging.error(f"Error downloading music to {best_instance}: {str(e)}")
167
+ return {
168
+ "error": "Error occurred during music download.",
169
+ "details": str(e)
170
+ }
171
 
172
  def find_music_path(self, title):
173
  """Find the path of the music in the indexed data based on the title."""