fix(download): explain filesystem failures and prepare the import dir
CI / chart (push) Successful in 3s
CI / chart-push (push) Successful in 4s
CI / test (push) Successful in 1m47s
CI / image (push) Successful in 3m45s

A failing Files.createDirectories only produced the directory path in the log,
which hid whether the music hostPath was read-only or owned by another user.
Errors now include the exception type, cause and a hint about UID 10001, and
permission or read-only failures fail immediately instead of retrying three
times.

The chart gets a prepare-imports init container (downloads.prepare) that
creates "<music>/AudioManager Imports" and gives it to UID 10001, so downloads
work on a hostPath owned by root. Chart version 0.2.4.
This commit is contained in:
TapTap committed 2026-11-15 14:40:28 +00:00
1 parent a5af1ee83f
commit e7704c42ea
6 files changed
+114 -8

No files matched your search

@@ -1,7 +1,10 @@
package win.taptap.musicmanager.download
import java.io.IOException
import java.nio.file.AccessDeniedException
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.ReadOnlyFileSystemException
import java.nio.file.StandardCopyOption
import org.slf4j.LoggerFactory
import win.taptap.musicmanager.config.FolderType
@@ -42,9 +45,10 @@ class DownloadService(
try {
process(job)
} catch (t: Throwable) {
val message = t.message ?: t::class.simpleName ?: "Download failed"
log.warn("Download {} failed (attempt {}): {}", job.videoId, job.attempts, message)
if (job.attempts >= maxAttempts) {
val message = describe(t)
val attempts = if (isPermanent(t)) maxAttempts else job.attempts
log.warn("Download {} failed (attempt {}): {}", job.videoId, attempts, message)
if (attempts >= maxAttempts) {
DownloadRepository.markFailed(job.id, message)
} else {
DownloadRepository.retryLater(job.id, message)
@@ -64,8 +68,7 @@ class DownloadService(
?: folders.values.firstOrNull()
?: throw IllegalStateException("No writable music folder for ${job.userName}")
val importDir = folder.path.resolve(IMPORT_DIR)
Files.createDirectories(importDir)
val importDir = prepareImportDir(folder.path)
val working = tempDir.resolve(job.id)
Files.createDirectories(working)
@@ -73,7 +76,14 @@ class DownloadService(
val downloaded = downloader.download(job.videoId, job.title, job.artist, working)
val extension = downloaded.fileName.toString().substringAfterLast('.', "mp3")
val target = uniqueTarget(importDir, job.artist, job.title, extension)
Files.move(downloaded, target, StandardCopyOption.REPLACE_EXISTING)
try {
Files.move(downloaded, target, StandardCopyOption.REPLACE_EXISTING)
} catch (e: IOException) {
throw IllegalStateException(
"Downloaded file could not be moved to $target: ${describe(e)}",
e,
)
}
log.info("Downloaded {} to {}", job.videoId, target)
awaitScan()
@@ -88,6 +98,22 @@ class DownloadService(
}
}
private fun prepareImportDir(musicFolder: Path): Path {
val dir = musicFolder.resolve(IMPORT_DIR)
try {
Files.createDirectories(dir)
} catch (e: IOException) {
throw IllegalStateException(
"Cannot write to the music folder: $dir. The server runs as UID 10001; " +
"make sure the hostPath is mounted read-write and that UID 10001 can write " +
"to it (the chart's prepare-imports init container does this). " +
"Underlying error: ${describe(e)}",
e,
)
}
return dir
}
private fun awaitScan() {
// If another scan is already running, wait for that one instead.
startScan()
@@ -122,6 +148,28 @@ class DownloadService(
return candidate
}
/** Includes the exception type and cause, which plain NIO messages omit. */
private fun describe(t: Throwable): String =
generateSequence(t) { it.cause }
.map { cause ->
val text = cause.message?.takeIf { it.isNotBlank() }
if (text == null) cause::class.simpleName ?: "error"
else "${cause::class.simpleName}: $text"
}
.distinct()
.take(3)
.joinToString(" -> ")
.take(1000)
/** Retrying a permission or read-only error never helps. */
private fun isPermanent(t: Throwable): Boolean =
generateSequence(t) { it.cause }.any { cause ->
cause is AccessDeniedException ||
cause is ReadOnlyFileSystemException ||
cause.message?.contains("Read-only", ignoreCase = true) == true ||
cause.message?.contains("Permission denied", ignoreCase = true) == true
}
private fun sanitize(value: String): String =
value.replace(Regex("[\\\\/:*?\"<>|\\p{Cntrl}]"), "_")
.replace(Regex("\\s+"), " ")