Browse Source

Merge branch '2024-11-27-forword-response-old-server' of Server/title_with_video into 2024-09-23newDbTasks

luojunhui 4 months ago
parent
commit
f4c1b22a76

+ 7 - 0
applications/const/__init__.py

@@ -0,0 +1,7 @@
+"""
+@author: luojunhui
+"""
+from .server_const import ServerConst
+
+
+server_const = ServerConst()

+ 29 - 0
applications/const/server_const.py

@@ -0,0 +1,29 @@
+"""
+@author: luojunhui
+"""
+
+
+class ServerConst:
+    """
+    服务常量
+    """
+    # 老服务内外公网 ip
+    OLD_SERVER_PUBLIC_IP = "47.99.132.47"
+    OLD_SERVER_INNER_IP = "192.168.205.85"
+
+    # 新服务内外公网 ip
+    NEW_SERVER_PUBLIC_IP = "47.98.154.124"
+    NEW_SERVER_INNER_IP = "192.168.0.142"
+
+    # 服务端口
+    PORT = "8111"
+
+    # RESPONSE 状态
+    REQUEST_INIT_STATUS = 0
+    REQUEST_SUCCESS_STATUS = 1
+    REQUEST_PROCESSING_TASK = 101
+
+    # 任务最大处理次数
+    TASK_MAX_PROCESS_TIMES = 3
+
+

+ 22 - 0
applications/functions/forward_request.py

@@ -0,0 +1,22 @@
+"""
+@author: luojunhui
+"""
+import requests
+
+from applications.const import server_const
+
+
+async def forward_requests(params, api):
+    """
+    转发请求
+    :return:
+    """
+    new_url = "http://{}:{}/{}".format(server_const.OLD_SERVER_INNER_IP, server_const.PORT, api)
+    response = requests.post(
+        url=new_url,
+        headers={
+            "Content-Type": "application/json",
+        },
+        json=params
+    )
+    return response.json()

+ 29 - 2
server/api/get_off_videos.py

@@ -5,6 +5,7 @@ import json
 import time
 
 from applications.log import logging
+from applications.functions.forward_request import forward_requests
 
 
 class GetOffVideos(object):
@@ -87,13 +88,39 @@ class GetOffVideos(object):
                 "msg": "traceId error, can't find trace_id"
             }
 
-    async def deal(self):
+    async def check_trace_id(self):
+        """
+        check trace id 是否存在与系统中
         """
+        select_sql = f"""
+            SELECT trace_id
+            FROM {self.article_match_video_table}
+            WHERE trace_id = '{self.trace_id}';
+            """
+        response = await self.mysql_client.async_select(select_sql)
+        if response:
+            return True
+        else:
+            return False
 
+    async def deal(self):
+        """
         :return:
         """
         params_error = self.check_params()
         if params_error:
             return params_error
         else:
-            return await self.push_video_into_queue()
+            trace_id_exist_flag = await self.check_trace_id()
+            if trace_id_exist_flag:
+                return await self.push_video_into_queue()
+            else:
+                # 只需要传trace_id, 老系统接口不穿strategy参数默认strategy_v1
+                response = await forward_requests(
+                    params={
+                        "traceId": self.trace_id
+                    },
+                    api="get_off_videos"
+                )
+                return response
+

+ 34 - 17
server/api/response.py

@@ -9,16 +9,14 @@ import hashlib
 import urllib.parse
 
 from applications.log import logging
+from applications.const import server_const
+from applications.functions.forward_request import forward_requests
 
 
 class Response(object):
     """
     Response
     """
-    REQUEST_INIT_STATUS = 0
-    REQUEST_SUCCESS_STATUS = 1
-    REQUEST_PROCESSING_TASK = 101
-    TASK_MAX_PROCESS_TIMES = 3
 
     def __init__(self, params, mysql_client, config):
         """
@@ -163,13 +161,6 @@ class Response(object):
                 L = []
                 new_item_list = []
                 for index, item in enumerate(response, 1):
-                    # random_num = random.randint(1, 10)
-                    # if random_num in [1, 2, 3, 4, 5, 6]:
-                    #     long_articles_mini_program_id = 25
-                    # elif random_num in [7, 8]:
-                    #     long_articles_mini_program_id = 29
-                    # else:
-                    #     long_articles_mini_program_id = 31
                     card, new_item = await self.generate_single_card(index, gh_id, long_articles_mini_program_id, item)
                     L.append(card)
                     new_item_list.append(new_item)
@@ -201,7 +192,7 @@ class Response(object):
         process_times = response.get('process_times')
         match status_code:
             case 0:
-                if process_times > self.TASK_MAX_PROCESS_TIMES:
+                if process_times > server_const.TASK_MAX_PROCESS_TIMES:
                     result = {
                         "traceId": self.trace_id,
                         "code": 0,
@@ -242,8 +233,8 @@ class Response(object):
                 affected_rows = await self.mysql_client.async_insert(
                     sql=update_sql,
                     params=(
-                        self.REQUEST_PROCESSING_TASK,
-                        self.REQUEST_INIT_STATUS,
+                        server_const.REQUEST_PROCESSING_TASK,
+                        server_const.REQUEST_INIT_STATUS,
                         self.trace_id
                     )
                 )
@@ -263,9 +254,9 @@ class Response(object):
                     sql=update_sql,
                     params=(
                         json.dumps(new_items, ensure_ascii=False),
-                        self.REQUEST_SUCCESS_STATUS,
+                        server_const.REQUEST_SUCCESS_STATUS,
                         self.trace_id,
-                        self.REQUEST_PROCESSING_TASK
+                        server_const.REQUEST_PROCESSING_TASK
                     )
                 )
                 return {
@@ -303,6 +294,21 @@ class Response(object):
                     "message": "该任务正在执行中"
                 }
 
+    async def check_trace_id(self):
+        """
+        check trace id 是否存在与系统中
+        """
+        select_sql = f"""
+            SELECT trace_id
+            FROM {self.article_match_video_table}
+            WHERE trace_id = '{self.trace_id}';
+            """
+        response = await self.mysql_client.async_select(select_sql)
+        if response:
+            return True
+        else:
+            return False
+
     async def deal(self):
         """
         api process starts from here
@@ -312,4 +318,15 @@ class Response(object):
         if params_error:
             return params_error
         else:
-            return await self.job()
+            trace_id_exist_flag = await self.check_trace_id()
+            if trace_id_exist_flag:
+                return await self.job()
+            else:
+                response = await forward_requests(
+                    params={
+                        "traceId": self.trace_id,
+                        "miniprogramUseType": self.mini_program_type
+                    },
+                    api="recall_videos"
+                )
+                return response