Skip to content

Commit

Permalink
Merge pull request #6 from thanek/scheduled_tree_rebuild
Browse files Browse the repository at this point in the history
scheduled tree rebuild fix
  • Loading branch information
thanek authored Oct 18, 2023
2 parents 59ea5a3 + bfeb202 commit 96481a0
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/main/kotlin/net/schowek/nextclouddlna/NextcloudDLNAApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -51,6 +57,7 @@ class ContentTreeProvider(
}
logger.info("Found {} items in {} nodes", tree.itemsCount, tree.nodesCount)
loadThumbnails(tree)
lastBuildTime = Instant.now().epochSecond
return tree
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down

0 comments on commit 96481a0

Please sign in to comment.