Explorar o código

异步推送消息和保存数据

locky hai 1 ano
pai
achega
2aab34296b
Modificáronse 3 ficheiros con 44 adicións e 42 borrados
  1. 3 3
      Controller/DetectController.py
  2. 27 24
      Controller/DetectControllerV2.py
  3. 14 15
      Service/DevicePushService.py

+ 3 - 3
Controller/DetectController.py

@@ -122,10 +122,10 @@ class NotificationView(View):
             params = {'nickname': nickname, 'uid': uid, 'kwag_args': kwag_args, 'is_st': is_st,
                       'is_sys_msg': is_sys_msg, 'channel': channel, 'event_type': event_type, 'n_time': n_time,
                       'electricity': '', 'bucket': bucket, 'app_push': '', 'storage_location': 1, 'ai_type': 0,
-                      'dealings_type': 0, 'detection': 0, 'device_type': 1,
-                      'app_push_config': ''}
+                      'dealings_type': 0, 'detection': 0, 'device_type': 1, 'app_push_config': '',
+                      'uid_set_push_list': uid_push_list}
             #  推送以及报警消息存库
-            result = DevicePushService.save_msg_push(uid_set_push_list=uid_push_list, **params)
+            result = DevicePushService.save_msg_push(**params)
             if result['code_date'] is None:
                 result['code_date'] = {'do_apns_code': '', 'do_fcm_code': '', 'do_jpush_code': ''}
             if detect_med_type == 1:

+ 27 - 24
Controller/DetectControllerV2.py

@@ -123,28 +123,26 @@ class NotificationV2View(View):
                       'electricity': electricity, 'bucket': bucket, 'aws_s3_client': aws_s3_client,
                       'app_push': cache_app_push, 'storage_location': 2, 'ai_type': ai_type, 'device_type': device_type,
                       'dealings_type': dealings_type, 'detection': detection,
-                      'app_push_config': uid_set_push_list[0]['uid_set__msg_notify']}
-
-            # 推送消息,生成推送数据列表
-            result = DevicePushService.save_msg_push(uid_set_push_list, **params)
-            # 保存推送数据
-            DevicePushService.save_sys_msg(
-                is_sys_msg,
-                result['local_date_time'],
-                result['sys_msg_list'],
-                result['new_device_info_list'])
+                      'app_push_config': uid_set_push_list[0]['uid_set__msg_notify'],
+                      'uid_set_push_list': uid_set_push_list}
 
             # 异步推送消息和保存数据
-            # push_thread = threading.Thread(
-            #     target=push_and_save_data,
-            #     args=(uid_set_push_list, is_sys_msg),
-            #     kwargs=params)
-            # push_thread.start()
-
-            params['aws_s3_client'] = aws_s3_client
-            params['uid_set_push_list'] = uid_set_push_list
+            push_thread = threading.Thread(
+                target=push_and_save_data,
+                kwargs=params)
+            push_thread.start()
+
+            # 获取S3对象上传链接
+            kwargs = {
+                'is_st': is_st,
+                'uid': uid,
+                'channel': channel,
+                'n_time': n_time,
+                'region': region,
+                'aws_s3_client': aws_s3_client
+            }
+            result_dict = DevicePushService.get_push_url(**kwargs)
 
-            result_dict = DevicePushService.get_push_url(**params)  # 获取S3对象上传链接
             TIME_LOGGER.info('推送响应,uid:{},n_time:{},事件类型:{},响应:{}'.format(
                 uid, n_time, event_type, json.dumps(result_dict)))
             return JsonResponse(status=200, data=result_dict)
@@ -157,13 +155,18 @@ class NotificationV2View(View):
             return JsonResponse(status=200, data=json.dumps(data), safe=False)
 
 
-def push_and_save_data(uid_set_push_list, is_sys_msg, **params):
+def push_and_save_data(**params):
+    TIME_LOGGER.info('{}开始异步推送'.format(params['uid']))
     # 推送消息,生成推送数据列表
-    result = DevicePushService.save_msg_push(uid_set_push_list, **params)
+    result = DevicePushService.save_msg_push(**params)
     # 保存推送数据
