diff --git a/src/main/kotlin/net/schowek/nextclouddlna/NextcloudDLNAApp.kt b/src/main/kotlin/net/schowek/nextclouddlna/NextcloudDLNAApp.kt index bb0f6e9..30c574b 100644 --- a/src/main/kotlin/net/schowek/nextclouddlna/NextcloudDLNAApp.kt +++ b/src/main/kotlin/net/schowek/nextclouddlna/NextcloudDLNAApp.kt @@ -2,10 +2,12 @@ package net.schowek.nextclouddlna import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.boot.runApplication +import org.springframework.scheduling.annotation.EnableScheduling import org.springframework.web.servlet.config.annotation.EnableWebMvc @SpringBootApplication @EnableWebMvc +@EnableScheduling class NextcloudDLNAApp fun main(args: Array) { diff --git a/src/main/kotlin/net/schowek/nextclouddlna/controller/ContentController.kt b/src/main/kotlin/net/schowek/nextclouddlna/controller/ContentController.kt index 19a77a9..bbfae82 100644 --- a/src/main/kotlin/net/schowek/nextclouddlna/controller/ContentController.kt +++ b/src/main/kotlin/net/schowek/nextclouddlna/controller/ContentController.kt @@ -57,7 +57,7 @@ class ContentController( @RequestMapping(method = [RequestMethod.GET], value = ["/rebuild"]) @ResponseBody fun reloadTree(): ResponseEntity<*> { - contentTreeProvider.rebuildTree() + contentTreeProvider.rebuildTree(true) return ResponseEntity(HttpStatus.OK) } diff --git a/src/main/kotlin/net/schowek/nextclouddlna/nextcloud/content/ContentTreeProvider.kt b/src/main/kotlin/net/schowek/nextclouddlna/nextcloud/content/ContentTreeProvider.kt index 88ec872..a57e5e5 100644 --- a/src/main/kotlin/net/schowek/nextclouddlna/nextcloud/content/ContentTreeProvider.kt +++ b/src/main/kotlin/net/schowek/nextclouddlna/nextcloud/content/ContentTreeProvider.kt @@ -16,7 +16,7 @@ class ContentTreeProvider( private val clock: Clock ) { private var tree = buildContentTree() - var lastMTime = 0L + var lastBuildTime = 0L @Scheduled(fixedDelay = REBUILD_TREE_DELAY_IN_MS, initialDelay = REBUILD_TREE_INIT_DELAY_IN_MS) final fun rebuildTree(): Boolean { @@ -26,13 +26,19 @@ class ContentTreeProvider( final fun rebuildTree(force: Boolean): Boolean { val maxMtime: Long = nextcloudDB.maxMtime() val now = Instant.now(clock).epochSecond - val rebuild = force || lastMTime < maxMtime || lastMTime + MAX_REBUILD_OFFSET_IN_S < now - if (rebuild) { - logger.info("ContentTree seems to be outdated - Loading...") + + val rebuildReasons = mapOf( + "forced rebuild" to force, + "nextcloud content changed" to (lastBuildTime < maxMtime), + "scheduled rebuild" to (lastBuildTime + MAX_REBUILD_OFFSET_IN_S < now) + ).filter { it.value } + + return if (rebuildReasons.any()) { + val reasonsList = rebuildReasons.keys.joinToString { it } + logger.info("Rebuilding the content tree, reason: $reasonsList...") this.tree = buildContentTree() - lastMTime = Instant.now().epochSecond - } - return rebuild + true + } else false } private final fun buildContentTree(): ContentTree { @@ -51,6 +57,7 @@ class ContentTreeProvider( } logger.info("Found {} items in {} nodes", tree.itemsCount, tree.nodesCount) loadThumbnails(tree) + lastBuildTime = Instant.now().epochSecond return tree } diff --git a/src/test/groovy/net/schowek/nextclouddlna/nextcloud/content/ContentTreeProviderTest.groovy b/src/test/groovy/net/schowek/nextclouddlna/nextcloud/content/ContentTreeProviderTest.groovy index 1a7822a..3bbecce 100644 --- a/src/test/groovy/net/schowek/nextclouddlna/nextcloud/content/ContentTreeProviderTest.groovy +++ b/src/test/groovy/net/schowek/nextclouddlna/nextcloud/content/ContentTreeProviderTest.groovy @@ -24,7 +24,7 @@ class ContentTreeProviderTest extends Specification { def clock = Clock.fixed(now, ZoneId.systemDefault()) def sut = new ContentTreeProvider(nextcloudDB, clock) - sut.lastMTime = lastMTime.epochSecond + sut.lastBuildTime = lastBuildTime.epochSecond nextcloudDB.maxMtime() >> maxMtime.epochSecond when: @@ -34,7 +34,7 @@ class ContentTreeProviderTest extends Specification { rebuild == expectedResult where: - force | now | lastMTime | maxMtime || expectedResult + force | now | lastBuildTime | maxMtime || expectedResult true | now() | ofEpochSecond(0L) | now().minus(1, DAYS) || true true | now() | ofEpochSecond(0L) | now().plus(1, DAYS) || true false | now() | ofEpochSecond(0L) | now().minus(1, DAYS) || true