Ver código fonte

get_off_videos.py bugfix
60 行只处理一个 videoId就 return
修改为 一次性插入 3 条视频

罗俊辉 6 meses atrás
pai
commit
e1644d4e7a
2 arquivos alterados com 50 adições e 20 exclusões
  1. 17 0
      applications/db/__init__.py
  2. 33 20
      server/api/get_off_videos.py

+ 17 - 0
applications/db/__init__.py

@@ -69,6 +69,23 @@ class AsyncMySQLClient(object):
                     await coon.rollback()
                     raise
 
+    async def async_insert_many(self, sql, params_list):
+        """
+        :param sql:
+        :param params_list:
+        :return:
+        """
+        async with self.mysql_pool.acquire() as coon:
+            async with coon.cursor() as cursor:
+                try:
+                    await cursor.executemany(sql, params_list)
+                    affected_rows = cursor.rowcount
+                    await coon.commit()
+                    return affected_rows
+                except Exception as e:
+                    await coon.rollback()
+                    raise
+
     async def __aenter__(self):
         await self.init_pool()
         return self

+ 33 - 20
server/api/get_off_videos.py

@@ -4,6 +4,8 @@
 import json
 import time
 
+from applications.log import logging
+
 
 class GetOffVideos(object):
     """
@@ -44,29 +46,40 @@ class GetOffVideos(object):
         result = await self.mysql_client.async_select(sql=select_sql)
         if result:
             video_list = json.loads(result[0][0])
-            for video in video_list:
-                video_id = video['videoId']
-                try:
-                    update_sql = f"""
+            info_list = [
+                (
+                    item['videoId'],
+                    int(time.time()),
+                    1,
+                    self.trace_id
+                )
+                for item in video_list
+            ]
+            try:
+                insert_sql = f"""
                     INSERT INTO {self.get_off_videos}
                     (video_id, publish_time, video_status, trace_id)
                     values 
-                    (%s, %s, %s, %s);
-                    """
-                    await self.mysql_client.async_insert(
-                        sql=update_sql,
-                        params=(video_id, int(time.time()), 1, self.trace_id)
-                    )
-                    return {
-                        "status": "success",
-                        "traceId": self.trace_id
-                    }
-                except Exception as e:
-                    return {
-                        "status": "fail",
-                        "traceId": self.trace_id,
-                        "msg": "insert fail---{}".format(e)
-                    }
+                    (%s, %s, %s, %s);"""
+                await self.mysql_client.async_insert_many(
+                    sql=insert_sql,
+                    params_list=info_list
+                )
+                return {
+                    "status": "success",
+                    "traceId": self.trace_id
+                }
+            except Exception as e:
+                logging(
+                    code="4123",
+                    info="自动下架视频处理失败: {}".format(e),
+                    function="get_off_videos"
+                )
+                return {
+                    "status": "fail",
+                    "traceId": self.trace_id,
+                    "msg": "insert fails, {}".format(e)
+                }
         else:
             return {
                 "status": "fail",