Skip to content

Commit

Permalink
DockerImagesCleaner constants and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
TheoLisin committed Nov 28, 2023
1 parent 55fb59f commit 91c9d52
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
6 changes: 4 additions & 2 deletions agent/worker/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import supervisely_lib as sly

from worker.agent_utils import TaskDirCleaner, AppDirCleaner
from worker.agent_utils import TaskDirCleaner, AppDirCleaner, DockerImagesCleaner
from worker.task_update import check_and_pull_sly_net_if_needed

warnings.filterwarnings(action="ignore", category=UserWarning)
Expand Down Expand Up @@ -532,6 +532,8 @@ def terminate_all_deamons():
def task_clear_old_data(self):
day = 60 * 60 * 24
cleaner = AppDirCleaner(self.logger)
image_cleaner = DockerImagesCleaner(self.docker_api, self.logger)

while True:
with self.task_pool_lock:
all_tasks = set(self.task_pool.keys())
Expand All @@ -542,5 +544,5 @@ def task_clear_old_data(self):
self.logger.exception(e)
# raise or not?
# raise e

image_cleaner.remove_idle_images()
time.sleep(day)
14 changes: 5 additions & 9 deletions agent/worker/agent_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ def remove_idle_images(self):
all_hists = sly.fs.list_files(self.path_to_history, filter_fn=self._is_history)
lock_file = os.path.join(self.path_to_history, "docker-images-lock.txt")

if lock_file in all_hists:
if sly.fs.file_exists(lock_file):
self.logger.info(
"Skip DockerImagesCleaner task: another agent is already working on this task"
)
Expand All @@ -329,7 +329,7 @@ def remove_idle_images(self):

try:
# check all
to_remove = self._parse_all_hists(all_hists, lock_file)
to_remove = self._parse_all_hists(all_hists)
for image in to_remove:
try:
self.docker_api.api.remove_image(image)
Expand All @@ -340,16 +340,12 @@ def remove_idle_images(self):
finally:
sly.fs.silent_remove(lock_file)

def _is_history(filename: str) -> bool:
is_hist = filename.startswith("docker-images-history-")
is_lock = filename.startswith("docker-images-lock")
return is_hist or is_lock
def _is_history(self, filename: str) -> bool:
return "docker-images-history-" in filename

def _parse_all_hists(self, hist_paths: List[str], lock_file: str) -> List[str]:
def _parse_all_hists(self, hist_paths: List[str]) -> List[str]:
to_remove = {}
for hist in hist_paths:
if hist == lock_file:
continue
to_remove = self._parse_and_update_history(hist, to_remove)
return list(to_remove.keys())

Expand Down
5 changes: 5 additions & 0 deletions agent/worker/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
_SUPERVISELY_AGENT_FILES = "SUPERVISELY_AGENT_FILES"
_SUPERVISELY_AGENT_FILES_CONTAINER = "SUPERVISELY_AGENT_FILES_CONTAINER"
_OFFLINE_MODE = "OFFLINE_MODE"
_REMOVE_IDLE_DOCKER_IMAGE_AFTER_X_DAYS = "REMOVE_IDLE_DOCKER_IMAGE_AFTER_X_DAYS"


_OPTIONAL_DEFAULTS = {
Expand Down Expand Up @@ -115,6 +116,7 @@
_AUTO_CLEAN_INT_RANGE_DAYS: 7,
_REQUESTS_CA_BUNDLE_DIR_CONTAINER: "/sly_certs",
_SECURITY_OPT: None,
_REMOVE_IDLE_DOCKER_IMAGE_AFTER_X_DAYS: 14,
}


Expand Down Expand Up @@ -465,6 +467,9 @@ def DISABLE_TELEMETRY():
return read_optional_setting(_DISABLE_TELEMETRY)


def REMOVE_IDLE_DOCKER_IMAGE_AFTER_X_DAYS():
return read_optional_setting(_REMOVE_IDLE_DOCKER_IMAGE_AFTER_X_DAYS)

def AGENT_ID():
try:
host_dir = SUPERVISELY_AGENT_FILES()
Expand Down

0 comments on commit 91c9d52

Please sign in to comment.