From 8ddbb56f518082527a704285aefff9866ea0450f Mon Sep 17 00:00:00 2001 From: Schaka <2223171+Schaka@users.noreply.github.com> Date: Fri, 18 Oct 2024 22:12:11 +0200 Subject: [PATCH] [Cleanup] Allow to run once before exiting process --- .../cleanup/AbstractCleanupSchedule.kt | 1 + .../janitorr/cleanup/MediaCleanupSchedule.kt | 8 +++-- .../github/schaka/janitorr/cleanup/RunOnce.kt | 29 +++++++++++++++++++ .../cleanup/TagBasedCleanupSchedule.kt | 12 ++++---- .../cleanup/WeeklyEpisodeCleanupSchedule.kt | 5 ++-- .../janitorr/config/ApplicationProperties.kt | 1 + src/main/resources/application-template.yml | 1 + 7 files changed, 47 insertions(+), 10 deletions(-) create mode 100644 src/main/kotlin/com/github/schaka/janitorr/cleanup/RunOnce.kt diff --git a/src/main/kotlin/com/github/schaka/janitorr/cleanup/AbstractCleanupSchedule.kt b/src/main/kotlin/com/github/schaka/janitorr/cleanup/AbstractCleanupSchedule.kt index d92ed9e..1a19afd 100644 --- a/src/main/kotlin/com/github/schaka/janitorr/cleanup/AbstractCleanupSchedule.kt +++ b/src/main/kotlin/com/github/schaka/janitorr/cleanup/AbstractCleanupSchedule.kt @@ -22,6 +22,7 @@ abstract class AbstractCleanupSchedule( protected val jellystatService: JellystatService, protected val fileSystemProperties: FileSystemProperties, protected val applicationProperties: ApplicationProperties, + protected val runOnce: RunOnce, protected val sonarrService: ServarrService, protected val radarrService: ServarrService, ) { diff --git a/src/main/kotlin/com/github/schaka/janitorr/cleanup/MediaCleanupSchedule.kt b/src/main/kotlin/com/github/schaka/janitorr/cleanup/MediaCleanupSchedule.kt index a642ee3..2329e04 100644 --- a/src/main/kotlin/com/github/schaka/janitorr/cleanup/MediaCleanupSchedule.kt +++ b/src/main/kotlin/com/github/schaka/janitorr/cleanup/MediaCleanupSchedule.kt @@ -27,9 +27,10 @@ class MediaCleanupSchedule( jellystatService: JellystatService, fileSystemProperties: FileSystemProperties, applicationProperties: ApplicationProperties, + runOnce: RunOnce, @Sonarr sonarrService: ServarrService, @Radarr radarrService: ServarrService, -) : AbstractCleanupSchedule(CleanupType.MEDIA, mediaServerService, jellyseerrService, jellystatService, fileSystemProperties, applicationProperties, sonarrService, radarrService) { +) : AbstractCleanupSchedule(CleanupType.MEDIA, mediaServerService, jellyseerrService, jellystatService, fileSystemProperties, applicationProperties, runOnce, sonarrService, radarrService) { companion object { private val log = LoggerFactory.getLogger(this::class.java.enclosingClass) @@ -43,6 +44,7 @@ class MediaCleanupSchedule( if (!applicationProperties.mediaDeletion.enabled) { log.info("Media based cleanup disabled, do nothing") + runOnce.hasMediaCleanupRun = true return } @@ -53,6 +55,8 @@ class MediaCleanupSchedule( scheduleDelete(TV_SHOWS, seasonExpiration) scheduleDelete(MOVIES, movieExpiration) + + runOnce.hasMediaCleanupRun = true } override fun needToDelete(type: LibraryType): Boolean { @@ -77,6 +81,4 @@ class MediaCleanupSchedule( val entry = deletionConditions.entries.filter { freeSpacePercentage < it.key }.minByOrNull { it.key } return entry?.value } - - } \ No newline at end of file diff --git a/src/main/kotlin/com/github/schaka/janitorr/cleanup/RunOnce.kt b/src/main/kotlin/com/github/schaka/janitorr/cleanup/RunOnce.kt new file mode 100644 index 0000000..8fe6cb7 --- /dev/null +++ b/src/main/kotlin/com/github/schaka/janitorr/cleanup/RunOnce.kt @@ -0,0 +1,29 @@ +package com.github.schaka.janitorr.cleanup + +import com.github.schaka.janitorr.config.ApplicationProperties +import org.slf4j.LoggerFactory +import org.springframework.scheduling.annotation.Scheduled +import org.springframework.stereotype.Component +import kotlin.system.exitProcess + +@Component +class RunOnce( + val applicationProperties: ApplicationProperties, + var hasMediaCleanupRun: Boolean, + var hasTagBasedCleanupRun: Boolean, + var hasWeeklyEpisodeCleanupRun: Boolean +) { + + companion object { + private val log = LoggerFactory.getLogger(this::class.java.enclosingClass) + } + + @Scheduled(fixedDelay = 1000) + fun run() { + if (applicationProperties.runOnce && hasMediaCleanupRun && hasTagBasedCleanupRun && hasWeeklyEpisodeCleanupRun) { + log.info("Run once enabled, all cleanups run. Terminating process.") + exitProcess(0) + } + } + +} \ No newline at end of file diff --git a/src/main/kotlin/com/github/schaka/janitorr/cleanup/TagBasedCleanupSchedule.kt b/src/main/kotlin/com/github/schaka/janitorr/cleanup/TagBasedCleanupSchedule.kt index 260cfc5..84bee94 100644 --- a/src/main/kotlin/com/github/schaka/janitorr/cleanup/TagBasedCleanupSchedule.kt +++ b/src/main/kotlin/com/github/schaka/janitorr/cleanup/TagBasedCleanupSchedule.kt @@ -28,11 +28,10 @@ class TagBasedCleanupSchedule( jellystatService: JellystatService, fileSystemProperties: FileSystemProperties, applicationProperties: ApplicationProperties, - @Sonarr - sonarrService: ServarrService, - @Radarr - radarrService: ServarrService, -) : AbstractCleanupSchedule(CleanupType.TAG, mediaServerService, jellyseerrService, jellystatService, fileSystemProperties, applicationProperties, sonarrService, radarrService) { + runOnce: RunOnce, + @Sonarr sonarrService: ServarrService, + @Radarr radarrService: ServarrService, +) : AbstractCleanupSchedule(CleanupType.TAG, mediaServerService, jellyseerrService, jellystatService, fileSystemProperties, applicationProperties, runOnce, sonarrService, radarrService) { companion object { private val log = LoggerFactory.getLogger(this::class.java.enclosingClass) @@ -45,6 +44,7 @@ class TagBasedCleanupSchedule( if (!applicationProperties.tagBasedDeletion.enabled) { log.info("Tag based cleanup disabled, do nothing") + runOnce.hasTagBasedCleanupRun = true return } @@ -60,6 +60,8 @@ class TagBasedCleanupSchedule( scheduleDelete(TV_SHOWS, tag.expiration, entryFilter = { item -> tagMatches(item, tag) }, true) scheduleDelete(MOVIES, tag.expiration, entryFilter = { item -> tagMatches(item, tag) }, true) } + + runOnce.hasTagBasedCleanupRun = true } private fun tagMatches(item: LibraryItem, tag: TagDeleteSchedule): Boolean { diff --git a/src/main/kotlin/com/github/schaka/janitorr/cleanup/WeeklyEpisodeCleanupSchedule.kt b/src/main/kotlin/com/github/schaka/janitorr/cleanup/WeeklyEpisodeCleanupSchedule.kt index 0f1e363..2aa60bd 100644 --- a/src/main/kotlin/com/github/schaka/janitorr/cleanup/WeeklyEpisodeCleanupSchedule.kt +++ b/src/main/kotlin/com/github/schaka/janitorr/cleanup/WeeklyEpisodeCleanupSchedule.kt @@ -26,6 +26,7 @@ class WeeklyEpisodeCleanupSchedule( val applicationProperties: ApplicationProperties, val sonarrProperties: SonarrProperties, val sonarrClient: SonarrClient, + val runOnce: RunOnce, var episodeTag: Tag = Tag(Integer.MIN_VALUE, "Not_Set") ) { @@ -47,6 +48,7 @@ class WeeklyEpisodeCleanupSchedule( if (!applicationProperties.episodeDeletion.enabled) { log.info("Episode based cleanup disabled, do nothing") + runOnce.hasWeeklyEpisodeCleanupRun = true return } @@ -96,8 +98,7 @@ class WeeklyEpisodeCleanupSchedule( } } - - + runOnce.hasWeeklyEpisodeCleanupRun = true } private fun parseDate(date: String): LocalDateTime { diff --git a/src/main/kotlin/com/github/schaka/janitorr/config/ApplicationProperties.kt b/src/main/kotlin/com/github/schaka/janitorr/config/ApplicationProperties.kt index ca544a6..6353a3e 100644 --- a/src/main/kotlin/com/github/schaka/janitorr/config/ApplicationProperties.kt +++ b/src/main/kotlin/com/github/schaka/janitorr/config/ApplicationProperties.kt @@ -12,6 +12,7 @@ data class ApplicationProperties( val tagBasedDeletion: TagDeletion, @NestedConfigurationProperty val episodeDeletion: EpisodeDeletion, + val runOnce: Boolean = false, val dryRun: Boolean = false, val wholeTvShow: Boolean = false, val wholeShowSeedingCheck: Boolean = false, diff --git a/src/main/resources/application-template.yml b/src/main/resources/application-template.yml index b8c23db..eab1b8b 100644 --- a/src/main/resources/application-template.yml +++ b/src/main/resources/application-template.yml @@ -14,6 +14,7 @@ file-system: application: dry-run: true + run-once: false # If you enable this, Janitorr will clean up once and then shut down. whole-tv-show: false # activating this will treat as a whole show as recently download/watched from a single episode, rather than that episode's season - shows will be deleted as a whole whole-show-seeding-check: false # Turning this off, disables the seeding check entirely if whole-tv-show is enabled. Activating this check will keep a whole TV show if any season is still seeding (requires file access). leaving-soon: 14d # 14 days before a movie is deleted, it gets added to a "Leaving Soon" type collection (i.e. movies that are 76 to 89 days old)