Skip to content

Commit

Permalink
[Cleanup] Allow to run once before exiting process
Browse files Browse the repository at this point in the history
  • Loading branch information
Schaka committed Oct 18, 2024
1 parent f985465 commit 8ddbb56
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -43,6 +44,7 @@ class MediaCleanupSchedule(

if (!applicationProperties.mediaDeletion.enabled) {
log.info("Media based cleanup disabled, do nothing")
runOnce.hasMediaCleanupRun = true
return
}

Expand All @@ -53,6 +55,8 @@ class MediaCleanupSchedule(

scheduleDelete(TV_SHOWS, seasonExpiration)
scheduleDelete(MOVIES, movieExpiration)

runOnce.hasMediaCleanupRun = true
}

override fun needToDelete(type: LibraryType): Boolean {
Expand All @@ -77,6 +81,4 @@ class MediaCleanupSchedule(
val entry = deletionConditions.entries.filter { freeSpacePercentage < it.key }.minByOrNull { it.key }
return entry?.value
}


}
29 changes: 29 additions & 0 deletions src/main/kotlin/com/github/schaka/janitorr/cleanup/RunOnce.kt
Original file line number Diff line number Diff line change
@@ -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)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -45,6 +44,7 @@ class TagBasedCleanupSchedule(

if (!applicationProperties.tagBasedDeletion.enabled) {
log.info("Tag based cleanup disabled, do nothing")
runOnce.hasTagBasedCleanupRun = true
return
}

Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
) {
Expand All @@ -47,6 +48,7 @@ class WeeklyEpisodeCleanupSchedule(

if (!applicationProperties.episodeDeletion.enabled) {
log.info("Episode based cleanup disabled, do nothing")
runOnce.hasWeeklyEpisodeCleanupRun = true
return
}

Expand Down Expand Up @@ -96,8 +98,7 @@ class WeeklyEpisodeCleanupSchedule(
}
}



runOnce.hasWeeklyEpisodeCleanupRun = true
}

private fun parseDate(date: String): LocalDateTime {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/application-template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 8ddbb56

Please sign in to comment.