From 9edcb5566b22794964c89cac14082f87a1fd26d8 Mon Sep 17 00:00:00 2001 From: aarbit Date: Thu, 6 Jun 2024 00:32:09 -0500 Subject: [PATCH] Makes caching async --- build.gradle.kts | 10 ++++++++- .../controller/CacheController.kt | 2 +- .../service/AlbumCacheService.kt | 16 ++++++++++---- .../prettyplayerapi/service/MusicService.kt | 1 + .../prettyplayerapi/service/StartupService.kt | 21 ++++++++++++++----- 5 files changed, 39 insertions(+), 11 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index a7b99dc..0b0e434 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -8,7 +8,7 @@ plugins { } group = "net.halfbinary" -version = "0.0.8-SNAPSHOT" +version = "0.0.11-SNAPSHOT" java { sourceCompatibility = JavaVersion.VERSION_21 @@ -32,6 +32,11 @@ val coroutinesVersion = "1.8.1" dependencies { implementation("org.springframework.boot:spring-boot-starter-web") + implementation("org.springframework.boot:spring-boot-starter-webflux") + implementation("com.fasterxml.jackson.module:jackson-module-kotlin") + implementation("io.projectreactor.kotlin:reactor-kotlin-extensions") + implementation("org.jetbrains.kotlin:kotlin-reflect") + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor") implementation("com.fasterxml.jackson.module:jackson-module-kotlin") implementation("org.jetbrains.kotlin:kotlin-reflect") implementation("com.mpatric:mp3agic:$mp3agicVersion") @@ -43,6 +48,9 @@ dependencies { annotationProcessor("org.springframework.boot:spring-boot-configuration-processor") testImplementation("org.springframework.boot:spring-boot-starter-test") testImplementation("ch.qos.logback:logback-classic:$logbackVersion") + testImplementation("org.springframework.boot:spring-boot-starter-test") + testImplementation("io.projectreactor:reactor-test") + testImplementation("org.jetbrains.kotlin:kotlin-test-junit5") testRuntimeOnly("org.junit.platform:junit-platform-launcher") } diff --git a/src/main/kotlin/net/halfbinary/prettyplayerapi/controller/CacheController.kt b/src/main/kotlin/net/halfbinary/prettyplayerapi/controller/CacheController.kt index cb6c012..0334546 100644 --- a/src/main/kotlin/net/halfbinary/prettyplayerapi/controller/CacheController.kt +++ b/src/main/kotlin/net/halfbinary/prettyplayerapi/controller/CacheController.kt @@ -9,7 +9,7 @@ import org.springframework.web.bind.annotation.RestController @CrossOrigin class CacheController(private val albumCacheService: AlbumCacheService) { @GetMapping("cache") - fun refreshAlbumCache() { + suspend fun refreshAlbumCache() { albumCacheService.refreshAlbumCache() } } \ No newline at end of file diff --git a/src/main/kotlin/net/halfbinary/prettyplayerapi/service/AlbumCacheService.kt b/src/main/kotlin/net/halfbinary/prettyplayerapi/service/AlbumCacheService.kt index 1b10535..322100d 100644 --- a/src/main/kotlin/net/halfbinary/prettyplayerapi/service/AlbumCacheService.kt +++ b/src/main/kotlin/net/halfbinary/prettyplayerapi/service/AlbumCacheService.kt @@ -2,8 +2,8 @@ package net.halfbinary.prettyplayerapi.service import com.mpatric.mp3agic.Mp3File import io.github.oshai.kotlinlogging.KotlinLogging +import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.launch -import kotlinx.coroutines.runBlocking import net.halfbinary.prettyplayerapi.exception.EnvironmentVariableNotFoundException import net.halfbinary.prettyplayerapi.model.AlbumInfo import net.halfbinary.prettyplayerapi.repository.AlbumRepository @@ -20,8 +20,14 @@ private val logger = KotlinLogging.logger {} @Service class AlbumCacheService(private val albumRepository: AlbumRepository) { - fun refreshAlbumCache() { - runBlocking{ + + suspend fun doAlbumCache() { + coroutineScope { + launch { cacheAlbums(false) } + } + } + suspend fun refreshAlbumCache() { + coroutineScope{ launch { cacheAlbums(true)} } } @@ -43,7 +49,9 @@ class AlbumCacheService(private val albumRepository: AlbumRepository) { .map { val folder = it.first.parentFile.toPath() val folderHash = folder.name.hashCode().toString() - runBlocking { launch {getAndWriteAlbumImageFile(it.first, folderHash, folder, refresh)} } + coroutineScope { + launch {getAndWriteAlbumImageFile(it.first, folderHash, folder, refresh)} + } Pair(folderHash, AlbumInfo(it.second.id3v2Tag.album, it.first.parent, folderHash)) } diff --git a/src/main/kotlin/net/halfbinary/prettyplayerapi/service/MusicService.kt b/src/main/kotlin/net/halfbinary/prettyplayerapi/service/MusicService.kt index 83fe0eb..ff8fad3 100644 --- a/src/main/kotlin/net/halfbinary/prettyplayerapi/service/MusicService.kt +++ b/src/main/kotlin/net/halfbinary/prettyplayerapi/service/MusicService.kt @@ -10,6 +10,7 @@ class MusicService(private val albumCacheService: AlbumCacheService) { val albumInfo = albumCacheService.getAlbumCache()[albumHash] return if(albumInfo != null) { File(albumInfo.albumFolder).listFiles()[trackNumber-1] + File(albumInfo.albumFolder).listFiles().sortedBy { it.name }[trackNumber-1] } else { throw AlbumHashNotFoundException("Album hash $albumHash not found") } diff --git a/src/main/kotlin/net/halfbinary/prettyplayerapi/service/StartupService.kt b/src/main/kotlin/net/halfbinary/prettyplayerapi/service/StartupService.kt index 1856d00..2869621 100644 --- a/src/main/kotlin/net/halfbinary/prettyplayerapi/service/StartupService.kt +++ b/src/main/kotlin/net/halfbinary/prettyplayerapi/service/StartupService.kt @@ -1,15 +1,26 @@ package net.halfbinary.prettyplayerapi.service -import jakarta.annotation.PostConstruct +import io.github.oshai.kotlinlogging.KotlinLogging +import kotlinx.coroutines.coroutineScope +import kotlinx.coroutines.launch +import org.springframework.boot.context.event.ApplicationStartedEvent +import org.springframework.context.event.EventListener +import org.springframework.scheduling.annotation.Async import org.springframework.stereotype.Component - +private val logger = KotlinLogging.logger {} @Component class StartupService(private val albumCacheService: AlbumCacheService) { - @PostConstruct - fun onStartup() { - albumCacheService.refreshAlbumCache() + @Async + @EventListener(ApplicationStartedEvent::class) + suspend fun onStartup() { + coroutineScope { + launch { + logger.info { "Starting up..." } + albumCacheService.doAlbumCache() + } + } } } \ No newline at end of file