From 57eebb61d5ab9722d136125ea3221fd015462fc2 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 10 May 2023 11:05:14 +0600 Subject: [PATCH] 0.2.2 --- CHANGELOG.md | 5 +++++ gradle.properties | 2 +- ratings/gc/src/jvmMain/kotlin/Plugin.kt | 27 ++++++++++++++++++++----- runner/config.json | 9 +++++++++ 4 files changed, 37 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index faeb258..ffdaa4b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # PlaguPoster +## 0.2.2 + +* `GarbageCollector`: + * Now on start will all clearing job done + ## 0.2.1 * `Versions`: diff --git a/gradle.properties b/gradle.properties index 4be7628..968ed54 100644 --- a/gradle.properties +++ b/gradle.properties @@ -10,4 +10,4 @@ android.enableJetifier=true # Project data group=dev.inmo -version=0.2.1 +version=0.2.2 diff --git a/ratings/gc/src/jvmMain/kotlin/Plugin.kt b/ratings/gc/src/jvmMain/kotlin/Plugin.kt index d8a297a..77f9ad9 100644 --- a/ratings/gc/src/jvmMain/kotlin/Plugin.kt +++ b/ratings/gc/src/jvmMain/kotlin/Plugin.kt @@ -1,12 +1,15 @@ package dev.inmo.plaguposter.ratings.gc +import com.soywiz.klock.DateTime import com.soywiz.klock.seconds import dev.inmo.krontab.KrontabTemplate import dev.inmo.krontab.toSchedule import dev.inmo.krontab.utils.asFlowWithDelays +import dev.inmo.micro_utils.coroutines.runCatchingSafely import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions import dev.inmo.micro_utils.repos.* import dev.inmo.plagubot.Plugin +import dev.inmo.plaguposter.posts.models.PostId import dev.inmo.plaguposter.posts.repo.PostsRepo import dev.inmo.plaguposter.ratings.models.Rating import dev.inmo.plaguposter.ratings.repo.RatingsRepo @@ -41,21 +44,35 @@ object Plugin : Plugin { val config = koin.get() config.immediateDrop ?.let { toDrop -> - ratingsRepo.onNewValue.subscribeSafelyWithoutExceptions(this) { - if (it.value <= toDrop) { - postsRepo.deleteById(it.id) + suspend fun checkAndOptionallyDrop(postId: PostId, rating: Rating) { + if (rating <= toDrop) { + postsRepo.deleteById(postId) } } + ratingsRepo.getAll().forEach { + runCatchingSafely { + checkAndOptionallyDrop(it.key, it.value) + } + } + ratingsRepo.onNewValue.subscribeSafelyWithoutExceptions(this) { + checkAndOptionallyDrop(it.first, it.second) + } } config.autoclear ?.let { autoclear -> - autoclear.autoClearKrontab.toSchedule().asFlowWithDelays().subscribeSafelyWithoutExceptions(scope) { - val dropCreatedBefore = it - (autoclear.skipPostAge ?: 0).seconds + suspend fun doAutoClear() { + val dropCreatedBefore = DateTime.now() - (autoclear.skipPostAge ?: 0).seconds ratingsRepo.getPostsWithRatingLessEq(autoclear.rating).keys.forEach { if ((postsRepo.getPostCreationTime(it) ?: return@forEach) < dropCreatedBefore) { postsRepo.deleteById(it) } } } + runCatchingSafely { + doAutoClear() + } + autoclear.autoClearKrontab.toSchedule().asFlowWithDelays().subscribeSafelyWithoutExceptions(scope) { + doAutoClear() + } } } } diff --git a/runner/config.json b/runner/config.json index 1ef0bf4..191ed5a 100644 --- a/runner/config.json +++ b/runner/config.json @@ -12,6 +12,7 @@ "dev.inmo.plaguposter.ratings.Plugin", "dev.inmo.plaguposter.ratings.source.Plugin", "dev.inmo.plaguposter.ratings.selector.Plugin", + "dev.inmo.plaguposter.ratings.gc.Plugin", "dev.inmo.plaguposter.triggers.selector_with_timer.Plugin", "dev.inmo.plagubot.plugins.inline.queries.Plugin", "dev.inmo.plaguposter.triggers.command.Plugin", @@ -72,5 +73,13 @@ }, "publish_command": { "panelButtonText": "Publish" + }, + "gc": { + "autoclear": { + "rating": -1, + "autoClearKrontab": "0 0 0 * *", + "skipPostAge": 86400 + }, + "immediateDrop": -6 } }