Skip to content

Commit

Permalink
update backend to use absolute paths and not relative
Browse files Browse the repository at this point in the history
  • Loading branch information
SinaKhalili committed Oct 17, 2024
1 parent f9cae30 commit b2ce677
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 18 deletions.
28 changes: 23 additions & 5 deletions backend/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,29 @@ async def repeatedly_retake_snapshot(state: BackendState) -> None:
await state.take_pickle_snapshot()


@repeat_every(seconds=60 * 8, wait_first=True)
async def repeatedly_clean_cache(state: BackendState) -> None:
def clean_cache(state: BackendState) -> None:
if not os.path.exists("pickles"):
print("pickles folder does not exist")
return

pickles = glob.glob("pickles/*")
if len(pickles) > 3:
print("pickles folder has more than 3 pickles, deleting old ones")

# check for pickle folders with less than 4 files (error in write)
incomplete_pickles = []
for pickle in pickles:
if len(glob.glob(f"{pickle}/*")) < 4:
incomplete_pickles.append(pickle)

for incomplete_pickle in incomplete_pickles:
print(f"deleting {incomplete_pickle}")
shutil.rmtree(incomplete_pickle)

pickles = glob.glob("pickles/*")

if len(pickles) > 5:
print("pickles folder has more than 5 pickles, deleting old ones")
pickles.sort(key=os.path.getmtime)
for pickle in pickles[:-3]:
for pickle in pickles[:-5]:
print(f"deleting {pickle}")
shutil.rmtree(pickle)

Expand All @@ -51,6 +63,11 @@ async def repeatedly_clean_cache(state: BackendState) -> None:
os.remove(cache_file)


@repeat_every(seconds=60 * 8, wait_first=True)
async def repeatedly_clean_cache(state: BackendState) -> None:
clean_cache(state)


@asynccontextmanager
async def lifespan(app: FastAPI):
url = os.getenv("RPC_URL")
Expand All @@ -60,6 +77,7 @@ async def lifespan(app: FastAPI):
state.initialize(url)

print("Checking if cached vat exists")
clean_cache(state)
cached_vat_path = sorted(glob.glob("pickles/*"))
if len(cached_vat_path) > 0:
print("Loading cached vat")
Expand Down
23 changes: 11 additions & 12 deletions backend/middleware/cache_middleware.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import glob
import hashlib
import os
import pickle
Expand Down Expand Up @@ -32,7 +33,7 @@ async def dispatch(self, request: BackendRequest, call_next: Callable):
return await call_next(request)

current_pickle = self.state.current_pickle_path
previous_pickle = self._get_previous_pickle(current_pickle)
previous_pickle = self._get_previous_pickle()

# Try to serve data from the current (latest) pickle first
current_cache_key = self._generate_cache_key(request, current_pickle)
Expand Down Expand Up @@ -143,18 +144,16 @@ def _generate_cache_key(self, request: BackendRequest, pickle_path: str) -> str:
print("Hash input: ", hash_input)
return hashlib.md5(hash_input.encode()).hexdigest()

def _get_previous_pickle(self, current_pickle: str) -> Optional[str]:
def _get_previous_pickle(self) -> Optional[str]:
print("Attempting previous pickle")
pickle_dir = os.path.dirname(current_pickle)
pickles = sorted(
[f for f in os.listdir(pickle_dir)],
key=lambda x: os.path.getmtime(os.path.join(pickle_dir, x)),
reverse=True,
)

if len(pickles) > 1:
print("Previous pickle: ", os.path.join(pickle_dir, pickles[1]))
return os.path.join(pickle_dir, pickles[1])
_pickle_paths = glob.glob(f"{self.state.current_pickle_path}/../*")
pickle_paths = sorted([os.path.realpath(dir) for dir in _pickle_paths])
print("Pickle paths: ", pickle_paths)

if len(pickle_paths) > 1:
previous_pickle_path = pickle_paths[-2]
print("Previous pickle: ", previous_pickle_path)
return previous_pickle_path

print("No previous pickle found")
return None
2 changes: 1 addition & 1 deletion backend/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ async def take_pickle_snapshot(self):

async def load_pickle_snapshot(self, directory: str):
pickle_map = load_newest_files(directory)
self.current_pickle_path = directory
self.current_pickle_path = os.path.realpath(directory)
with waiting_for("unpickling"):
await self.vat.unpickle(
users_filename=pickle_map["usermap"],
Expand Down

0 comments on commit b2ce677

Please sign in to comment.