93 lines
3.1 KiB
Java
93 lines
3.1 KiB
Java
package com.kri.notihub.server.scheduler;
|
|
|
|
import java.io.IOException;
|
|
import java.nio.file.DirectoryStream;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.nio.file.Paths;
|
|
import java.nio.file.attribute.BasicFileAttributes;
|
|
import java.time.Duration;
|
|
import java.time.Instant;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
@Slf4j
|
|
@Component
|
|
public class TempFileCleanupScheduler {
|
|
|
|
private final Path uploadBaseDir;
|
|
private final Duration expireDuration;
|
|
|
|
public TempFileCleanupScheduler(
|
|
@Value("${kri.body-image.upload-base-dir}") String uploadBaseDirStr,
|
|
@Value("${kri.body-image.temp-expire-hours:24}") long expireHours
|
|
) {
|
|
this.uploadBaseDir = Paths.get(uploadBaseDirStr);
|
|
this.expireDuration = Duration.ofHours(expireHours);
|
|
}
|
|
|
|
/**
|
|
* 매일 자정(00:00)에 실행. 필요하면 application.yml 에서 kri.body-image.temp-cleanup-cron 으로 변경 가능.
|
|
*/
|
|
@Scheduled(cron = "${kri.body-image.temp-cleanup-cron:0 0 0 * * *}")
|
|
public void cleanupOldTempFiles() {
|
|
if (!Files.isDirectory(uploadBaseDir)) {
|
|
log.warn("[TempCleanup] uploadBaseDir({}) 가 디렉토리가 아닙니다.", uploadBaseDir);
|
|
return;
|
|
}
|
|
|
|
Instant now = Instant.now();
|
|
Instant threshold = now.minus(expireDuration);
|
|
|
|
log.info("[TempCleanup] 시작 - baseDir={}, threshold={}", uploadBaseDir, threshold);
|
|
|
|
try (DirectoryStream<Path> projectDirs = Files.newDirectoryStream(uploadBaseDir)) {
|
|
for (Path projectDir : projectDirs) {
|
|
if (!Files.isDirectory(projectDir)) {
|
|
continue;
|
|
}
|
|
Path tempDir = projectDir.resolve("Temp"); // {prjctCd}/Temp
|
|
if (!Files.isDirectory(tempDir)) {
|
|
continue;
|
|
}
|
|
cleanupTempDir(tempDir, threshold);
|
|
}
|
|
} catch (IOException e) {
|
|
log.error("[TempCleanup] baseDir 스캔 중 오류 - baseDir={}", uploadBaseDir, e);
|
|
}
|
|
|
|
log.info("[TempCleanup] 종료");
|
|
}
|
|
|
|
private void cleanupTempDir(Path tempDir, Instant threshold) {
|
|
try (DirectoryStream<Path> stream = Files.newDirectoryStream(tempDir)) {
|
|
for (Path file : stream) {
|
|
if (!Files.isRegularFile(file)) {
|
|
continue;
|
|
}
|
|
|
|
try {
|
|
BasicFileAttributes attrs =
|
|
Files.readAttributes(file, BasicFileAttributes.class);
|
|
|
|
// 생성 시간(지원 안 하면 마지막 수정 시간)을 기준으로 비교
|
|
Instant created = attrs.creationTime() != null
|
|
? attrs.creationTime().toInstant()
|
|
: attrs.lastModifiedTime().toInstant();
|
|
|
|
if (created.isBefore(threshold)) {
|
|
Files.deleteIfExists(file);
|
|
log.debug("[TempCleanup] 삭제: {} (created={})", file, created);
|
|
}
|
|
} catch (IOException ex) {
|
|
log.warn("[TempCleanup] 파일 삭제 실패: {}", file, ex);
|
|
}
|
|
}
|
|
} catch (IOException e) {
|
|
log.warn("[TempCleanup] temp 디렉토리 스캔 실패: {}", tempDir, e);
|
|
}
|
|
}
|
|
}
|