Skip to content

Commit

Permalink
Revert "Added filevisitor implementation"
Browse files Browse the repository at this point in the history
This reverts commit 09aa6c6.
  • Loading branch information
chris.ditcher authored and chris.ditcher committed Jul 17, 2023
1 parent 6163c11 commit facad35
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 142 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package ca.bc.gov.educ.api.distribution.config;

import ca.bc.gov.educ.api.distribution.util.DeleteExpiredFilesFileVisitorImpl;
import ca.bc.gov.educ.api.distribution.util.IOUtils;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -12,17 +9,7 @@
import org.springframework.web.reactive.function.client.WebClient;
import reactor.netty.http.client.HttpClient;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.List;

@Configuration
public class EducDistributionApiConfig {
Expand All @@ -48,8 +35,16 @@ public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}

@Bean("TmpCacheFileVisitor")
public FileVisitor<Path> createCleanTmpCacheFilesFileVisitor(@Value("${scheduler.clean-tmp-cache-ignore}") List<String> ignore, @Value("${scheduler.clean-tmp-cache-interval-in-days}") int days) {
return new DeleteExpiredFilesFileVisitorImpl(ignore, LocalDateTime.now().minusDays(days));

@Bean
public FileFilter createFileFilter(){
return pathname -> {
String name = pathname.getName();
return !name.startsWith(".") &&
!name.contains("undertow") &&
!name.contains("hsperfdata");
};
}


}
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
package ca.bc.gov.educ.api.distribution.config;

import ca.bc.gov.educ.api.distribution.util.EducDistributionApiConstants;
import ca.bc.gov.educ.api.distribution.util.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

import java.io.IOException;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.File;
import java.io.FileFilter;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.List;

@Configuration
public class ScheduledTasksConfig {

private static final Logger logger = LoggerFactory.getLogger(ScheduledTasksConfig.class);

EducDistributionApiConstants educDistributionApiConstants;
FileVisitor<Path> fileVisitor;
FileFilter fileFilter;

@Autowired
public ScheduledTasksConfig(@Qualifier("TmpCacheFileVisitor") FileVisitor<Path> fileVisitor, EducDistributionApiConstants educDistributionApiConstants) {
this.fileVisitor = fileVisitor;
this.educDistributionApiConstants = educDistributionApiConstants;
public ScheduledTasksConfig(FileFilter fileFilter) {
this.fileFilter = fileFilter;
}

@Scheduled(cron = "${scheduler.clean-tmp-cache-cron}")
public void cleanTmpCacheFiles() {
Path startingDir = Paths.get(educDistributionApiConstants.getCleanTmpCacheBaseDir());
if(Files.exists(startingDir)){
try {
Files.walkFileTree(startingDir, fileVisitor);
} catch (IOException e) {
logger.error("ScheduledTasksConfig: There was an error removing file cache: {}", e.getLocalizedMessage());
File dir = new File(EducDistributionApiConstants.TMP_DIR);
List<File> files = Arrays.asList(dir.listFiles(this.fileFilter));
LocalDateTime fileExpiry = LocalDateTime.now().minusHours(3);
files.forEach(file -> {
LocalDateTime lastModified = LocalDateTime.ofInstant(Instant.ofEpochMilli(file.lastModified()), ZoneId.systemDefault());
if(lastModified.isBefore(fileExpiry)){
logger.debug("Removing file or directory: {}", file.getName());
IOUtils.removeFileOrDirectory(file);
}
}

});
}


}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Getter
@Setter
Expand All @@ -27,8 +25,6 @@ public class EducDistributionApiConstants {
//Grad2-1931 - mchintha
public static final String DATE_FORMAT_YYYYMMDD = "yyyyMMdd";
public static final String TMP_DIR = "/tmp";

public static final String BATCH_DIR = TMP_DIR + "/Batch";
public static final String DEL = "/";
public static final String FILES_FOLDER_STRUCTURE = "/Batch/PSI/";
public static final String TRANSMISSION_MODE_FTP = "FTP";
Expand Down Expand Up @@ -158,16 +154,4 @@ public class EducDistributionApiConstants {

private int threadPoolMaxSize = 15;

@Value("${scheduler.clean-tmp-cache-cron}")
private String cleanTmpCacheCron;

@Value("${scheduler.clean-tmp-cache-interval-in-days}")
private int cleanTmpCacheIntervalInDays;

@Value("${scheduler.clean-tmp-cache-base-dir}")
private String cleanTmpCacheBaseDir;

@Value("${scheduler.clean-tmp-cache-ignore}")
private List<String> cleanTmpCacheIgnore;

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
Expand Down
7 changes: 0 additions & 7 deletions api/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,4 @@ splunk:

scheduler:
clean-tmp-cache-cron: ${CLEAN_TMP_CACHE_CRON}
clean-tmp-cache-interval-in-days: 5
clean-tmp-cache-base-dir: /tmp/Batch
clean-tmp-cache-ignore:
- batch
- psi
- ftp
- paper

9 changes: 1 addition & 8 deletions api/src/test/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,4 @@ splunk:
enabled: false

scheduler:
clean-tmp-cache-cron: 0 0 */4 * * *
clean-tmp-cache-interval-in-days: 5
clean-tmp-cache-base-dir: /tmp/Batch
clean-tmp-cache-ignore:
- batch
- psi
- ftp
- paper
clean-tmp-cache-cron: 0 0 */4 * * *

0 comments on commit facad35

Please sign in to comment.