From eb913c4446fc2a0907f8f5690530a9962a23fb67 Mon Sep 17 00:00:00 2001 From: Olaf Wintermann Date: Mon, 8 Sep 2025 09:45:49 +0200 Subject: [PATCH] calculate delay time until next sync --- .../main/kotlin/de/unixwork/rssreader/App.kt | 18 +++++++++++++---- .../kotlin/de/unixwork/rssreader/Database.kt | 20 +++++++++++++++++++ 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/rss-application/src/main/kotlin/de/unixwork/rssreader/App.kt b/rss-application/src/main/kotlin/de/unixwork/rssreader/App.kt index cd127fa..8a151dc 100644 --- a/rss-application/src/main/kotlin/de/unixwork/rssreader/App.kt +++ b/rss-application/src/main/kotlin/de/unixwork/rssreader/App.kt @@ -36,7 +36,7 @@ class App : Application { window = MainWindow() window?.show() - backgroundSync(10 * 1000L) + backgroundSync(10 * 1000L) // TODO: config } override fun shutdown() { @@ -50,7 +50,7 @@ class App : Application { while(true) { println("Background sync") try { - val pending = Database.getPendingFeeds(1 * 60) + val pending = Database.getPendingFeeds(1 * 60) // TODO: config if(!pending.isEmpty()) { SyncJob { pending }.syncBlocking() GlobalScope.launch(ToolkitDispatcher) { @@ -64,9 +64,19 @@ class App : Application { } println("Background sync done") - // TODO: check time until next feed needs sync + // get time until the next feed is pending, but wait at least the minimum delay time + var delayTimeMS = 60 * 1000L // TODO: config + try { + val seconds = Database.getUpdateWaitTime(1 * 60) // TODO: config + if(seconds > 0) { + println("wait for $seconds seconds until next sync") + delayTimeMS = seconds * 1000L + } + } catch (e: Exception) { + e.printStackTrace() + } - delay(60 * 1000L) + delay(delayTimeMS) } } } diff --git a/rss-application/src/main/kotlin/de/unixwork/rssreader/Database.kt b/rss-application/src/main/kotlin/de/unixwork/rssreader/Database.kt index 4461840..92d3020 100644 --- a/rss-application/src/main/kotlin/de/unixwork/rssreader/Database.kt +++ b/rss-application/src/main/kotlin/de/unixwork/rssreader/Database.kt @@ -402,4 +402,24 @@ object Database { } } } + + public fun getUpdateWaitTime(defaultUpdateInterval: Int) : Int { + var seconds = 0 + dataSource.connection.use { conn -> + conn.prepareStatement(""" + select + datediff(ss, now(), min(dateadd(ss, case when c.update_interval = 0 then ? else c.update_interval end, coalesce(last_update, '1970-01-01')))) as next_update_in + from feeds f + inner join feedcollections c on f.feedcollection_id = c.feedcollection_id; + """.trimIndent()).use { stmt -> + stmt.setInt(1, defaultUpdateInterval) + stmt.executeQuery().use { rs -> + if(rs.next()) { + seconds = rs.getInt(1) + } + } + } + } + return seconds + } } -- 2.47.3