pepejik

Untitled

Feb 21st, 2025
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.48 KB | None | 0 0
  1. package com.pepej.truncator.indexer
  2.  
  3. import io.ktor.server.application.Application
  4. import kotlinx.coroutines.delay
  5. import kotlinx.coroutines.launch
  6. import org.slf4j.LoggerFactory
  7. import java.io.File
  8. import kotlin.time.Duration
  9. import kotlin.time.Duration.Companion.minutes
  10.  
  11. private val log = LoggerFactory.getLogger("LogsUpdater")
  12.  
  13.  
  14. fun Application.logsUpdater(
  15.     folder: String = "mclogs",
  16.     interval: Duration = 30.minutes,
  17.     force: Boolean = false
  18. ) {
  19.     launch {
  20.         while (true) {
  21.             try {
  22.                 log.info("Начинаем автоматическую проверку логов...")
  23.                 updateLogs(folder, force = force)
  24.  
  25.                 cleanupLogs(folder)
  26.  
  27.                 log.info("Автоматическое обновление логов завершено.")
  28.             } catch (e: Exception) {
  29.                 log.error("Ошибка при автообновлении логов: ${e.message}", e)
  30.             }
  31.             delay(interval)
  32.         }
  33.     }
  34. }
  35.  
  36. fun cleanupLogs(folder: String) {
  37.     val dir = File(folder)
  38.     if (!dir.exists()) return
  39.  
  40.     dir.listFiles { file ->
  41.         file.isFile && file.extension == "log"
  42.     }?.forEach { file ->
  43.         val deleted = file.delete()
  44.         if (!deleted) {
  45.             log.warn("Не удалось удалить файл: ${file.absolutePath}")
  46.         } else {
  47.             log.info("Удалён файл: ${file.absolutePath}")
  48.         }
  49.     }
  50. }
  51.  
Advertisement
Add Comment
Please, Sign In to add comment