Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.pepej.truncator.indexer
- import io.ktor.server.application.Application
- import kotlinx.coroutines.delay
- import kotlinx.coroutines.launch
- import org.slf4j.LoggerFactory
- import java.io.File
- import kotlin.time.Duration
- import kotlin.time.Duration.Companion.minutes
- private val log = LoggerFactory.getLogger("LogsUpdater")
- fun Application.logsUpdater(
- folder: String = "mclogs",
- interval: Duration = 30.minutes,
- force: Boolean = false
- ) {
- launch {
- while (true) {
- try {
- log.info("Начинаем автоматическую проверку логов...")
- updateLogs(folder, force = force)
- cleanupLogs(folder)
- log.info("Автоматическое обновление логов завершено.")
- } catch (e: Exception) {
- log.error("Ошибка при автообновлении логов: ${e.message}", e)
- }
- delay(interval)
- }
- }
- }
- fun cleanupLogs(folder: String) {
- val dir = File(folder)
- if (!dir.exists()) return
- dir.listFiles { file ->
- file.isFile && file.extension == "log"
- }?.forEach { file ->
- val deleted = file.delete()
- if (!deleted) {
- log.warn("Не удалось удалить файл: ${file.absolutePath}")
- } else {
- log.info("Удалён файл: ${file.absolutePath}")
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment