This commit is contained in:
parent
e7bb3ec077
commit
2aa3dc4a80
2 changed files with 22 additions and 7 deletions
|
|
@ -3,17 +3,21 @@ package ru.spcex.clearing.csv.importer.component;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
|
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import ru.spcex.clearing.csv.importer.config.settings.CsvImporterSettings;
|
import ru.spcex.clearing.csv.importer.config.settings.CsvImporterSettings;
|
||||||
import ru.spcex.platform.utils.log.ExceptionUtils;
|
import ru.spcex.platform.utils.log.ExceptionUtils;
|
||||||
|
import ru.spcex.platform.utils.time.TimeUtil;
|
||||||
|
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class FileService {
|
public class FileService {
|
||||||
private static final Logger log = LoggerFactory.getLogger(FileService.class);
|
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 outDir;
|
||||||
private final Path errDir;
|
private final Path errDir;
|
||||||
|
|
||||||
|
|
@ -24,27 +28,33 @@ public class FileService {
|
||||||
checkDir(errDir);
|
checkDir(errDir);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void moveSuccess(Path file) {
|
public void moveSuccess(Path file, Instant now) {
|
||||||
try {
|
try {
|
||||||
log.info("moving {} to {}", file, outDir);
|
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) {
|
} catch (IOException e) {
|
||||||
log.error("failed to move to {}, trying to move to {}: {}", outDir, errDir, ExceptionUtils.getStackTrace(e));
|
log.error("failed to move to {}, trying to move to {}: {}", outDir, errDir, ExceptionUtils.getStackTrace(e));
|
||||||
moveError(file);
|
moveError(file, now);
|
||||||
//notification?
|
//notification?
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void moveError(Path file) {
|
public void moveError(Path file, Instant now) {
|
||||||
try {
|
try {
|
||||||
log.info("moving {} to {}", file, errDir);
|
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) {
|
} catch (IOException e) {
|
||||||
log.error("failed to move to {}: {}", errDir, ExceptionUtils.getStackTrace(e));
|
log.error("failed to move to {}: {}", errDir, ExceptionUtils.getStackTrace(e));
|
||||||
//notification?
|
//notification?
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String fileNameWithTimestamp(Path file, Instant now) {
|
||||||
|
return TimeUtil.formatInstant(now, FORMATTER) + '_' + file.getFileName();
|
||||||
|
}
|
||||||
|
|
||||||
public static boolean isReady(Path file) {
|
public static boolean isReady(Path file) {
|
||||||
try {
|
try {
|
||||||
var modified = Files.getLastModifiedTime(file).toInstant();
|
var modified = Files.getLastModifiedTime(file).toInstant();
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package ru.spcex.clearing.csv.importer.service;
|
||||||
|
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
import java.time.Instant;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
@ -60,6 +61,7 @@ public class SOrdersScheduler {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void processFile(Path path) {
|
private void processFile(Path path) {
|
||||||
|
Instant now = Instant.now();
|
||||||
try {
|
try {
|
||||||
log.info("processing file {}", path);
|
log.info("processing file {}", path);
|
||||||
List<SOrder> sOrders = processor.readFile(path);
|
List<SOrder> sOrders = processor.readFile(path);
|
||||||
|
|
@ -71,6 +73,9 @@ public class SOrdersScheduler {
|
||||||
if (order == null) {
|
if (order == null) {
|
||||||
order = new SOrders();
|
order = new SOrders();
|
||||||
order.setId(transId);
|
order.setId(transId);
|
||||||
|
order.setCreated(now);
|
||||||
|
} else {
|
||||||
|
order.setUpdated(now);
|
||||||
}
|
}
|
||||||
order.setState(orderFromFile.getStatus());
|
order.setState(orderFromFile.getStatus());
|
||||||
order.setOrderNum(orderFromFile.getOrderNumber());
|
order.setOrderNum(orderFromFile.getOrderNumber());
|
||||||
|
|
@ -83,10 +88,10 @@ public class SOrdersScheduler {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sendToDefaultManagement(sOrders);
|
sendToDefaultManagement(sOrders);
|
||||||
fileService.moveSuccess(path);
|
fileService.moveSuccess(path, now);
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
log.error("error for {}: {}", path, ExceptionUtils.getStackTrace(e));
|
log.error("error for {}: {}", path, ExceptionUtils.getStackTrace(e));
|
||||||
fileService.moveError(path);
|
fileService.moveError(path, now);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue