Explorar o código

删除原始资源定时任务双向锁

dsx %!s(int64=2) %!d(string=hai) anos
pai
achega
9565a6a9de

+ 32 - 0
src/main/java/com/fdkankan/scene/service/impl/SceneCleanOrigServiceImpl.java

@@ -11,6 +11,9 @@ import com.fdkankan.common.constant.CommonSuccessStatus;
 import com.fdkankan.common.constant.OperationType;
 import com.fdkankan.fyun.face.FYunFileServiceInterface;
 import com.fdkankan.model.constants.ConstantFilePath;
+import com.fdkankan.model.utils.SceneUtil;
+import com.fdkankan.redis.constant.RedisKey;
+import com.fdkankan.redis.util.RedisUtil;
 import com.fdkankan.scene.bean.SceneBean;
 import com.fdkankan.scene.entity.SceneCleanOrig;
 import com.fdkankan.scene.mapper.ISceneCleanOrigMapper;
@@ -29,6 +32,7 @@ import sun.font.TextRecord;
 
 import java.util.Date;
 import java.util.List;
+import java.util.Map;
 import java.util.Objects;
 
 /**
@@ -55,6 +59,8 @@ public class SceneCleanOrigServiceImpl extends ServiceImpl<ISceneCleanOrigMapper
     private IScenePlusExtService scenePlusExtService;
     @Autowired
     private FYunFileServiceInterface fYunFileService;
+    @Autowired
+    private RedisUtil redisUtil;
 
     @Override
     public void cleanOrigV4() {
@@ -84,6 +90,10 @@ public class SceneCleanOrigServiceImpl extends ServiceImpl<ISceneCleanOrigMapper
 
     private void cleanOrigHandler(SceneBean scene){
         try {
+            boolean lock = this.lock(scene.getDataSource());
+            if(!lock){
+                throw new RuntimeException("场景正在上传");
+            }
             int successStatus = CommonSuccessStatus.SUCCESS.code();
             String reason = null;
             String dataSource = scene.getDataSource();
@@ -110,6 +120,8 @@ public class SceneCleanOrigServiceImpl extends ServiceImpl<ISceneCleanOrigMapper
         }catch (Exception e){
             log.error("删除原始资源失败,num : " + scene.getNum(), e);
             this.saveLog(scene.getNum(), CommonSuccessStatus.FAIL.code(), ExceptionUtil.stacktraceToString(e, 3000));
+        }finally {
+            this.releaseLock(scene.getDataSource());
         }
     }
 
@@ -126,4 +138,24 @@ public class SceneCleanOrigServiceImpl extends ServiceImpl<ISceneCleanOrigMapper
         this.saveOrUpdate(sceneCleanOrig);
     }
 
+    private boolean lock(String dataSource){
+        Map<String, String> property = SceneUtil.getPropertyFromDataSource(dataSource);
+        String homePath = property.get("homePath");
+        String uuid = property.get("uuid");
+
+        String uploadLock = redisUtil.get(String.format(RedisKey.SCENE_OSS_HOME_DIR_UPLOAD, uuid));
+        //场景正在上传,不删除
+        if(StrUtil.isNotEmpty(uploadLock)){
+            return false;
+        }
+        redisUtil.set(String.format(RedisKey.SCENE_OSS_HOME_DIR_DELETE, uuid), homePath, 8*60*60);
+        return true;
+    }
+
+    private void releaseLock(String dataSource){
+        Map<String, String> property = SceneUtil.getPropertyFromDataSource(dataSource);
+        String uuid = property.get("uuid");
+        redisUtil.del(String.format(RedisKey.SCENE_OSS_HOME_DIR_DELETE, uuid));
+    }
+
 }