Browse Source

backup reports every 100 hours

sunyj 7 years ago
parent
commit
c54dd8a502

+ 24 - 10
report/src/main/java/com/uas/report/SystemProperties.java

@@ -13,10 +13,16 @@ import org.springframework.context.annotation.Configuration;
 public class SystemProperties {
 
 	/**
-	 * 定时任务间隔时间(毫秒)
+	 * 定时删除临时文件的任务间隔时间(毫秒)
 	 */
-	@Value("${schedule.period}")
-	private int taskPeriod;
+	@Value("${schedule.period.delete-generated-files}")
+	private int deleteGeneratedFilesPeriod;
+
+    /**
+     * 定时备份报表模版的任务间隔时间(毫秒)
+     */
+    @Value("${schedule.period.backup}")
+    private int backupPeriod;
 
 	/**
 	 * 定时任务是否自动开启
@@ -36,15 +42,23 @@ public class SystemProperties {
     @Value("${datasource-default-config}")
 	private String dataSourceDefaultConfig;
 
-	public int getTaskPeriod() {
-		return taskPeriod;
-	}
+    public int getDeleteGeneratedFilesPeriod() {
+        return deleteGeneratedFilesPeriod;
+    }
 
-	public void setTaskPeriod(int taskPeriod) {
-		this.taskPeriod = taskPeriod;
-	}
+    public void setDeleteGeneratedFilesPeriod(int deleteGeneratedFilesPeriod) {
+        this.deleteGeneratedFilesPeriod = deleteGeneratedFilesPeriod;
+    }
+
+    public int getBackupPeriod() {
+        return backupPeriod;
+    }
+
+    public void setBackupPeriod(int backupPeriod) {
+        this.backupPeriod = backupPeriod;
+    }
 
-	public boolean isTaskAutoStart() {
+    public boolean isTaskAutoStart() {
 		return taskAutoStart;
 	}
 

+ 9 - 1
report/src/main/java/com/uas/report/service/FileService.java

@@ -254,7 +254,15 @@ public interface FileService {
 	 * 
 	 * @param taskPeriod
 	 *            定时任务间隔时间(毫秒)
-	 * @return
+	 * @return 定时任务信息
 	 */
 	public TaskInformation newDeleteGeneratedFilesTask(Integer taskPeriod);
+
+    /**
+     * 开启备份模版文件的定时任务
+     *
+     * @param taskPeriod 定时任务间隔时间(毫秒)
+     * @return 定时任务信息
+     */
+    TaskInformation newBackupTask(Integer taskPeriod);
 }

+ 28 - 0
report/src/main/java/com/uas/report/service/impl/FileServiceImpl.java

@@ -709,4 +709,32 @@ public class FileServiceImpl implements FileService {
 		return taskInformation;
 	}
 
+    @Override
+    public TaskInformation newBackupTask(Integer taskPeriod) {
+        if (taskPeriod == null) {
+            throw new NullPointerException();
+        }
+        String title = "定时备份模版文件";
+        Executable command = new Executable() {
+            @Override
+            public String execute() {
+                try {
+                    File reportZipFile = new File(ReportUtils.getBackupDir(), "data.zip");
+                    if(reportZipFile.getParentFile() != null && !reportZipFile.getParentFile().exists()){
+                        reportZipFile.getParentFile().mkdirs();
+                    }
+                    // 先将整个目录压缩,再备份该压缩文件
+                    ZipUtils.zipFolder(dynamicProperties.getLocalBaseDir(), reportZipFile.getPath(), fileFilter);
+                    FileUtils.backup(reportZipFile, 10);
+                } catch (IOException e) {
+                    logger.error("备份失败", e);
+                }
+                return "success";
+            }
+        };
+        TaskInformation taskInformation = new TaskInformation(title, command, INITIAL_DELAY, taskPeriod);
+        taskService.newTask(taskInformation);
+        return taskInformation;
+    }
+
 }

+ 9 - 1
report/src/main/java/com/uas/report/util/ReportUtils.java

@@ -33,7 +33,8 @@ public class ReportUtils {
 		if (systemProperties.isTaskAutoStart()) {
 			FileService fileService = ContextUtils.getBean(FileService.class);
 			TaskService taskService = ContextUtils.getBean(TaskService.class);
-			fileService.newDeleteGeneratedFilesTask(systemProperties.getTaskPeriod());
+			fileService.newDeleteGeneratedFilesTask(systemProperties.getDeleteGeneratedFilesPeriod());
+			fileService.newBackupTask(systemProperties.getBackupPeriod());
 
 			// 开启定时任务
 			if (!taskService.isStopped()) {
@@ -70,4 +71,11 @@ public class ReportUtils {
 	public static String getLibDir() {
 		return getTmpDir() + "/lib";
 	}
+
+    /**
+     * @return 存放 lib 的临时路径
+     */
+    public static String getBackupDir() {
+        return getTmpDir() + "/backup";
+    }
 }

+ 5 - 1
report/src/main/resources/application.yml

@@ -26,7 +26,11 @@ security:
  ignored: false
   
 schedule:
- period: 3600000
+ period:
+#  every 1 hour
+  delete-generated-files: 3600000
+#  every 100 hours
+  backup: 360000000
  auto-start: true
  
 extract-jars: report-common, jasperreports, fastjson