fix(app): only clear tempdirs if ephemeral and before creating tempdir

Also, this needs to happen in init, else it deletes the temp dir created in init
This commit is contained in:
psychedelicious 2024-04-23 16:55:46 +10:00
parent 4b2b983646
commit e6386d969f

View File

@ -36,6 +36,12 @@ class ObjectSerializerDisk(ObjectSerializerBase[T]):
self._ephemeral = ephemeral self._ephemeral = ephemeral
self._base_output_dir = output_dir self._base_output_dir = output_dir
self._base_output_dir.mkdir(parents=True, exist_ok=True) self._base_output_dir.mkdir(parents=True, exist_ok=True)
if self._ephemeral:
# Remove dangling tempdirs that might have been left over from an earlier unplanned shutdown.
for temp_dir in filter(Path.is_dir, self._base_output_dir.glob("tmp*")):
shutil.rmtree(temp_dir)
# Must specify `ignore_cleanup_errors` to avoid fatal errors during cleanup on Windows # Must specify `ignore_cleanup_errors` to avoid fatal errors during cleanup on Windows
self._tempdir = ( self._tempdir = (
tempfile.TemporaryDirectory(dir=self._base_output_dir, ignore_cleanup_errors=True) if ephemeral else None tempfile.TemporaryDirectory(dir=self._base_output_dir, ignore_cleanup_errors=True) if ephemeral else None
@ -82,16 +88,5 @@ class ObjectSerializerDisk(ObjectSerializerBase[T]):
# In case the service is not properly stopped, clean up the temporary directory when the class instance is GC'd. # In case the service is not properly stopped, clean up the temporary directory when the class instance is GC'd.
self._tempdir_cleanup() 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: def stop(self, invoker: "Invoker") -> None:
self._tempdir_cleanup() self._tempdir_cleanup()