Merge pull request #6 from thanek/scheduled_tree_rebuild

scheduled tree rebuild fix
This commit is contained in:
xis 2023-10-18 16:03:58 +02:00 committed by GitHub
commit 96481a0d1a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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.runApplication
import org.springframework.scheduling.annotation.EnableScheduling
import org.springframework.web.servlet.config.annotation.EnableWebMvc
@SpringBootApplication
@EnableWebMvc
@EnableScheduling
class NextcloudDLNAApp
fun main(args: Array<String>) {

View file

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

View file

@ -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
}

View file

@ -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