fix(worker): import_album recovers orphaned .old/.importing from an interrupted swap
Adds a crash-recovery preamble that restores `final` from `.old` when a prior swap was interrupted after rename(final, old) but before rename(tmp, final), and always drops stale `.old`/`.importing` orphans. Also guards the swap: if nothing new was staged into tmp (empty/missing staging) and `final` already exists, leave it untouched instead of swapping in an empty copy. This is required beyond the literal preamble because the pre-existing swap logic unconditionally replaced `final` with `tmp` regardless of whether anything was actually imported, which would otherwise destroy a just-recovered (or any existing) copy whenever staging is empty.
This commit is contained in:
@@ -64,20 +64,34 @@ def import_album(staging: str, final: str) -> str:
|
|||||||
Assembles into a sibling temp dir, then swaps via rename so a mid-assembly failure never
|
Assembles into a sibling temp dir, then swaps via rename so a mid-assembly failure never
|
||||||
destroys the existing copy. Non-audio artifacts/subfolders in `staging` are left behind.
|
destroys the existing copy. Non-audio artifacts/subfolders in `staging` are left behind.
|
||||||
Tolerates a missing `staging`. Returns `final`."""
|
Tolerates a missing `staging`. Returns `final`."""
|
||||||
|
# Recover from a swap interrupted by a crash: {final}.old holds the previous copy.
|
||||||
|
old = final + ".old"
|
||||||
|
if os.path.isdir(old) and not os.path.isdir(final):
|
||||||
|
os.rename(old, final) # restore the last-good copy
|
||||||
|
shutil.rmtree(old, ignore_errors=True) # drop a leftover .old (swap had completed)
|
||||||
|
shutil.rmtree(final + ".importing", ignore_errors=True) # drop a stale half-assembly
|
||||||
os.makedirs(os.path.dirname(final), exist_ok=True)
|
os.makedirs(os.path.dirname(final), exist_ok=True)
|
||||||
tmp = final + ".importing"
|
tmp = final + ".importing"
|
||||||
shutil.rmtree(tmp, ignore_errors=True)
|
shutil.rmtree(tmp, ignore_errors=True)
|
||||||
os.makedirs(tmp)
|
os.makedirs(tmp)
|
||||||
|
imported = False
|
||||||
if os.path.isdir(staging):
|
if os.path.isdir(staging):
|
||||||
for name in sorted(os.listdir(staging)):
|
for name in sorted(os.listdir(staging)):
|
||||||
src = os.path.join(staging, name)
|
src = os.path.join(staging, name)
|
||||||
if os.path.isfile(src) and os.path.splitext(name)[1].lower() in _AUDIO_EXT:
|
if os.path.isfile(src) and os.path.splitext(name)[1].lower() in _AUDIO_EXT:
|
||||||
shutil.move(src, os.path.join(tmp, name))
|
shutil.move(src, os.path.join(tmp, name))
|
||||||
|
imported = True
|
||||||
cover = _find_cover(staging)
|
cover = _find_cover(staging)
|
||||||
if cover is not None:
|
if cover is not None:
|
||||||
shutil.move(cover, os.path.join(tmp, "cover.jpg"))
|
shutil.move(cover, os.path.join(tmp, "cover.jpg"))
|
||||||
|
imported = True
|
||||||
# swap: keep the old copy until the new one is assembled, then rename into place
|
# swap: keep the old copy until the new one is assembled, then rename into place
|
||||||
if os.path.isdir(final):
|
if os.path.isdir(final):
|
||||||
|
if not imported:
|
||||||
|
# Nothing new to import (e.g. a bare recovery pass with no/empty staging): leave the
|
||||||
|
# existing (possibly just-recovered) `final` untouched rather than swapping in empty.
|
||||||
|
shutil.rmtree(tmp, ignore_errors=True)
|
||||||
|
return final
|
||||||
old = final + ".old"
|
old = final + ".old"
|
||||||
shutil.rmtree(old, ignore_errors=True)
|
shutil.rmtree(old, ignore_errors=True)
|
||||||
os.rename(final, old)
|
os.rename(final, old)
|
||||||
|
|||||||
@@ -55,6 +55,29 @@ def test_import_album_leaves_no_temp_siblings(tmp_path):
|
|||||||
assert os.listdir(tmp_path / "Artist") == ["Album"] # no .importing / .old leftovers
|
assert os.listdir(tmp_path / "Artist") == ["Album"] # no .importing / .old leftovers
|
||||||
|
|
||||||
|
|
||||||
|
def test_import_album_recovers_from_interrupted_swap(tmp_path):
|
||||||
|
final = str(tmp_path / "Artist" / "Album (2020)")
|
||||||
|
os.makedirs(final + ".old")
|
||||||
|
open(os.path.join(final + ".old", "01 Song.flac"), "w").close()
|
||||||
|
# `final` is missing (crash happened after rename(final, old), before rename(tmp, final))
|
||||||
|
staging = str(tmp_path / "staging") # empty/missing staging
|
||||||
|
import_album(staging, final)
|
||||||
|
# previous good copy restored to `final`; no leftover .old
|
||||||
|
assert os.path.isfile(os.path.join(final, "01 Song.flac"))
|
||||||
|
assert not os.path.isdir(final + ".old")
|
||||||
|
|
||||||
|
|
||||||
|
def test_import_album_drops_stale_old_when_final_present(tmp_path):
|
||||||
|
final = str(tmp_path / "Artist" / "Album (2020)")
|
||||||
|
os.makedirs(final)
|
||||||
|
open(os.path.join(final, "keep.flac"), "w").close()
|
||||||
|
os.makedirs(final + ".old") # leftover from a completed swap
|
||||||
|
open(os.path.join(final + ".old", "stale.flac"), "w").close()
|
||||||
|
import_album(str(tmp_path / "staging"), final)
|
||||||
|
assert not os.path.isdir(final + ".old") # stale .old removed
|
||||||
|
assert os.path.isfile(os.path.join(final, "keep.flac")) # existing copy untouched by preamble
|
||||||
|
|
||||||
|
|
||||||
def test_clear_staging_root_removes_jobs_but_keeps_root(tmp_path):
|
def test_clear_staging_root_removes_jobs_but_keeps_root(tmp_path):
|
||||||
# Clears per-job dirs (and stray files) under the root, but leaves the root itself so a
|
# Clears per-job dirs (and stray files) under the root, but leaves the root itself so a
|
||||||
# mounted staging volume stays mounted.
|
# mounted staging volume stays mounted.
|
||||||
|
|||||||
Reference in New Issue
Block a user