瀏覽代碼

迁移场景back优化

lyhzzz 2 年之前
父節點
當前提交
57133c0d5b

+ 3 - 0
src/main/java/com/fdkankan/manage/ManageApplication.java

@@ -21,6 +21,8 @@ public class ManageApplication implements CommandLineRunner {
 
     @Value("${fyun.type}")
     private String uploadType;
+    @Value("${fyun.bucket}")
+    private String bucket;
 
     public static void main(String[] args) {
         SpringApplication.run(ManageApplication.class, args);
@@ -29,6 +31,7 @@ public class ManageApplication implements CommandLineRunner {
     @Override
     public void run(String... args) throws Exception {
         CacheUtil.uploadType = uploadType;
+        CacheUtil.bucket = bucket;
 
     }
 }

+ 1 - 0
src/main/java/com/fdkankan/manage/common/CacheUtil.java

@@ -6,6 +6,7 @@ import com.fdkankan.manage.vo.request.OrderParam;
 public class CacheUtil {
 
     public static String uploadType;
+    public static String bucket;
     public static OrderParam orderParam = new OrderParam();
     public static AgentNewLogParam agentParam = new AgentNewLogParam();
 

+ 13 - 0
src/main/java/com/fdkankan/manage/common/ShellCmd.java

@@ -0,0 +1,13 @@
+package com.fdkankan.manage.common;
+
+public class ShellCmd {
+
+	/**
+	 * oss文件上传命令 bash /opt/ossutil/fyun-upload.sh {bucket} {srcPath} {destPath} {fyunType} {opType}
+	 * opType: file or folder
+	 * fyunType : oss ,aws
+	 */
+	public static final String FYUN_UPLOAD = "sudo bash /opt/ossutil/fyun-upload.sh %s %s /%s %s %s";
+	public static final String FYUN_DOWN = "sudo bash /opt/ossutil/fyun-download.sh %s /%s %s %s %s";
+
+}

+ 127 - 0
src/main/java/com/fdkankan/manage/common/ShellUtil.java

@@ -0,0 +1,127 @@
+package com.fdkankan.manage.common;
+
+import lombok.extern.slf4j.Slf4j;
+
+import java.io.BufferedReader;
+import java.io.Closeable;
+import java.io.IOException;
+import java.io.InputStreamReader;
+
+@Slf4j
+public class ShellUtil {
+
+    /**
+     * 执行系统命令, 返回执行结果
+     * @param cmd 需要执行的命令
+     */
+    public static void execCmd(String cmd) {
+        StringBuilder result = new StringBuilder();
+
+        Process process = null;
+        BufferedReader bufrIn = null;
+        BufferedReader bufrError = null;
+        long startTime = System.currentTimeMillis();
+
+        try {
+            // 执行命令, 返回一个子进程对象(命令在子进程中执行)
+            log.info("执行cmd:{}",cmd);
+            process = Runtime.getRuntime().exec(cmd);
+            // 获取命令执行结果, 有两个结果: 正常的输出 和 错误的输出(PS: 子进程的输出就是主进程的输入)
+            //处理InputStream的线程
+            threadRun(process);
+            // 方法阻塞, 等待命令执行完成(成功会返回0)
+            process.waitFor();
+        }catch (Exception e){
+            e.printStackTrace();
+        }finally {
+            closeStream(bufrIn);
+            closeStream(bufrError);
+            // 销毁子进程
+            if (process != null) {
+                process.destroy();
+            }
+        }
+        // 返回执行结果
+        log.info("执行cmd:{},结果:{},耗时:{}", cmd,result.toString(),System.currentTimeMillis() -startTime);
+    }
+
+    private static void threadRun(Process process) {
+        new Thread() {
+            @Override
+            public void run() {
+                BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
+                String line = null;
+                try {
+                    while((line = in.readLine()) != null) {
+                       log.debug("output: " + line);
+                    }
+                }
+                catch (IOException e) {
+                    e.printStackTrace();
+                }
+                finally {
+                    try {
+                        in.close();
+                    }
+                    catch (IOException e) {
+                        e.printStackTrace();
+                    }
+                }
+            }
+        }.start();
+        new Thread() {
+            @Override
+            public void run() {
+                BufferedReader err = new BufferedReader(new InputStreamReader(process.getErrorStream()));
+                String line = null;
+                try {
+                    while((line = err.readLine()) != null) {
+                        log.debug("err: " + line);
+                    }
+                }
+                catch (IOException e) {
+                    e.printStackTrace();
+                }
+                finally {
+                    try {
+                        err.close();
+                    }
+                    catch (IOException e) {
+                        e.printStackTrace();
+                    }
+                }
+            }
+        }.start();
+    }
+
+    private static void closeStream(Closeable stream) {
+        if (stream != null) {
+            try {
+                stream.close();
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        }
+    }
+
+
+    /**
+     * oss文件上传命令 bash /opt/ossutil/fyun-upload.sh {bucket} {srcPath} {destPath} {fyunType} {opType}
+     * opType: file or folder
+     *  //@param bucket     桶名
+     * @param srcPath    源文件路径
+     * @param destPath   目标文件路径
+     *  //@param fyunType   oss or aws
+     */
+    public static void yunUpload(String srcPath,String destPath){
+        String opType = srcPath.contains(".")? "file":"folder" ;
+        String cmd = String.format(ShellCmd.FYUN_UPLOAD, CacheUtil.bucket,srcPath,destPath,CacheUtil.uploadType,opType);
+        execCmd(cmd);
+    }
+    public static void yunDownload(String srcPath,String destPath){
+        String opType = srcPath.contains(".")? "file":"folder" ;
+        String cmd = String.format(ShellCmd.FYUN_DOWN,CacheUtil.bucket,srcPath,destPath,CacheUtil.uploadType,opType);
+        execCmd(cmd);
+    }
+
+}

+ 46 - 2
src/main/java/com/fdkankan/manage/service/impl/SceneProServiceImpl.java

@@ -446,14 +446,18 @@ public class SceneProServiceImpl extends ServiceImpl<ISceneProMapper, ScenePro>
 
     @Override
     public void updateFdage(String oldSnCode,String newSnCode,String dataSource) {
-        String localPath = String.format(OssPath.localFdagePath,dataSource);
+        String localPath = String.format(OssPath.localFdagePath,dataSource)+"/data.fdage";
         try {
             String fdagePaht =  dataSource.replace("/mnt/data","home") +"/data.fdage";
             String fileContent = fYunFileServiceInterface.getFileContent(fdagePaht);
             String newJson = fileContent.replaceAll("(?i)"+oldSnCode,newSnCode.toLowerCase());
             FileUtils.writeFile(localPath ,newJson);
             log.info("updateFdage--localPath:{},ossPath:{}",localPath,fdagePaht);
-            fYunFileServiceInterface.copyFileInBucket(fdagePaht, fdagePaht+"_"+oldSnCode+"-"+newSnCode+"_"+Dateutils.getDateN(new Date())+".back");
+            String localPathBack = localPath+"_"+oldSnCode+"-"+newSnCode+"_"+Dateutils.getDateN(new Date())+".back";
+            String fdagePathBack = fdagePaht+"_"+oldSnCode+"-"+newSnCode+"_"+Dateutils.getDateN(new Date())+".back";
+
+            fYunFileServiceInterface.downloadFile(fdagePaht,localPathBack);
+            fYunFileServiceInterface.uploadFile(localPathBack,fdagePathBack);
             fYunFileServiceInterface.uploadFile(localPath,fdagePaht);
         }catch (Exception e){
             log.error("updateFdage-error:oldSnCode:{},newSnCode:{},dataSource:{}",oldSnCode,newSnCode,dataSource);
@@ -463,6 +467,46 @@ public class SceneProServiceImpl extends ServiceImpl<ISceneProMapper, ScenePro>
         }
     }
 
+    public void updateFdageNewDataSource(String oldSnCode,String newSnCode,String dataSource) {
+        String localPath = String.format(OssPath.localFdagePath,dataSource);
+        try {
+            String localPathFdage = String.format(OssPath.localFdagePath,dataSource)+"/data.fdage";
+
+            String homeData =  dataSource.replace("/mnt/data","home");
+            ShellUtil.yunDownload(homeData,localPath);
+
+            String fileContent = FileUtils.readFile(localPathFdage);
+            String newJson = fileContent.replaceAll("(?i)"+oldSnCode,newSnCode.toLowerCase());
+            FileUtils.writeFile(localPathFdage ,newJson);
+
+            String oldFdagePaht =  dataSource.replace("/mnt/data","home") ;
+            String newFdagePaht =  oldFdagePaht.replaceAll("(?i)"+oldSnCode,newSnCode.toLowerCase()) ;
+            ShellUtil.yunUpload(localPath,newFdagePaht);
+
+            log.info("updateFdage--localPath:{},ossPath:{}",localPath,newFdagePaht);
+            String newDataSource = dataSource.replaceAll("(?i)"+oldSnCode,newSnCode.toLowerCase()) ;
+            this.updateDataSource(dataSource,newDataSource);
+            fYunFileServiceInterface.deleteFolder(oldFdagePaht);
+        }catch (Exception e){
+            log.error("updateFdage-error:oldSnCode:{},newSnCode:{},dataSource:{}",oldSnCode,newSnCode,dataSource);
+            log.error("updateFdage-error:",e);
+        }finally {
+            FileUtil.del(localPath);
+        }
+    }
+
+    private void updateDataSource(String dataSource, String newDataSource) {
+        LambdaUpdateWrapper<ScenePro> wrapper = new LambdaUpdateWrapper<>();
+        wrapper.eq(ScenePro::getDataSource,dataSource);
+        wrapper.set(ScenePro::getDataSource,newDataSource);
+        this.update(wrapper);
+
+        LambdaUpdateWrapper<ScenePlusExt> wrapperPlus = new LambdaUpdateWrapper<>();
+        wrapperPlus.eq(ScenePlusExt::getDataSource,dataSource);
+        wrapperPlus.set(ScenePlusExt::getDataSource,newDataSource);
+        scenePlusExtService.update(wrapperPlus);
+    }
+
     @Override
     public void copy(String sceneNum) throws Exception {
         ScenePro scenePro = this.getByNum(sceneNum);