ialbert 2026-04-22 13:49:36 +03:00
parent e7bb3ec077
commit 2aa3dc4a80
2 changed files with 22 additions and 7 deletions

View file

@ -3,17 +3,21 @@ package ru.spcex.clearing.csv.importer.component;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Instant;
import java.time.format.DateTimeFormatter;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import ru.spcex.clearing.csv.importer.config.settings.CsvImporterSettings;
import ru.spcex.platform.utils.log.ExceptionUtils;
import ru.spcex.platform.utils.time.TimeUtil;
@Component
public class FileService {
private static final Logger log = LoggerFactory.getLogger(FileService.class);
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy.MM.dd HH.mm.ss");
private final Path outDir;
private final Path errDir;
@ -24,27 +28,33 @@ public class FileService {
checkDir(errDir);
}
public void moveSuccess(Path file) {
public void moveSuccess(Path file, Instant now) {
try {
log.info("moving {} to {}", file, outDir);
Files.move(file, outDir.resolve(file.getFileName()), REPLACE_EXISTING);
String newFileNameWithTimestamp = fileNameWithTimestamp(file, now);
Files.move(file, outDir.resolve(newFileNameWithTimestamp), REPLACE_EXISTING);
} catch (IOException e) {
log.error("failed to move to {}, trying to move to {}: {}", outDir, errDir, ExceptionUtils.getStackTrace(e));
moveError(file);
moveError(file, now);
//notification?
}
}
public void moveError(Path file) {
public void moveError(Path file, Instant now) {
try {
log.info("moving {} to {}", file, errDir);
Files.move(file, errDir.resolve(file.getFileName()), REPLACE_EXISTING);
String newFileNameWithTimestamp = fileNameWithTimestamp(file, now);
Files.move(file, errDir.resolve(newFileNameWithTimestamp), REPLACE_EXISTING);
} catch (IOException e) {
log.error("failed to move to {}: {}", errDir, ExceptionUtils.getStackTrace(e));
//notification?
}
}
public String fileNameWithTimestamp(Path file, Instant now) {
return TimeUtil.formatInstant(now, FORMATTER) + '_' + file.getFileName();
}
public static boolean isReady(Path file) {
try {
var modified = Files.getLastModifiedTime(file).toInstant();

View file

@ -2,6 +2,7 @@ package ru.spcex.clearing.csv.importer.service;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Instant;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -60,6 +61,7 @@ public class SOrdersScheduler {
}
private void processFile(Path path) {
Instant now = Instant.now();
try {
log.info("processing file {}", path);
List<SOrder> sOrders = processor.readFile(path);
@ -71,6 +73,9 @@ public class SOrdersScheduler {
if (order == null) {
order = new SOrders();
order.setId(transId);
order.setCreated(now);
} else {
order.setUpdated(now);
}
order.setState(orderFromFile.getStatus());
order.setOrderNum(orderFromFile.getOrderNumber());
@ -83,10 +88,10 @@ public class SOrdersScheduler {
}
}
sendToDefaultManagement(sOrders);
fileService.moveSuccess(path);
fileService.moveSuccess(path, now);
} catch (Throwable e) {
log.error("error for {}: {}", path, ExceptionUtils.getStackTrace(e));
fileService.moveError(path);
fileService.moveError(path, now);
}
}