From 5a148e5e6241e89983f18c42c735024c0bd4135a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E6=96=B0=E6=B5=B7?= Date: Sat, 24 Jul 2021 19:18:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8A=9F=E8=83=BD=20?= =?UTF-8?q?=E6=B8=85=E7=A9=BA=E5=8E=86=E5=8F=B2=E4=B8=8B=E8=BD=BD=E6=96=87?= =?UTF-8?q?=E4=BB=B6=20=E3=80=82=E5=9C=A8=E9=A1=B9=E7=9B=AE=E5=90=AF?= =?UTF-8?q?=E5=8A=A8=E6=88=90=E5=8A=9F=E5=92=8C=E5=AE=9A=E6=97=B6=E6=B8=85?= =?UTF-8?q?=E7=A9=BA=20=E5=8E=86=E5=8F=B2=E6=96=87=E4=BB=B6=E5=8D=A0?= =?UTF-8?q?=E6=9C=BA=E5=99=A8=E8=B5=84=E6=BA=90=E3=80=81=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=A4=9A=E4=B9=9F=E4=B8=8D=E6=96=B9=E4=BE=BF=E6=9F=A5=E6=89=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/plume/code/PlumeCodeApplication.java | 2 ++ .../common/bean/ApplicationRunnerImpl.java | 36 +++++++++++++++++++ .../plume/code/job/ClearHistoryFileTask.java | 31 ++++++++++++++++ .../service/impl/GeneratorServiceImpl.java | 6 +++- 4 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 plume-code-starter/src/main/java/com/plume/code/common/bean/ApplicationRunnerImpl.java create mode 100644 plume-code-starter/src/main/java/com/plume/code/job/ClearHistoryFileTask.java 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 f35c7ba..aac705a 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 0000000..b7cf2b7 --- /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 0000000..617998c --- /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 7022a28..cfd781e 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()) -- Gitee