This commit is contained in:
InsanusMokrassar 2023-05-10 11:05:14 +06:00
parent 87957dba30
commit 57eebb61d5
4 changed files with 37 additions and 6 deletions

View File

@ -1,5 +1,10 @@
# PlaguPoster # PlaguPoster
## 0.2.2
* `GarbageCollector`:
* Now on start will all clearing job done
## 0.2.1 ## 0.2.1
* `Versions`: * `Versions`:

View File

@ -10,4 +10,4 @@ android.enableJetifier=true
# Project data # Project data
group=dev.inmo group=dev.inmo
version=0.2.1 version=0.2.2

View File

@ -1,12 +1,15 @@
package dev.inmo.plaguposter.ratings.gc package dev.inmo.plaguposter.ratings.gc
import com.soywiz.klock.DateTime
import com.soywiz.klock.seconds import com.soywiz.klock.seconds
import dev.inmo.krontab.KrontabTemplate import dev.inmo.krontab.KrontabTemplate
import dev.inmo.krontab.toSchedule import dev.inmo.krontab.toSchedule
import dev.inmo.krontab.utils.asFlowWithDelays 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.coroutines.subscribeSafelyWithoutExceptions
import dev.inmo.micro_utils.repos.* import dev.inmo.micro_utils.repos.*
import dev.inmo.plagubot.Plugin import dev.inmo.plagubot.Plugin
import dev.inmo.plaguposter.posts.models.PostId
import dev.inmo.plaguposter.posts.repo.PostsRepo import dev.inmo.plaguposter.posts.repo.PostsRepo
import dev.inmo.plaguposter.ratings.models.Rating import dev.inmo.plaguposter.ratings.models.Rating
import dev.inmo.plaguposter.ratings.repo.RatingsRepo import dev.inmo.plaguposter.ratings.repo.RatingsRepo
@ -41,21 +44,35 @@ object Plugin : Plugin {
val config = koin.get<Config>() val config = koin.get<Config>()
config.immediateDrop ?.let { toDrop -> config.immediateDrop ?.let { toDrop ->
ratingsRepo.onNewValue.subscribeSafelyWithoutExceptions(this) { suspend fun checkAndOptionallyDrop(postId: PostId, rating: Rating) {
if (it.value <= toDrop) { if (rating <= toDrop) {
postsRepo.deleteById(it.id) 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 -> config.autoclear ?.let { autoclear ->
autoclear.autoClearKrontab.toSchedule().asFlowWithDelays().subscribeSafelyWithoutExceptions(scope) { suspend fun doAutoClear() {
val dropCreatedBefore = it - (autoclear.skipPostAge ?: 0).seconds val dropCreatedBefore = DateTime.now() - (autoclear.skipPostAge ?: 0).seconds
ratingsRepo.getPostsWithRatingLessEq(autoclear.rating).keys.forEach { ratingsRepo.getPostsWithRatingLessEq(autoclear.rating).keys.forEach {
if ((postsRepo.getPostCreationTime(it) ?: return@forEach) < dropCreatedBefore) { if ((postsRepo.getPostCreationTime(it) ?: return@forEach) < dropCreatedBefore) {
postsRepo.deleteById(it) postsRepo.deleteById(it)
} }
} }
} }
runCatchingSafely {
doAutoClear()
}
autoclear.autoClearKrontab.toSchedule().asFlowWithDelays().subscribeSafelyWithoutExceptions(scope) {
doAutoClear()
}
} }
} }
} }

View File

@ -12,6 +12,7 @@
"dev.inmo.plaguposter.ratings.Plugin", "dev.inmo.plaguposter.ratings.Plugin",
"dev.inmo.plaguposter.ratings.source.Plugin", "dev.inmo.plaguposter.ratings.source.Plugin",
"dev.inmo.plaguposter.ratings.selector.Plugin", "dev.inmo.plaguposter.ratings.selector.Plugin",
"dev.inmo.plaguposter.ratings.gc.Plugin",
"dev.inmo.plaguposter.triggers.selector_with_timer.Plugin", "dev.inmo.plaguposter.triggers.selector_with_timer.Plugin",
"dev.inmo.plagubot.plugins.inline.queries.Plugin", "dev.inmo.plagubot.plugins.inline.queries.Plugin",
"dev.inmo.plaguposter.triggers.command.Plugin", "dev.inmo.plaguposter.triggers.command.Plugin",
@ -72,5 +73,13 @@
}, },
"publish_command": { "publish_command": {
"panelButtonText": "Publish" "panelButtonText": "Publish"
},
"gc": {
"autoclear": {
"rating": -1,
"autoClearKrontab": "0 0 0 * *",
"skipPostAge": 86400
},
"immediateDrop": -6
} }
} }