Bladeren bron

定制化异步推送消息

locky 1 jaar geleden
bovenliggende
commit
ac5705a5c1
1 gewijzigde bestanden met toevoegingen van 45 en 18 verwijderingen
  1. 45 18
      Service/CustomizedPushService.py

+ 45 - 18
Service/CustomizedPushService.py

@@ -2,6 +2,7 @@
 # @File      : CustomizedPushService.py
 # @Time      : 2023/10/19 15:49
 import logging
+import threading
 import time
 
 from Model.models import DeviceTypeModel, Device_Info, GatewayPush, CountryModel, SysMsgModel
@@ -79,8 +80,15 @@ class CustomizedPushObject:
         if icon_link is not None:
             icon_link = icon_link.encode('UTF-8', 'ignore').decode('UTF-8')
 
-        # 推送
         n_time = int(time.time())
+        push_kwargs = {
+            'n_time': n_time,
+            'title': title,
+            'msg': msg,
+            'icon_link': icon_link
+        }
+
+        # 推送
         if kwargs['push_app'] == 'ZosiSmart':
             app_bundle_id_list = ['com.ansjer.zccloud_a', 'com.ansjer.zccloud']
         else:
@@ -90,26 +98,43 @@ class CustomizedPushObject:
             gateway_push_qs = GatewayPush.objects.filter(user_id__in=user_id_list, app_bundle_id__in=app_bundle_id_list).\
                 values('user_id', 'app_bundle_id', 'push_type', 'token_val')
             for gateway_push in gateway_push_qs:
-                push_type = gateway_push['push_type']
-                user_id = gateway_push['user_id']
-                app_bundle_id = gateway_push['app_bundle_id']
-                token_val = gateway_push['token_val']
-
-                CUSTOMIZED_PUSH_LOGGER.info('准备推送: {}'.format(user_id))
-                push_succeed = cls.push_msg(push_type, app_bundle_id, token_val, n_time, title, msg, icon_link)
-
-                # 推送成功,写入系统消息
-                if push_succeed:
-                    SysMsgModel.objects.create(
-                        userID_id=user_id, title=title, msg=msg, jumpLink=link, addTime=n_time, updTime=n_time
-                    )
-                    CUSTOMIZED_PUSH_LOGGER.info('用户{}推送成功'.format(user_id))
-                else:
-                    CUSTOMIZED_PUSH_LOGGER.info('用户{}推送失败,push_type:{}'.format(user_id, push_type))
+                # 异步推送消息
+                push_kwargs['gateway_push'] = gateway_push
+                push_thread = threading.Thread(
+                    target=cls.start_push,
+                    kwargs=push_kwargs)
+                push_thread.start()
 
             CUSTOMIZED_PUSH_LOGGER.info('customized_push_id:{}推送完成'.format(kwargs['id']))
         except Exception as e:
-            CUSTOMIZED_PUSH_LOGGER.info('定制化推送异常,error_line:{},error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
+            CUSTOMIZED_PUSH_LOGGER.info('定制化推送或保存数据异常,'
+                                        'error_line:{},error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
+
+    @classmethod
+    def start_push(cls, **kwargs):
+        gateway_push = kwargs['gateway_push']
+        title = kwargs['title']
+        n_time = kwargs['n_time']
+        msg = kwargs['msg']
+        link = kwargs['link']
+        icon_link = kwargs['icon_link']
+
+        push_type = gateway_push['push_type']
+        user_id = gateway_push['user_id']
+        app_bundle_id = gateway_push['app_bundle_id']
+        token_val = gateway_push['token_val']
+
+        CUSTOMIZED_PUSH_LOGGER.info('准备推送: {}'.format(user_id))
+        push_succeed = cls.push_msg(push_type, app_bundle_id, token_val, n_time, title, msg, icon_link)
+
+        # 推送成功,写入系统消息
+        if push_succeed:
+            SysMsgModel.objects.create(
+                userID_id=user_id, title=title, msg=msg, jumpLink=link, addTime=n_time, updTime=n_time
+            )
+            CUSTOMIZED_PUSH_LOGGER.info('用户{}推送成功'.format(user_id))
+        else:
+            CUSTOMIZED_PUSH_LOGGER.info('用户{}推送失败,push_type:{}'.format(user_id, push_type))
 
     @staticmethod
     def push_msg(push_type, app_bundle_id, token_val, n_time, title, msg, icon_link):
@@ -158,4 +183,6 @@ class CustomizedPushObject:
             else:
                 return False
         except Exception as e:
+            CUSTOMIZED_PUSH_LOGGER.info('定制化推送异常,'
+                                        'error_line:{},error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
             return False