瀏覽代碼

feat: 完成中断视频稳相接口

WANGKANG 11 月之前
父節點
當前提交
a031c63afc

+ 2 - 1
taais-admin/src/main/resources/application-dev.yml

@@ -1,5 +1,6 @@
 server:
-  video_stable_url: http://localhost:11001/video_stable
+  video_stable_start_url: http://localhost:11001/video_stable
+  video_stable_stop_url: http://localhost:11001/video_stable_stop
 # 数据源配置
 spring:
   datasource:

+ 5 - 0
taais-modules/taais-biz/src/main/java/com/taais/biz/controller/VideoStableController.java

@@ -105,6 +105,11 @@ public class VideoStableController extends BaseController {
         return videoStableService.start(id);
     }
 
+        @GetMapping("/stop/{id}")
+    public CommonResult stop(@PathVariable("id") Long id) {
+        return videoStableService.stop(id);
+    }
+
     /**
      * 查询视频去抖动列表
      */

+ 23 - 0
taais-modules/taais-biz/src/main/java/com/taais/biz/domain/HttpResponseEntity.java

@@ -0,0 +1,23 @@
+package com.taais.biz.domain;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+/**
+ * @Datetime : 2023/12/5 12:13
+ * @Author : WANGKANG
+ * @Email : 1686617586@qq.com
+ * @File : HttpResponseEntity.java
+ * @Brief :
+ * Copyright 2023 WANGKANG, All Rights Reserved.
+ */
+
+@Data
+@AllArgsConstructor
+@NoArgsConstructor
+public class HttpResponseEntity {
+    private int status; // 200 OK 500 Internal Server Error
+    private String message;
+    private String id; // 唯一标识
+}

+ 2 - 0
taais-modules/taais-biz/src/main/java/com/taais/biz/service/IVideoStableService.java

@@ -70,4 +70,6 @@ public interface IVideoStableService extends IBaseService<VideoStable> {
     CommonResult getResult(VideoStableStartResultBo videoStableStartResultBo);
 
     Object getCompareNum(Long taskId);
+
+    CommonResult stop(Long id);
 }

+ 35 - 12
taais-modules/taais-biz/src/main/java/com/taais/biz/service/impl/VideoStableServiceImpl.java

@@ -13,6 +13,7 @@ import cn.hutool.core.util.ObjectUtil;
 import com.mybatisflex.core.paginate.Page;
 import com.mybatisflex.core.query.QueryWrapper;
 import com.taais.biz.constant.BizConstant;
+import com.taais.biz.domain.HttpResponseEntity;
 import com.taais.biz.domain.bo.VideoStableStartResultBo;
 import com.taais.biz.utils.ZipUtils;
 import com.taais.common.core.config.TaaisConfig;
@@ -37,6 +38,7 @@ import org.apache.http.impl.client.HttpClients;
 import org.apache.http.util.EntityUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.actuate.autoconfigure.observation.ObservationProperties;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import com.taais.biz.mapper.VideoStableMapper;
@@ -56,8 +58,11 @@ import static com.taais.biz.domain.table.VideoStableTableDef.VIDEO_STABLE;
 @Service
 @Slf4j
 public class VideoStableServiceImpl extends BaseServiceImpl<VideoStableMapper, VideoStable> implements IVideoStableService {
-    @Value("${server.video_stable_url}")
-    private String video_stable_url;
+    @Value("${server.video_stable_start_url}")
+    private String video_stable_start_url;
+
+    @Value("${server.video_stable_stop_url}")
+    private String video_stable_stop_url;
 
     @Autowired
     private ISysOssService ossService;
@@ -232,8 +237,8 @@ public class VideoStableServiceImpl extends BaseServiceImpl<VideoStableMapper, V
         videoStable.setStatus(BizConstant.VideoStatus.RUNNING);
         updateById(videoStable);
 
-        boolean flag = sendMsg(videoStable);
-        if (!flag) {
+        HttpResponseEntity responseEntity = sendPostMsg(video_stable_start_url, videoStable);
+        if (responseEntity.getStatus() == 200) {
             videoStable.setStatus(BizConstant.VideoStatus.FAILED);
             updateById(videoStable);
             return CommonResult.fail("start video_stable failed");
@@ -242,25 +247,29 @@ public class VideoStableServiceImpl extends BaseServiceImpl<VideoStableMapper, V
         return CommonResult.success("start video_stable success");
     }
 
-    public boolean sendMsg(VideoStable videoStable) {
-        log.info("start video_stable_url: {}", video_stable_url);
+    public HttpResponseEntity sendPostMsg(String url, Object obj) {
+        log.info("sendMsg: {} - {}", url, JsonUtils.toJsonString(obj));
 
         CloseableHttpClient httpClient = HttpClients.createDefault();
-        HttpPost request = new HttpPost(video_stable_url);
+        HttpPost request = new HttpPost(url);
         // 设置请求体
-        System.out.println(JsonUtils.toJsonString(videoStable));
-        StringEntity entity = new StringEntity(JsonUtils.toJsonString(videoStable), "UTF-8");
+        StringEntity entity = new StringEntity(JsonUtils.toJsonString(obj), "UTF-8");
         entity.setContentType("application/json");
         request.setEntity(entity);
 
         try {
             CloseableHttpResponse response = httpClient.execute(request);
+            if (response.getStatusLine().getStatusCode() != 200) {
+                log.error("sendMsg failed, status code -> " + response.getStatusLine().getStatusCode());
+                return null;
+            }
             String responseBody = EntityUtils.toString(response.getEntity());
             log.info("responseBody -> " + responseBody);
-            return true;
+            // 使用 JsonUtils 将响应体转换为指定的实体类
+            return JsonUtils.parseObject(responseBody, HttpResponseEntity.class);
         } catch (Exception e) {
             log.error("sendMsg error", e);
-            return false;
+            return null;
         }
     }
 
@@ -275,7 +284,7 @@ public class VideoStableServiceImpl extends BaseServiceImpl<VideoStableMapper, V
         videoStable.setEndTime(new Date());
         try {
             videoStable.setCostSecond((videoStable.getEndTime().getTime() - videoStable.getStartTime().getTime()) / 1000);
-        }catch(Exception e) {
+        } catch (Exception e) {
             videoStable.setCostSecond(null);
         }
         updateById(videoStable);
@@ -316,6 +325,20 @@ public class VideoStableServiceImpl extends BaseServiceImpl<VideoStableMapper, V
         return map;
     }
 
+    @Override
+    public CommonResult stop(Long id) {
+        VideoStable videoStable = getById(id);
+
+        HttpResponseEntity responseEntity = sendPostMsg(video_stable_stop_url, videoStable);
+        if (ObjectUtil.isNotNull(responseEntity) && responseEntity.getStatus() == 200) {
+            videoStable.setStatus(BizConstant.VideoStatus.INTERRUPTED);
+            updateById(videoStable);
+            return CommonResult.fail("终止任务成功");
+        } else {
+            return CommonResult.fail("终止任务失败");
+        }
+    }
+
     /**
      * 递归统计文件夹中的文件数量
      *