Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions docker/server.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
from aiohttp import web
from cellpack.autopack import get_local_file_location
from cellpack.autopack.DBRecipeHandler import DataDoc, DBUploader
from cellpack.autopack.interface_objects.database_ids import DATABASE_IDS
from cellpack.bin.pack import pack
Expand All @@ -25,6 +26,38 @@ def job_exists(self, dedup_hash):
job_status, _ = db.get_doc_by_id("job_status", dedup_hash)
return job_status is not None

def prefetch_example_grid_files(self):
"""
Download grid files for all example cellpack client recipes into the local
cache so they are ready before the server starts accepting packing requests
"""
db = self._get_firebase_handler()
if not db:
return

recipe_docs = db.get_all_docs("example_packings")
if not recipe_docs:
return

for doc in recipe_docs:
recipe_data = doc.to_dict()
recipe_id = recipe_data.get("recipe")
if not recipe_id:
continue

recipe_doc, _ = db.get_doc_by_id("recipes", recipe_id)
if not recipe_doc:
continue

grid_file_path = recipe_doc.get("grid_file_path")
if grid_file_path:
try:
# Downloads grid file to the local cache if its not already present
get_local_file_location(grid_file_path, cache="grids")
except Exception as e:
# Don't let a failed prefetch prevent the server from starting
print(f"Failed to prefetch grid file '{grid_file_path}': {e}")

async def run_packing(self, dedup_hash, recipe, config=None):
self.update_job_status(dedup_hash, "RUNNING")
try:
Expand Down Expand Up @@ -84,6 +117,7 @@ async def pack_handler(self, request: web.Request) -> web.Response:
async def init_app() -> web.Application:
app = web.Application()
server = CellpackServer()
server.prefetch_example_grid_files()
app.add_routes(
[
web.get("/hello", server.hello_world),
Expand Down
Loading