Spaces:
Paused
Paused
Commit
·
d7c60b2
1
Parent(s):
e262a9b
0.2.6 V Alpha
Browse files- Instance.py +25 -11
Instance.py
CHANGED
|
@@ -3,12 +3,11 @@ import requests
|
|
| 3 |
import json
|
| 4 |
import urllib.request
|
| 5 |
import time
|
| 6 |
-
from threading import Thread
|
| 7 |
from requests.exceptions import RequestException
|
| 8 |
from tqdm import tqdm
|
| 9 |
from indexer import indexer
|
| 10 |
import logging
|
| 11 |
-
from threading import Event
|
| 12 |
|
| 13 |
CACHE_DIR = os.getenv("CACHE_DIR")
|
| 14 |
|
|
@@ -16,7 +15,7 @@ download_progress = {}
|
|
| 16 |
|
| 17 |
class Instance:
|
| 18 |
def __init__(self, id, url, cache_dir, index_file, token, repo, load_balancer_api, max_retries=20, initial_delay=1):
|
| 19 |
-
self.version = "0.2.
|
| 20 |
self.id = id
|
| 21 |
self.url = url
|
| 22 |
self.CACHE_DIR = cache_dir
|
|
@@ -42,21 +41,36 @@ class Instance:
|
|
| 42 |
with open(path, 'w') as json_file:
|
| 43 |
json.dump({}, json_file)
|
| 44 |
|
| 45 |
-
# Index the file structure
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
|
|
|
|
|
|
|
| 49 |
if not os.path.exists(self.INDEX_FILE):
|
| 50 |
raise FileNotFoundError(f"{self.INDEX_FILE} not found. Please make sure the file exists.")
|
| 51 |
|
| 52 |
with open(self.INDEX_FILE, 'r') as f:
|
| 53 |
self.file_structure = json.load(f)
|
|
|
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
| 60 |
|
| 61 |
def compile_report(self):
|
| 62 |
self.last_report_time = time.time() # Update the last report time
|
|
|
|
| 3 |
import json
|
| 4 |
import urllib.request
|
| 5 |
import time
|
| 6 |
+
from threading import Thread, Event
|
| 7 |
from requests.exceptions import RequestException
|
| 8 |
from tqdm import tqdm
|
| 9 |
from indexer import indexer
|
| 10 |
import logging
|
|
|
|
| 11 |
|
| 12 |
CACHE_DIR = os.getenv("CACHE_DIR")
|
| 13 |
|
|
|
|
| 15 |
|
| 16 |
class Instance:
|
| 17 |
def __init__(self, id, url, cache_dir, index_file, token, repo, load_balancer_api, max_retries=20, initial_delay=1):
|
| 18 |
+
self.version = "0.2.6 V Alpha"
|
| 19 |
self.id = id
|
| 20 |
self.url = url
|
| 21 |
self.CACHE_DIR = cache_dir
|
|
|
|
| 41 |
with open(path, 'w') as json_file:
|
| 42 |
json.dump({}, json_file)
|
| 43 |
|
| 44 |
+
# Index the file structure and load it
|
| 45 |
+
self.run_indexer_and_load()
|
| 46 |
+
|
| 47 |
+
# Start prefetching metadata and monitoring registration
|
| 48 |
+
self.register_to_load_balancer()
|
| 49 |
+
registration_thread = Thread(target=self.monitor_registration)
|
| 50 |
+
registration_thread.daemon = True
|
| 51 |
+
registration_thread.start()
|
| 52 |
+
|
| 53 |
+
# Start the thread to re-index every 2 minutes
|
| 54 |
+
indexer_thread = Thread(target=self.run_indexer_periodically)
|
| 55 |
+
indexer_thread.daemon = True
|
| 56 |
+
indexer_thread.start()
|
| 57 |
|
| 58 |
+
def run_indexer_and_load(self):
|
| 59 |
+
"""Runs the indexer and loads the file structure from INDEX_FILE."""
|
| 60 |
+
indexer()
|
| 61 |
if not os.path.exists(self.INDEX_FILE):
|
| 62 |
raise FileNotFoundError(f"{self.INDEX_FILE} not found. Please make sure the file exists.")
|
| 63 |
|
| 64 |
with open(self.INDEX_FILE, 'r') as f:
|
| 65 |
self.file_structure = json.load(f)
|
| 66 |
+
logging.info("File structure reloaded successfully.")
|
| 67 |
|
| 68 |
+
def run_indexer_periodically(self):
|
| 69 |
+
"""Periodically reruns the indexer and reloads the file structure."""
|
| 70 |
+
while True:
|
| 71 |
+
time.sleep(120) # Wait for 2 minutes
|
| 72 |
+
logging.info("Re-running indexer and reloading file structure.")
|
| 73 |
+
self.run_indexer_and_load()
|
| 74 |
|
| 75 |
def compile_report(self):
|
| 76 |
self.last_report_time = time.time() # Update the last report time
|