260212_01_DOC: 프로젝트 전체에 구글 java 스타일 적용
This commit is contained in:
@@ -1,91 +1,92 @@
|
||||
package com.kri.contact.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;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.*;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class TempFileCleanupScheduler {
|
||||
|
||||
private final Path uploadBaseDir;
|
||||
private final Duration expireDuration;
|
||||
private final Path uploadBaseDir;
|
||||
private final Duration expireDuration;
|
||||
|
||||
public TempFileCleanupScheduler(
|
||||
@Value("${kri.contact-senario.upload-base-dir}") String uploadBaseDirStr,
|
||||
@Value("${kri.contact-senario.temp-expire-hours:24}") long expireHours
|
||||
) {
|
||||
this.uploadBaseDir = Paths.get(uploadBaseDirStr);
|
||||
this.expireDuration = Duration.ofHours(expireHours);
|
||||
public TempFileCleanupScheduler(
|
||||
@Value("${kri.contact-senario.upload-base-dir}") String uploadBaseDirStr,
|
||||
@Value("${kri.contact-senario.temp-expire-hours:24}") long expireHours
|
||||
) {
|
||||
this.uploadBaseDir = Paths.get(uploadBaseDirStr);
|
||||
this.expireDuration = Duration.ofHours(expireHours);
|
||||
}
|
||||
|
||||
/**
|
||||
* 매일 자정(00:00)에 실행. 필요하면 application.yml 에서 kri.contact-senario.temp-cleanup-cron 으로 변경 가능.
|
||||
*/
|
||||
@Scheduled(cron = "${kri.contact-senario.temp-cleanup-cron:0 0 0 * * *}")
|
||||
public void cleanupOldTempFiles() {
|
||||
if (!Files.isDirectory(uploadBaseDir)) {
|
||||
log.warn("[TempCleanup] uploadBaseDir({}) 가 디렉토리가 아닙니다.", uploadBaseDir);
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* 매일 자정(00:00)에 실행.
|
||||
* 필요하면 application.yml 에서 kri.contact-senario.temp-cleanup-cron 으로 변경 가능.
|
||||
*/
|
||||
@Scheduled(cron = "${kri.contact-senario.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;
|
||||
}
|
||||
|
||||
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);
|
||||
Path tempDir = projectDir.resolve("Temp"); // {prjctCd}/Temp
|
||||
if (!Files.isDirectory(tempDir)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
log.info("[TempCleanup] 종료");
|
||||
cleanupTempDir(tempDir, threshold);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error("[TempCleanup] baseDir 스캔 중 오류 - baseDir={}", uploadBaseDir, e);
|
||||
}
|
||||
|
||||
private void cleanupTempDir(Path tempDir, Instant threshold) {
|
||||
try (DirectoryStream<Path> stream = Files.newDirectoryStream(tempDir)) {
|
||||
for (Path file : stream) {
|
||||
if (!Files.isRegularFile(file)) {
|
||||
continue;
|
||||
}
|
||||
log.info("[TempCleanup] 종료");
|
||||
}
|
||||
|
||||
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);
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user