diff --git a/plume-code-starter/src/main/java/com/plume/code/PlumeCodeApplication.java b/plume-code-starter/src/main/java/com/plume/code/PlumeCodeApplication.java index f35c7badb3228942d83684281cf1559554205f97..aac705a7ad20a2359061ee29047d86f73446224e 100644 --- a/plume-code-starter/src/main/java/com/plume/code/PlumeCodeApplication.java +++ b/plume-code-starter/src/main/java/com/plume/code/PlumeCodeApplication.java @@ -5,11 +5,13 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; +import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, RedisAutoConfiguration.class}) @Slf4j +@EnableScheduling public class PlumeCodeApplication { public static void main(String[] args) { SpringApplication.run(PlumeCodeApplication.class, args); diff --git a/plume-code-starter/src/main/java/com/plume/code/common/bean/ApplicationRunnerImpl.java b/plume-code-starter/src/main/java/com/plume/code/common/bean/ApplicationRunnerImpl.java new file mode 100644 index 0000000000000000000000000000000000000000..b7cf2b7d21f348181697509d6c330de844126b33 --- /dev/null +++ b/plume-code-starter/src/main/java/com/plume/code/common/bean/ApplicationRunnerImpl.java @@ -0,0 +1,36 @@ +package com.plume.code.common.bean; + +import cn.hutool.core.io.FileUtil; +import com.plume.code.common.helper.PathHelper; +import com.plume.code.service.impl.GeneratorServiceImpl; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; +import org.springframework.stereotype.Component; + +import java.io.File; + +@Component +public class ApplicationRunnerImpl implements ApplicationRunner { + private static final Logger logger = LoggerFactory.getLogger(ApplicationRunnerImpl.class); + + @Override + public void run(ApplicationArguments args) { + try { + String downloadPath = PathHelper.getDownloadPath(); +// FileUtil.del(downloadPath);//可以用 但会把download删除 + logger.info("[历史数据清空] 清空文件下载目录开始"); + File downloadDir = new File(downloadPath); + File[] files = downloadDir.listFiles(); + for (File file : files) { + FileUtil.del(file); + } + logger.info("[历史数据清空] 目录已清空 目录: {}", downloadPath); + } catch (Exception e) { + logger.error("[历史数据清空] 异常:{}", e.getMessage()); + } + + } + +} diff --git a/plume-code-starter/src/main/java/com/plume/code/job/ClearHistoryFileTask.java b/plume-code-starter/src/main/java/com/plume/code/job/ClearHistoryFileTask.java new file mode 100644 index 0000000000000000000000000000000000000000..617998cb830683cd69af913c2c1d5922fd815a76 --- /dev/null +++ b/plume-code-starter/src/main/java/com/plume/code/job/ClearHistoryFileTask.java @@ -0,0 +1,31 @@ +package com.plume.code.job; + +import cn.hutool.core.io.FileUtil; +import com.plume.code.common.bean.ApplicationRunnerImpl; +import com.plume.code.common.helper.PathHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +import java.io.File; + +@Component +public class ClearHistoryFileTask { + private static final Logger logger = LoggerFactory.getLogger(ApplicationRunnerImpl.class); + + /** + * 清空历史文件 占机器资源、文件多也不方便查找 + */ + @Scheduled(cron = "0 0 4 * * ?") + public void clearHistoryFile() { + logger.info("[定时任务][历史数据清空] 清空下载目录开始"); + String downloadPath = PathHelper.getDownloadPath(); + File downloadDir = new File(downloadPath); + File[] files = downloadDir.listFiles(); + for (File file : files) { + FileUtil.del(file); + } + logger.info("[定时任务][历史数据清空] 目录已清空 目录: {}", downloadPath); + } +} diff --git a/plume-code-starter/src/main/java/com/plume/code/service/impl/GeneratorServiceImpl.java b/plume-code-starter/src/main/java/com/plume/code/service/impl/GeneratorServiceImpl.java index 7022a28df808f010652799fd863a8d283000da5d..cfd781e6230aa335e862cb0bbb3f6abd5070d434 100644 --- a/plume-code-starter/src/main/java/com/plume/code/service/impl/GeneratorServiceImpl.java +++ b/plume-code-starter/src/main/java/com/plume/code/service/impl/GeneratorServiceImpl.java @@ -12,6 +12,8 @@ import com.plume.code.lib.generator.GeneratorBehavior; import com.plume.code.lib.generator.GeneratorBehaviorFactory; import com.plume.code.service.GeneratorService; import lombok.SneakyThrows; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; @@ -26,7 +28,7 @@ import java.util.stream.Collectors; @Service public class GeneratorServiceImpl implements GeneratorService { - + private static final Logger logger = LoggerFactory.getLogger(GeneratorServiceImpl.class); @Autowired protected DatabaseBehaviorFactory databaseBehaviorFactory; @@ -51,7 +53,9 @@ public class GeneratorServiceImpl implements GeneratorService { throw new FileNotFoundException(directoryPath); } + logger.info("[plume-code] 文件生成完毕 目录:{}", directoryPath); File zip = ZipUtil.zip(directoryPath); + logger.info("[plume-code] 文件zip生成 路径:{}", zip.getAbsolutePath()); return ResultModel.builder() .batchNo(settingModel.getBatchNo())