Explorar o código

代码块加上try except,并且返回信息加上account_id信息

liyuyi@c-top.com.cn %!s(int64=4) %!d(string=hai) anos
pai
achega
fedf85dfbb
Modificáronse 1 ficheiros con 45 adicións e 41 borrados
  1. 45 41
      ai_strategy_request_func.py

+ 45 - 41
ai_strategy_request_func.py

@@ -1,7 +1,7 @@
 import datetime
 import json
 import uuid
-from logging.handlers import TimedRotatingFileHandler
+import traceback
 from concurrent_log import ConcurrentTimedRotatingFileHandler
 import pandas as pd
 import pymysql
@@ -11,8 +11,6 @@ from utils.UrlConfig import *
 import logging
 
 log_formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s', '%m/%d/%Y %I:%M:%S %p')
-# log_handler = TimedRotatingFileHandler("logs/ai_strategy_request.log",
-#                                        when="midnight", backupCount=100)
 log_handler = ConcurrentTimedRotatingFileHandler("logs/ai_strategy_request.log", when="midnight", backupCount=100)
 log_handler.setFormatter(log_formatter)
 logger = logging.getLogger('ai_strategy_request_logger')
@@ -27,44 +25,50 @@ def ai_strategy_request_parse(data):
     :param data:
     :return:
     """
-    print(data)
-    if data["operation_type"] == 1:
-        inst = ParseAddCampaignOrAddGroupRequest(data)
-        logger.info("账户信息=%s, 当前进程=%s, 当前进程的主进程=%s" % (inst.account_id, os.getpid(), os.getppid()))
-        # 1 获取客户策略信息
-        inst.get_advertiser_strategy_info()
-
-        # 2 写入智能策略信息
-        inst.write_intelligence_strategy_table()
-
-        # 3 判断 campaign_id 是否为空,为空则发送创建计划的请求
-        if inst.campaign_id == "" or inst.campaign_id is None:
-            if inst.add_campaign() == -1:
-                return {'code': -2, 'message': '广告计划创建失败!'}
-
-        # 4 unit_type "4-自定义" or  "7-程序化创意2.0" ,调用对应的拼装参数函数
-        if data['group_info'].get('unit_type', 4) == 4:
-            inst.assemble_group_and_creative_params()
-        if data['group_info'].get('unit_type', 4) == 7:
-            inst.assemble_group_and_programme_creative_params()
-
-        # 5 发送的请求信息更新到数据库表
-        inst.update_intelligence_strategy_table(request_content=inst.res_data, message=None)
-
-        # 6 发送创建组和创意的请求
-        request = requests.post(create_group_and_creative_url,
-                                headers=headers,
-                                data=json.JSONEncoder().encode(inst.res_data))
-        response_data = json.loads(request.text)
-
-        # 7 接受的返回信息更新到数据库表
-        inst.update_intelligence_strategy_table(message=response_data)
 
-        # 8 写入日志信息
-        logger.info("策略uuid = %s 的请求返回信息:%s" % (inst.ai_strategy_uuid, response_data))
-        return {'code': 0, 'message': response_data}
-    else:
-        return {'code': -1, 'message': '暂时不支持非新增的操作请求!'}
+    try:
+        account_id_for_logger = None
+        if data["operation_type"] == 1:
+            inst = ParseAddCampaignOrAddGroupRequest(data)
+            account_id_for_logger = inst.account_id
+            logger.info("账户信息=%s, 当前进程=%s, 当前进程的主进程=%s" % (inst.account_id, os.getpid(), os.getppid()))
+            # 1 获取客户策略信息
+            inst.get_advertiser_strategy_info()
+
+            # 2 写入智能策略信息
+            inst.write_intelligence_strategy_table()
+
+            # 3 判断 campaign_id 是否为空,为空则发送创建计划的请求
+            if inst.campaign_id == "" or inst.campaign_id is None:
+                if inst.add_campaign() == -1:
+                    return {'code': -2, 'message': '广告计划创建失败!'}
+
+            # 4 unit_type "4-自定义" or  "7-程序化创意2.0" ,调用对应的拼装参数函数
+            if data['group_info'].get('unit_type', 4) == 4:
+                inst.assemble_group_and_creative_params()
+            if data['group_info'].get('unit_type', 4) == 7:
+                inst.assemble_group_and_programme_creative_params()
+
+            # 5 发送的请求信息更新到数据库表
+            inst.update_intelligence_strategy_table(request_content=inst.res_data, message=None)
+
+            # 6 发送创建组和创意的请求
+            request = requests.post(create_group_and_creative_url,
+                                    headers=headers,
+                                    data=json.JSONEncoder().encode(inst.res_data))
+            response_data = json.loads(request.text)
+
+            # 7 接受的返回信息更新到数据库表
+            inst.update_intelligence_strategy_table(message=response_data)
+
+            # 8 写入日志信息
+            logger.info("account_id = %s, 策略uuid = %s 的请求返回信息:%s" % (account_id_for_logger, inst.ai_strategy_uuid, response_data))
+            return {'account_id': inst.account_id, 'code': 0, 'message': response_data}
+        else:
+            return {"account_id": account_id_for_logger, "code": -1, "message": "暂时不支持非新增的操作请求!"}
+    except Exception:
+        logger.error("account_id = %s, traceback is %s" % (account_id_for_logger, traceback.format_exc()))
+        return {"account_id": account_id_for_logger, "message": traceback.format_exc(), "code": -2}
 
 
 # 该类解析的是 手动请求的新增广告计划或者新增广告组的数据
@@ -77,7 +81,7 @@ class ParseAddCampaignOrAddGroupRequest(object):
         self.campaign_info = self.request_data['campaign_info']
         self.campaign_id = self.request_data['campaign_info'].get('campaign_id', None)
         self.group_info = self.request_data.get('group_info', None)
-        self.creative_info = self.request_data.get('creative_info',None)
+        self.creative_info = self.request_data.get('creative_info', None)
         self.operation_type = self.request_data['operation_type']
 
         self.advertiser_strategy_id = None