Skip to content

Commit

Permalink
Refactor image update logic, introduce delay and streamline handling
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick borowy committed Mar 22, 2024
1 parent 58bba70 commit 3719e8c
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,12 +355,17 @@ async def update_containers(docker, image, containers, module_digest_map):
await asyncio.sleep(delay)
await recreate_container(docker, container, module_digest_map, last_pull_times)

async def handle_image_update(image, containers, module_digest_map):
async def handle_image_update(image, module_digest_map):
"""Handles updating of containers for a specific image."""
nonlocal images_to_update
async with Docker() as docker:
await asyncio.sleep(5)
containers = images_to_update[image]
pulled = await pull_image_if_needed(docker, image)
if pulled:
await update_containers(docker, image, containers, module_digest_map)
await update_containers(
docker, image, containers, module_digest_map
)

async def schedule_update(container, image: str, module_digest_map):
"""
Expand All @@ -375,11 +380,13 @@ async def schedule_update(container, image: str, module_digest_map):

# Check if this is the first container for the image to schedule the task
if len(images_to_update[image]) == 1:
asyncio.create_task(handle_image_update(image, images_to_update[image], module_digest_map))
asyncio.create_task(
handle_image_update(
image, module_digest_map
)
)

return schedule_update

# Example usage:
schedule_update = build_update_function(
delay=5, validity_threshold_seconds=30
)
Expand All @@ -390,7 +397,6 @@ def build_updater():
module_digest_map = json.loads(preloaded_module_digest_map)
else:
module_digest_map = {}
logging.info(f"MODULE_DIGEST_MAP IS {json.dumps(module_digest_map, indent=4)}")
async def enforce_versioning(client):
logging.info("Enforcing versioning")
nonlocal module_digest_map
Expand All @@ -407,6 +413,7 @@ async def enforce_versioning(client):
if current_digest is None or current_digest != latest_digest:
logging.info(f"Updating module_digest_map: {module_digest_map}")
module_digest_map[img] = latest_digest

logging.info(f"Scheduling an update for {img}")
await schedule_update(container, img, module_digest_map)

Expand Down

0 comments on commit 3719e8c

Please sign in to comment.