فهرست منبع

定时清理过期附件版本

陈长荣 2 ماه پیش
والد
کامیت
fec0c12a01

+ 32 - 0
jfcloud-gene-biz/src/main/java/com/github/jfcloud/gene/flow/service/impl/FlowFileVersionServiceImpl.java

@@ -1,14 +1,21 @@
 package com.github.jfcloud.gene.flow.service.impl;
 
+import cn.hutool.core.date.DateField;
+import cn.hutool.core.date.DateTime;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.github.jfcloud.gene.constants.GeneStatusEnum;
 import com.github.jfcloud.gene.file.vo.FileVo;
 import com.github.jfcloud.gene.flow.entity.FlowFileVersion;
 import com.github.jfcloud.gene.flow.mapper.FlowFileVersionMapper;
 import com.github.jfcloud.gene.flow.service.FlowFileVersionService;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Service;
 
+import java.util.List;
+import java.util.stream.Collectors;
+
 @Slf4j
 @Service
 public class FlowFileVersionServiceImpl extends ServiceImpl<FlowFileVersionMapper, FlowFileVersion> implements FlowFileVersionService {
@@ -26,4 +33,29 @@ public class FlowFileVersionServiceImpl extends ServiceImpl<FlowFileVersionMappe
         }
         return new FileVo(flowFileVersion.getFilename(), flowFileVersion.getFilepath());
     }
+
+
+    /**
+     * 清理过期版本,减少数据库空间
+     */
+    @Scheduled(cron = "0 0 1 * * ?")
+    public void clearExpireVersion() {
+        //查询已完成的项目
+        List<FlowFileVersion> versions = list(new LambdaQueryWrapper<>(FlowFileVersion.class)
+                .select(FlowFileVersion::getFlowId)
+                .eq(FlowFileVersion::getFlowStatus, GeneStatusEnum.COMPLETED.getStatus()));
+        List<Long> flowIdList = versions.stream().map(FlowFileVersion::getFlowId).distinct().collect(Collectors.toList());
+        if (flowIdList.isEmpty()) {
+            return;
+        }
+
+        //清理10天前的项目
+        DateTime beforeTime = new DateTime().offset(DateField.DAY_OF_YEAR, -5);
+
+        remove(new LambdaQueryWrapper<>(FlowFileVersion.class)
+                .in(FlowFileVersion::getFlowId, flowIdList)
+                .lt(FlowFileVersion::getCreateTime, beforeTime)
+                .ne(FlowFileVersion::getFlowStatus, GeneStatusEnum.COMPLETED.getStatus()));
+        log.info("清空项目过期附件版本已完成");
+    }
 }