diff --git a/invokeai/app/api/dependencies.py b/invokeai/app/api/dependencies.py index 0bbe524506..346b274248 100644 --- a/invokeai/app/api/dependencies.py +++ b/invokeai/app/api/dependencies.py @@ -1,8 +1,6 @@ # Copyright (c) 2022 Kyle Schouviller (https://github.com/kyle0654) -import shutil from logging import Logger -from pathlib import Path import torch @@ -54,14 +52,6 @@ def check_internet() -> bool: return False -def cleanup_tmpdirs(parent_folder: Path) -> None: - # Remove dangling tempdirs that might have been left over - # from an earlier unplanned shutdown. - for d in parent_folder.glob("tmp*"): - if d.is_dir(): - shutil.rmtree(d) - - logger = InvokeAILogger.get_logger() @@ -88,7 +78,6 @@ class ApiDependencies: configuration = config logger = logger - cleanup_tmpdirs(tensor_folder) board_image_records = SqliteBoardImageRecordStorage(db=db) board_images = BoardImagesService() board_records = SqliteBoardRecordStorage(db=db) diff --git a/invokeai/app/services/object_serializer/object_serializer_disk.py b/invokeai/app/services/object_serializer/object_serializer_disk.py index 935fec3060..16e5ffa9c9 100644 --- a/invokeai/app/services/object_serializer/object_serializer_disk.py +++ b/invokeai/app/services/object_serializer/object_serializer_disk.py @@ -1,3 +1,4 @@ +import shutil import tempfile import typing from dataclasses import dataclass @@ -81,5 +82,16 @@ class ObjectSerializerDisk(ObjectSerializerBase[T]): # In case the service is not properly stopped, clean up the temporary directory when the class instance is GC'd. self._tempdir_cleanup() + @classmethod + def _cleanup_dangling_temporary_dirs(cls, directory: Path): + # Remove dangling tempdirs that might have been left over + # from an earlier unplanned shutdown. + for d in directory.glob("tmp*"): + if d.is_dir(): + shutil.rmtree(d) + + def start(self, invoker: "Invoker") -> None: + self._cleanup_dangling_temporary_dirs(self._base_output_dir) + def stop(self, invoker: "Invoker") -> None: self._tempdir_cleanup()