feat: rebuild AudioManager around downloadable moods
- V8 wipe migration and the shared song store keyed by YouTube video id - media store, indexer, downloader, recommendation top-up and refcounted GC - mood songs plus the recommendation lifecycle and mixed radio pools - new search, song and recommendation API; remove library/longform/seed routes - chart PVC/media env, docs, Nix, README and an end-to-end smoke test
This commit is contained in:
1 parent
50d8b6c580
commit
824eb36dd2
59 files changed
+3838
-5276
No files matched your search
@@ -20,7 +20,6 @@ import io.ktor.server.response.respondText
|
||||
import io.ktor.server.routing.get
|
||||
import io.ktor.server.routing.routing
|
||||
import java.nio.file.Files
|
||||
import java.time.Instant
|
||||
import java.util.concurrent.TimeUnit
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
@@ -33,87 +32,96 @@ import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kotlinx.serialization.json.Json
|
||||
import org.slf4j.LoggerFactory
|
||||
import win.taptap.musicmanager.config.AppConfig
|
||||
import win.taptap.musicmanager.config.LibraryConfig
|
||||
import win.taptap.musicmanager.config.UsersConfig
|
||||
import win.taptap.musicmanager.api.ApiErrorBody
|
||||
import win.taptap.musicmanager.api.ApiError
|
||||
import win.taptap.musicmanager.api.ApiErrorBody
|
||||
import win.taptap.musicmanager.api.ApiException
|
||||
import win.taptap.musicmanager.api.apiRoutes
|
||||
import win.taptap.musicmanager.config.AppConfig
|
||||
import win.taptap.musicmanager.config.UsersConfig
|
||||
import win.taptap.musicmanager.db.ConfigRepository
|
||||
import win.taptap.musicmanager.db.DatabaseFactory
|
||||
import win.taptap.musicmanager.db.MediaRepository
|
||||
import win.taptap.musicmanager.db.MoodRepository
|
||||
import win.taptap.musicmanager.discovery.DiscoveryService
|
||||
import win.taptap.musicmanager.discovery.TopUpService
|
||||
import win.taptap.musicmanager.discovery.YtDlpSearch
|
||||
import win.taptap.musicmanager.download.DownloadService
|
||||
import win.taptap.musicmanager.download.YtDlpDownloader
|
||||
import win.taptap.musicmanager.media.MediaGcService
|
||||
import win.taptap.musicmanager.media.MediaStore
|
||||
import win.taptap.musicmanager.radio.RadioService
|
||||
import win.taptap.musicmanager.scanner.CoverArtService
|
||||
import win.taptap.musicmanager.scanner.MediaScanner
|
||||
import win.taptap.musicmanager.scanner.SongIndexer
|
||||
import win.taptap.musicmanager.transcoding.FfmpegTranscoder
|
||||
|
||||
private val log = LoggerFactory.getLogger("win.taptap.musicmanager.Application")
|
||||
|
||||
fun main() {
|
||||
val config = AppConfig.fromEnv()
|
||||
val users = UsersConfig.load(config.usersFile)
|
||||
|
||||
val library = LibraryConfig.load(config.libraryConfigPath).also { it.validatePaths() }
|
||||
val users = UsersConfig.load(config.usersFile).also { it.validateAgainst(library) }
|
||||
Files.createDirectories(config.dataDir)
|
||||
Files.createDirectories(config.mediaDir)
|
||||
Files.createDirectories(config.downloadDir)
|
||||
|
||||
val db = DatabaseFactory.connect(config)
|
||||
try {
|
||||
ConfigRepository.sync(library, users)
|
||||
ConfigRepository.sync(users)
|
||||
|
||||
val applicationScope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
|
||||
val coverArtService = CoverArtService(config.dataDir)
|
||||
val mediaStore = MediaStore(config.mediaDir)
|
||||
val indexer = SongIndexer(mediaStore, coverArtService)
|
||||
val gc = MediaGcService(mediaStore, coverArtService)
|
||||
val transcoder = FfmpegTranscoder(config.ffmpegPath)
|
||||
val radioService = RadioService()
|
||||
val discoveryService = DiscoveryService(YtDlpSearch(config.ytDlpPath))
|
||||
val scanner = MediaScanner(
|
||||
library = library,
|
||||
coverArtService = coverArtService,
|
||||
val ytSearch = YtDlpSearch(config.ytDlpPath)
|
||||
val discoveryService = DiscoveryService(ytSearch)
|
||||
val topUpService = TopUpService(discoveryService, applicationScope)
|
||||
val scanner = MediaScanner(mediaStore, indexer)
|
||||
val downloadService = DownloadService(
|
||||
downloader = YtDlpDownloader(config.ytDlpPath),
|
||||
mediaStore = mediaStore,
|
||||
indexer = indexer,
|
||||
gc = gc,
|
||||
tempDir = config.downloadDir,
|
||||
)
|
||||
val downloadService = if (config.downloadsEnabled) {
|
||||
Files.createDirectories(config.downloadDir)
|
||||
DownloadService(
|
||||
users = users,
|
||||
library = library,
|
||||
downloader = YtDlpDownloader(config.ytDlpPath),
|
||||
tempDir = config.downloadDir,
|
||||
startScan = { scanner.start(applicationScope) },
|
||||
isScanning = { scanner.status().scanning },
|
||||
folderId = config.downloadFolderId,
|
||||
)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
if (MediaRepository.countSongs() == 0L) {
|
||||
log.info("Song table is empty, starting initial library scan")
|
||||
scanner.start(applicationScope)
|
||||
}
|
||||
|
||||
// Accepted recommendations that are not in the library are fetched in
|
||||
// the background and promoted after a scan.
|
||||
downloadService?.let { service ->
|
||||
applicationScope.launch(Dispatchers.IO) {
|
||||
delay(TimeUnit.MINUTES.toMillis(1))
|
||||
while (isActive) {
|
||||
val worked = runCatching { service.processOne() }
|
||||
.onFailure { log.warn("Download worker error", it) }
|
||||
.getOrDefault(false)
|
||||
delay(if (worked) 1_000 else 20_000)
|
||||
}
|
||||
// The media root is the only source of songs, so index it at startup.
|
||||
log.info("Scanning media root {}", config.mediaDir)
|
||||
scanner.start(applicationScope)
|
||||
|
||||
applicationScope.launch(Dispatchers.IO) {
|
||||
delay(TimeUnit.SECONDS.toMillis(5))
|
||||
while (isActive) {
|
||||
val worked = runCatching { downloadService.processOne() }
|
||||
.onFailure { log.warn("Download worker error", it) }
|
||||
.getOrDefault(false)
|
||||
delay(if (worked) 1_000 else 15_000)
|
||||
}
|
||||
}
|
||||
|
||||
// Moods refresh their YT recommendations in the background; the client
|
||||
// can also force a pass from the mood screen.
|
||||
// Refill recommendation slots and run due discovery passes.
|
||||
applicationScope.launch(Dispatchers.IO) {
|
||||
delay(TimeUnit.MINUTES.toMillis(2))
|
||||
delay(TimeUnit.MINUTES.toMillis(1))
|
||||
while (isActive) {
|
||||
runCatching { discoverStaleMoods(users, library, discoveryService) }
|
||||
.onFailure { log.warn("Background discovery pass failed", it) }
|
||||
runCatching {
|
||||
MoodRepository.all()
|
||||
.filter { it.recommendationCount > 0 }
|
||||
.forEach { mood ->
|
||||
topUpService.topUp(mood, force = false)
|
||||
}
|
||||
}.onFailure { log.warn("Background recommendation top-up failed", it) }
|
||||
delay(TimeUnit.HOURS.toMillis(1))
|
||||
}
|
||||
}
|
||||
|
||||
// Delete files that no mood, recommendation or open session references.
|
||||
applicationScope.launch(Dispatchers.IO) {
|
||||
delay(TimeUnit.MINUTES.toMillis(10))
|
||||
while (isActive) {
|
||||
runCatching { gc.collectGarbage() }
|
||||
.onFailure { log.warn("Media GC failed", it) }
|
||||
delay(TimeUnit.HOURS.toMillis(1))
|
||||
}
|
||||
}
|
||||
@@ -183,12 +191,15 @@ fun main() {
|
||||
}
|
||||
apiRoutes(
|
||||
users = users,
|
||||
library = library,
|
||||
coverArtService = coverArtService,
|
||||
transcoder = transcoder,
|
||||
radioService = radioService,
|
||||
discoveryService = discoveryService,
|
||||
downloadsEnabled = config.downloadsEnabled,
|
||||
topUpService = topUpService,
|
||||
ytSearch = ytSearch,
|
||||
ytResolver = ytSearch,
|
||||
mediaStore = mediaStore,
|
||||
gc = gc,
|
||||
startScan = { scanner.start(applicationScope) },
|
||||
scanStatus = { scanner.status() },
|
||||
)
|
||||
@@ -205,10 +216,10 @@ fun main() {
|
||||
)
|
||||
|
||||
log.info(
|
||||
"Starting MusicManager on port {} with {} library folders and {} users",
|
||||
"Starting AudioManager on port {} with {} users and media root {}",
|
||||
config.port,
|
||||
library.folders.size,
|
||||
users.users.size,
|
||||
config.mediaDir,
|
||||
)
|
||||
server.start(wait = true)
|
||||
} catch (t: Throwable) {
|
||||
@@ -216,15 +227,3 @@ fun main() {
|
||||
throw t
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun discoverStaleMoods(
|
||||
users: UsersConfig,
|
||||
library: LibraryConfig,
|
||||
discovery: DiscoveryService,
|
||||
) {
|
||||
MoodRepository.staleDiscovery(Instant.now(), 10).forEach { mood ->
|
||||
val owner = users.byName(mood.owner) ?: return@forEach
|
||||
runCatching { discovery.refresh(mood, owner, library) }
|
||||
.onFailure { log.warn("Discovery for mood {} failed", mood.name, it) }
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user