ChandimaPrabath commited on
Commit
b8db82b
·
1 Parent(s): 1787c80

0.0.2 Alpha test

Browse files
Files changed (1) hide show
  1. LoadBalancer.py +25 -6
LoadBalancer.py CHANGED
@@ -11,7 +11,7 @@ CACHE_DIR = os.getenv("CACHE_DIR")
11
 
12
  class LoadBalancer:
13
  def __init__(self, cache_dir, token, repo, polling_interval=4, max_retries=3, initial_delay=1):
14
- self.version = "0.0.1 Alpha"
15
  self.instances = []
16
  self.instances_health = {}
17
  self.polling_interval = polling_interval
@@ -24,13 +24,14 @@ class LoadBalancer:
24
  self.REPO = repo
25
  self.MUSIC_STORE = {}
26
  self.file_structure = None
 
27
 
28
  # Ensure CACHE_DIR exists
29
  if not os.path.exists(self.CACHE_DIR):
30
  os.makedirs(self.CACHE_DIR)
31
 
32
  # Initialize file structure and start prefetching
33
- self.file_structure = indexer()
34
 
35
  # Start polling and file checking in separate threads
36
  polling_thread = Thread(target=self.start_polling)
@@ -40,10 +41,22 @@ class LoadBalancer:
40
  # Start periodic tasks
41
  asyncio.create_task(self.run_periodic_tasks())
42
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  async def run_periodic_tasks(self):
44
  """Run indexer and prefetch functions every 5 minutes."""
45
  while not self.stop_event.is_set():
46
- self.file_structure = indexer() # Re-run indexer
47
  await asyncio.sleep(300) # Sleep for 5 minutes
48
 
49
  def get_reports(self):
@@ -106,9 +119,7 @@ class LoadBalancer:
106
  logging.info(f"Updated instance {instance} with cache size {cache_size}")
107
 
108
  def download_music_to_best_instance(self, file_name):
109
- """
110
- Downloads a music file to the first instance that has more free space on the self.instance_health list variable.
111
- """
112
  best_instance = None
113
  max_free_space = -1
114
 
@@ -159,3 +170,11 @@ class LoadBalancer:
159
  if sub_directory['type'] == 'file':
160
  music_files.append(sub_directory['path'])
161
  return music_files
 
 
 
 
 
 
 
 
 
11
 
12
  class LoadBalancer:
13
  def __init__(self, cache_dir, token, repo, polling_interval=4, max_retries=3, initial_delay=1):
14
+ self.version = "0.0.2 Alpha"
15
  self.instances = []
16
  self.instances_health = {}
17
  self.polling_interval = polling_interval
 
24
  self.REPO = repo
25
  self.MUSIC_STORE = {}
26
  self.file_structure = None
27
+ self.category_files_map = {}
28
 
29
  # Ensure CACHE_DIR exists
30
  if not os.path.exists(self.CACHE_DIR):
31
  os.makedirs(self.CACHE_DIR)
32
 
33
  # Initialize file structure and start prefetching
34
+ self.update_file_structure()
35
 
36
  # Start polling and file checking in separate threads
37
  polling_thread = Thread(target=self.start_polling)
 
41
  # Start periodic tasks
42
  asyncio.create_task(self.run_periodic_tasks())
43
 
44
+ def update_file_structure(self):
45
+ """Update the file structure and the category-files map."""
46
+ self.file_structure = indexer() # Assume this re-fetches the file structure
47
+ self.category_files_map = {} # Reset the map
48
+
49
+ for directory in self.file_structure:
50
+ if directory['type'] == 'directory':
51
+ # Map category to its files
52
+ self.category_files_map[directory['path']] = [
53
+ file['path'] for file in directory['contents'] if file['type'] == 'file'
54
+ ]
55
+
56
  async def run_periodic_tasks(self):
57
  """Run indexer and prefetch functions every 5 minutes."""
58
  while not self.stop_event.is_set():
59
+ self.update_file_structure() # Re-run indexer and update the map
60
  await asyncio.sleep(300) # Sleep for 5 minutes
61
 
62
  def get_reports(self):
 
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
 
 
170
  if sub_directory['type'] == 'file':
171
  music_files.append(sub_directory['path'])
172
  return music_files
173
+
174
+ def get_all_categories(self):
175
+ """Get a list of all category folders."""
176
+ return list(self.category_files_map.keys())
177
+
178
+ def get_files_from_category(self, category):
179
+ """Get all files from a specified category."""
180
+ return self.category_files_map.get(category, [])