scheduled tree rebuild fix

This commit is contained in:
xis 2023-10-18 15:55:04 +02:00
parent 59ea5a3e11
commit bfeb202307
4 changed files with 19 additions and 10 deletions

View file

@ -2,10 +2,12 @@ package net.schowek.nextclouddlna
import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication import org.springframework.boot.runApplication
import org.springframework.scheduling.annotation.EnableScheduling
import org.springframework.web.servlet.config.annotation.EnableWebMvc import org.springframework.web.servlet.config.annotation.EnableWebMvc
@SpringBootApplication @SpringBootApplication
@EnableWebMvc @EnableWebMvc
@EnableScheduling
class NextcloudDLNAApp class NextcloudDLNAApp
fun main(args: Array<String>) { fun main(args: Array<String>) {

View file

@ -57,7 +57,7 @@ class ContentController(
@RequestMapping(method = [RequestMethod.GET], value = ["/rebuild"]) @RequestMapping(method = [RequestMethod.GET], value = ["/rebuild"])
@ResponseBody @ResponseBody
fun reloadTree(): ResponseEntity<*> { fun reloadTree(): ResponseEntity<*> {
contentTreeProvider.rebuildTree() contentTreeProvider.rebuildTree(true)
return ResponseEntity<Any>(HttpStatus.OK) return ResponseEntity<Any>(HttpStatus.OK)
} }

View file

@ -16,7 +16,7 @@ class ContentTreeProvider(
private val clock: Clock private val clock: Clock
) { ) {
private var tree = buildContentTree() private var tree = buildContentTree()
var lastMTime = 0L var lastBuildTime = 0L
@Scheduled(fixedDelay = REBUILD_TREE_DELAY_IN_MS, initialDelay = REBUILD_TREE_INIT_DELAY_IN_MS) @Scheduled(fixedDelay = REBUILD_TREE_DELAY_IN_MS, initialDelay = REBUILD_TREE_INIT_DELAY_IN_MS)
final fun rebuildTree(): Boolean { final fun rebuildTree(): Boolean {
@ -26,13 +26,19 @@ class ContentTreeProvider(
final fun rebuildTree(force: Boolean): Boolean { final fun rebuildTree(force: Boolean): Boolean {
val maxMtime: Long = nextcloudDB.maxMtime() val maxMtime: Long = nextcloudDB.maxMtime()
val now = Instant.now(clock).epochSecond val now = Instant.now(clock).epochSecond
val rebuild = force || lastMTime < maxMtime || lastMTime + MAX_REBUILD_OFFSET_IN_S < now
if (rebuild) { val rebuildReasons = mapOf(
logger.info("ContentTree seems to be outdated - Loading...") "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() this.tree = buildContentTree()
lastMTime = Instant.now().epochSecond true
} } else false
return rebuild
} }
private final fun buildContentTree(): ContentTree { private final fun buildContentTree(): ContentTree {
@ -51,6 +57,7 @@ class ContentTreeProvider(
} }
logger.info("Found {} items in {} nodes", tree.itemsCount, tree.nodesCount) logger.info("Found {} items in {} nodes", tree.itemsCount, tree.nodesCount)
loadThumbnails(tree) loadThumbnails(tree)
lastBuildTime = Instant.now().epochSecond
return tree return tree
} }

View file

@ -24,7 +24,7 @@ class ContentTreeProviderTest extends Specification {
def clock = Clock.fixed(now, ZoneId.systemDefault()) def clock = Clock.fixed(now, ZoneId.systemDefault())
def sut = new ContentTreeProvider(nextcloudDB, clock) def sut = new ContentTreeProvider(nextcloudDB, clock)
sut.lastMTime = lastMTime.epochSecond sut.lastBuildTime = lastBuildTime.epochSecond
nextcloudDB.maxMtime() >> maxMtime.epochSecond nextcloudDB.maxMtime() >> maxMtime.epochSecond
when: when:
@ -34,7 +34,7 @@ class ContentTreeProviderTest extends Specification {
rebuild == expectedResult rebuild == expectedResult
where: 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().minus(1, DAYS) || true
true | now() | ofEpochSecond(0L) | now().plus(1, DAYS) || true true | now() | ofEpochSecond(0L) | now().plus(1, DAYS) || true
false | now() | ofEpochSecond(0L) | now().minus(1, DAYS) || true false | now() | ofEpochSecond(0L) | now().minus(1, DAYS) || true