class TaskLogCleanup extends ComponentSupport {

  void cleanup() {
    String taskLogsHome = getTaskLogHome();

    if (taskLogsHome == null) {
      // we are forgiving if the task logs home is not defined. Just log a message with a call to action.
      log.warn("Unable to cleanup task log files. Please check that the 'tasklogfile' appender exists in logback.xml");
      return;
    }

    File logFilesHome = new File(taskLogsHome);

    log.info("Cleaning up log files in {} older than {} days", logFilesHome.getAbsolutePath(), numberOfDays);

    LocalDate now = LocalDate.now().minusDays(numberOfDays);
    Date thresholdDate = Date.from(now.atStartOfDay(ZoneId.systemDefault()).toInstant());
    AgeFileFilter ageFileFilter = new AgeFileFilter(thresholdDate);
    Iterator<File> filesToDelete = iterateFiles(logFilesHome, ageFileFilter, ageFileFilter);
    filesToDelete.forEachRemaining(f -> {
          try {
            forceDelete(f);
            log.info("Removed task log file {}", f.toString());
          }
          catch (IOException e) { // NOSONAR
            log.error("Unable to delete task file {}. Message was {}.", f.toString(), e.getMessage());
          }
        }
    );
  }

  @Inject public  TaskLogCleanup(@Named("${nexus.tasks.log.cleanup.numberOfDays:-30}") final Integer numberOfDays);

}