-    DevicePushService.save_sys_msg(
-        is_sys_msg,
+    TIME_LOGGER.info('{}开始异步存表'.format(params['uid']))
+    save_success = DevicePushService.save_sys_msg(
+        params['is_sys_msg'],
         result['local_date_time'],
         result['sys_msg_list'],
         result['new_device_info_list'])
-
+    if save_success:
+        TIME_LOGGER.info('{}异步存表成功'.format(params['uid']))
+    else:
+        TIME_LOGGER.info('{}异步存表失败'.format(params['uid']))

+ 14 - 15
Service/DevicePushService.py

@@ -138,14 +138,13 @@ class DevicePushService:
         redis_obj.set_data(key=name, val=1, expire=detect_interval - 5)
 
     @classmethod
-    def save_msg_push(cls, uid_set_push_list, **params):
+    def save_msg_push(cls, **params):
         """
         推送消息,返回推送数据列表
         @param uid_set_push_list: redis对象
         @param params: 推送参数
         @return: dict
         """
-        LOGGING.info('uid_set_push_list:{}'.format(uid_set_push_list))
         new_device_info_list = []
         sys_msg_list = []
         userID_ids = []
@@ -157,7 +156,7 @@ class DevicePushService:
             params['event_tag'] = cls.get_event_tag(params['ai_type'], params['event_type'], params['detection'])
             is_app_push = True if params['event_tag'] in [606, 607] else \
                 cls.is_send_app_push(params['event_type'], params['event_tag'], params['app_push_config'])
-            for up in uid_set_push_list:
+            for up in params['uid_set_push_list']:
                 appBundleId = up['appBundleId']
                 token_val = up['token_val']
                 lang = up['lang']
@@ -632,36 +631,36 @@ class DevicePushService:
             LOGGING.info('图片推送异常,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
 
     @staticmethod
-    def get_push_url(**params):
+    def get_push_url(**kwargs):
         """
         获取推送URL,设备根本当前返回结果进行数据上传
         @return: re_data
         """
         re_data = {'code': 0, 'msg': 'success'}
-        if params['is_st'] == 0 or params['is_st'] == 2:
+        if kwargs['is_st'] == 0 or kwargs['is_st'] == 2:
             re_data['msg'] = 'success 0 or 2'
-            re_data['re_list'] = params['uid_set_push_list']
-        elif params['is_st'] == 1:
+        elif kwargs['is_st'] == 1:
             key_name = '{uid}/{channel}/{filename}.jpeg' \
-                .format(uid=params['uid'], channel=params['channel'], filename=params['n_time'])
+                .format(uid=kwargs['uid'], channel=kwargs['channel'], filename=kwargs['n_time'])
             re_args = {'Key': key_name}
-            if params['region'] == 2:  # 2:国内
+            if kwargs['region'] == 2:  # 2:国内
                 re_args['Bucket'] = 'push'
             else:  # 1:国外
                 re_args['Bucket'] = 'foreignpush'
-            response_url = DevicePushService.generate_s3_url(params['aws_s3_client'], re_args)
+            response_url = DevicePushService.generate_s3_url(kwargs['aws_s3_client'], re_args)
             re_data['img_push'] = response_url
-        elif params['is_st'] == 3:
+            re_data['msg'] = 'success 1'
+        elif kwargs['is_st'] == 3:
             img_url_list = []
-            if params['region'] == 2:  # 2:国内
+            if kwargs['region'] == 2:  # 2:国内
                 re_args = {'Bucket': 'push'}
             else:  # 1:国外
                 re_args = {'Bucket': 'foreignpush'}
-            for i in range(params['is_st']):
+            for i in range(kwargs['is_st']):
                 key_name = '{uid}/{channel}/{filename}_{st}.jpeg'. \
-                    format(uid=params['uid'], channel=params['channel'], filename=params['n_time'], st=i)
+                    format(uid=kwargs['uid'], channel=kwargs['channel'], filename=kwargs['n_time'], st=i)
                 re_args['Key'] = key_name
-                response_url = DevicePushService.generate_s3_url(params['aws_s3_client'], re_args)
+                response_url = DevicePushService.generate_s3_url(kwargs['aws_s3_client'], re_args)
                 img_url_list.append(response_url)
             re_data['img_url_list'] = img_url_list
             re_data['msg'] = 'success 3'