Prechádzať zdrojové kódy

优化定时删除推送消息接口

locky 1 rok pred
rodič
commit
3a6023b308
1 zmenil súbory, kde vykonal 55 pridanie a 23 odobranie
  1. 55 23
      Controller/Cron/CronTaskController.py

+ 55 - 23
Controller/Cron/CronTaskController.py

@@ -173,10 +173,9 @@ class CronDelDataView(View):
         except Exception as e:
             return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
 
-    @staticmethod
-    def delPushInfo(response):
+    @classmethod
+    def delPushInfo(cls, response):
         now_time = int(time.time())
-        cursor = connections['mysql02'].cursor()
         try:
             # 当前时间转日期
             local_date_now = str(datetime.datetime.fromtimestamp(int(now_time)).date())
@@ -184,33 +183,66 @@ class CronDelDataView(View):
             week_val = LocalDateTimeUtil.date_to_week(local_date_now)
             # 根据当前时间获取7天前时间戳
             expiration_time = LocalDateTimeUtil.get_before_days_timestamp(now_time, 7)
-            # 每次删除条数
-            size = 10000
-            # 删除7天前的数据
-            sql = "DELETE FROM equipment_info WHERE addTime<= %s LIMIT %s "
-            for i in range(6):
-                cursor.execute(sql, [expiration_time, size])
+
+            # 异步删除推送消息
+            kwargs = {
+                'week_val': week_val,
+                'expiration_time': expiration_time
+            }
+            del_push_info_thread = threading.Thread(
+                target=cls.del_push_info_data,
+                kwargs=kwargs)
+            del_push_info_thread.start()
+            return response.json(0)
+        except Exception as e:
+            return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
+
+    @staticmethod
+    def del_push_info_data(**kwargs):
+        cursor = connections['mysql02'].cursor()
+
+        # 获取删除星期列表
+        week_val = kwargs['week_val']
+        del_week_val_list = [i for i in range(1, 8)]
+        # 移除当天和前后两天
+        del_week_val_list.remove(week_val)
+
+        if week_val == 1:
+            pre_week_val = 7
+        else:
+            pre_week_val = week_val - 1
+        del_week_val_list.remove(pre_week_val)
+
+        if week_val == 7:
+            nex_week_val = 1
+        else:
+            nex_week_val = week_val + 1
+        del_week_val_list.remove(nex_week_val)
+
+        expiration_time = kwargs['expiration_time']
+        # 每次删除条数
+        size = 5000
+        # 删除7天前的数据
+        sql = "DELETE FROM equipment_info WHERE addTime<= %s LIMIT %s "
+        cursor.execute(sql, [expiration_time, size])
+        for week_val in del_week_val_list:
             if week_val == 1:
-                sql = "DELETE FROM equipment_info_sunday WHERE add_time<= %s LIMIT %s "
-            if week_val == 2:
                 sql = "DELETE FROM equipment_info_monday WHERE add_time<= %s LIMIT %s "
-            if week_val == 3:
+            if week_val == 2:
                 sql = "DELETE FROM equipment_info_tuesday WHERE add_time<= %s LIMIT %s "
-            if week_val == 4:
+            if week_val == 3:
                 sql = "DELETE FROM equipment_info_wednesday WHERE add_time<= %s LIMIT %s "
-            if week_val == 5:
+            if week_val == 4:
                 sql = "DELETE FROM equipment_info_thursday WHERE add_time<= %s LIMIT %s "
-            if week_val == 6:
+            if week_val == 5:
                 sql = "DELETE FROM equipment_info_friday WHERE add_time<= %s LIMIT %s "
-            if week_val == 7:
+            if week_val == 6:
                 sql = "DELETE FROM equipment_info_saturday WHERE add_time<= %s LIMIT %s "
-            for i in range(5):
-                cursor.execute(sql, [expiration_time, size])
-            # 关闭游标
-            cursor.close()
-            return response.json(0)
-        except Exception as e:
-            return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
+            if week_val == 7:
+                sql = "DELETE FROM equipment_info_sunday WHERE add_time<= %s LIMIT %s "
+            cursor.execute(sql, [expiration_time, size])
+        # 关闭游标
+        cursor.close()
 
     @staticmethod
     def delVodHls(response):