Skip to content

Commit

Permalink
Clear agent-files on clear apps data task (#94)
Browse files Browse the repository at this point in the history
* clear agent-files on clear apps data task

* change the directory

* check that directory exist before deletion
  • Loading branch information
NikolaiPetukhov authored Dec 4, 2024
1 parent 5775770 commit 28201e1
Showing 1 changed file with 40 additions and 9 deletions.
49 changes: 40 additions & 9 deletions agent/worker/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ def __init__(self):
with open(self._history_file, "r") as json_file:
images_stat = json.load(json_file)
except json.JSONDecodeError:
self.logger.warning(f"Corrupted JSON in {self._history_file}. Resetting images_stat.")
self.logger.warning(
f"Corrupted JSON in {self._history_file}. Resetting images_stat."
)

images_stat[self.agent_info["agent_image"]] = cur_date

Expand Down Expand Up @@ -689,9 +691,10 @@ def task_clear_tasks_dir(self):
self.logger.info("Start background task: Clearing Tasks data")
try:
task_dir = constants.AGENT_TASKS_DIR()
shutil.move(task_dir, tmp_dir)
os.makedirs(task_dir)
shutil.rmtree(tmp_dir)
if os.path.exists(task_dir):
shutil.move(task_dir, tmp_dir)
os.makedirs(task_dir)
shutil.rmtree(tmp_dir)
except:
self.logger.warn("Background task error: Failed to clear tasks data", exc_info=True)
else:
Expand All @@ -716,9 +719,10 @@ def task_clear_pip_cache(self):
elif constants.SHOULD_CLEAN_PIP_CACHE():
self.logger.info("Start background task: Clearing pip cache")
try:
shutil.move(constants.APPS_PIP_CACHE_DIR(), tmp_dir)
os.makedirs(constants.APPS_PIP_CACHE_DIR())
shutil.rmtree(tmp_dir)
if os.path.exists(constants.APPS_PIP_CACHE_DIR()):
shutil.move(constants.APPS_PIP_CACHE_DIR(), tmp_dir)
os.makedirs(constants.APPS_PIP_CACHE_DIR())
shutil.rmtree(tmp_dir)
except:
self.logger.warn("Background task error: Failed to clear pip cache", exc_info=True)
else:
Expand All @@ -743,11 +747,38 @@ def task_clear_apps_data(self):
elif constants.SHOULD_CLEAN_APPS_DATA():
self.logger.info("Start background task: Clearing apps data")
try:
shutil.move(constants.AGENT_APPS_CACHE_DIR(), tmp_dir)
os.makedirs(constants.AGENT_APPS_CACHE_DIR())
if os.path.exists(constants.AGENT_APPS_CACHE_DIR()):
shutil.move(constants.AGENT_APPS_CACHE_DIR(), tmp_dir)
os.makedirs(constants.AGENT_APPS_CACHE_DIR())
shutil.rmtree(tmp_dir)
except:
self.logger.warn("Background task error: Failed to clear apps data", exc_info=True)
else:
self.logger.info(
"Background task finished: Agent data has been cleared successfully"
)
tmp_dir = constants.SUPERVISELY_SYNCED_APP_DATA_CONTAINER() + "_to_remove"
if os.path.exists(tmp_dir):
self.logger.info(
"Start background task: Clearing apps data [_to_remove directory detected]"
)
try:
shutil.rmtree(tmp_dir)
except:
self.logger.warn("Background task error: Failed to clear apps data", exc_info=True)
else:
self.logger.info(
"Background task finished: Apps data has been cleared successfully"
)
elif constants.SHOULD_CLEAN_APPS_DATA():
self.logger.info("Start background task: Clearing apps data")
try:
if os.path.exists(constants.SUPERVISELY_SYNCED_APP_DATA_CONTAINER()):
shutil.move(constants.SUPERVISELY_SYNCED_APP_DATA_CONTAINER(), tmp_dir)
os.makedirs(constants.SUPERVISELY_SYNCED_APP_DATA_CONTAINER())
shutil.rmtree(tmp_dir)
except:
self.logger.warn("Background task error: Failed to clear apps data", exc_info=True)
else:
self.logger.info(
"Background task finished: Agent data has been cleared successfully"
Expand Down

0 comments on commit 28201e1

Please sign in to comment.