Skip to content

Commit

Permalink
Write temporary file to local /tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
albireox committed Nov 7, 2023
1 parent c1741b6 commit d0a7b45
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions archon/actor/delegate.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from contextlib import suppress
from dataclasses import dataclass, field
from functools import partial, reduce
from tempfile import NamedTemporaryFile, TemporaryFile
from time import time
from unittest.mock import MagicMock
from uuid import uuid4
Expand Down Expand Up @@ -436,29 +437,30 @@ async def _write_to_file(self, hdu: fits.PrimaryHDU, file_path: str):
loop = asyncio.get_running_loop()

writeto = partial(hdu.writeto, checksum=True)

temp_uuid = uuid4().hex[:8]
temp_file = file_path + f".{temp_uuid}"
temp_file = NamedTemporaryFile(suffix=".fits", delete=False).name

if file_path.endswith(".gz"):
# Astropy compresses with gzip -9 which takes forever.
# Instead we compress manually with -1, which is still pretty good.
await loop.run_in_executor(None, writeto, file_path[:-3])
await gzip_async(file_path[:-3], complevel=1, suffix=f".gz.{temp_uuid}")
await loop.run_in_executor(None, writeto, temp_file)
await gzip_async(temp_file, complevel=1, suffix=".gz")
temp_file = temp_file + ".gz"
else:
await loop.run_in_executor(None, writeto, temp_file)

assert os.path.exists(temp_file), "Failed writing image to disk."

# Rename to final file.
try:
shutil.move(temp_file, file_path)
shutil.copyfile(temp_file, file_path)
except Exception:
self.command.error(
f"Failed renaming temporary file {temp_file}. "
"The original file is still available."
)
return
else:
os.unlink(temp_file)

return file_path

Expand Down

0 comments on commit d0a7b45

Please sign in to comment.