backend/__init__ eagerly imports all backends; DRY the docker subprocess helpers #359
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Two related cleanups surfaced in review of #358.
1.
backend/__init__.pyeagerly imports all three backendsbot_bottle/backend/__init__.pyimportsDockerBottleBackend,FirecrackerBottleBackend, andMacosContainerBottleBackendat module top (bottom of the file,# noqa: E402). So importing any backend submodule — even the leanbackend.docker.util(akadocker_mod, only imports...log) — runs the package__init__and drags in the whole backend layer.Measured:
from bot_bottle.backend.docker import utilpulls in ~76bot_bottlemodules, including the entire firecracker and macos backends plus the manifest/egress/git-gate/supervise/workspace framework.Fix: lazy-load the backends — import them inside the
get_bottle_backendfactory (and/or expose them via module-level__getattr__/ PEP 562) instead of at import time. Then importing a backend util is cheap, andfrom bot_bottle.backend import DockerBottleBackendstill works but only loads on access.This is why the orchestrator's
run_dockerwas placed in a lean top-levelbot_bottle/docker_cmd.pyrather than reusingdocker_mod— routing the orchestrator throughdocker_modtoday would make every orchestrator import load all three backends.2. DRY the docker subprocess helpers onto
docker_cmdOnce (1) makes backend utils cheap to import, thread
backend/docker/util.py's ad-hocsubprocess.run(["docker", ...])calls (and_silent_run,force_remove_container,container_exists, ...) through the sharedbot_bottle/docker_cmd.run_docker, so there's one docker-subprocess primitive shared by the backend and the orchestrator.Ordering: (1) then (2). Neither blocks the orchestrator stack (#352/#356/#357/#358).