# import threading,json,time
# from concurrent.futures import ThreadPoolExecutor
# import traceback
# import requests
# from rocketmq.client import PushConsumer
# from AutomatedTestPlatform.settings import GROUP_ID,MESSAGE_MODEL,NAME_SERVER_ADDRESS,TOPIC
# pool = ThreadPoolExecutor(5, 'publish')
# import logging
# logger = logging.getLogger('django')
#
#
# class PublishConsumer:
#
#     @staticmethod
#     def consumer(model_config, body):
#         # 消息内容{"projectName":"sms-base-service","moduleName":"sms-base-starter","env":"prod","branch":"master-27839-1654070365-tag","channel":"QIKE","mode":2,"defaultBranch":"prod","emailToUserList":"yanfuchang@qike366.com,chentaolian@qike366.com","publishId":4812,"publishUserName":"liubo"}
#         # 提取消息中的项目名称
#         project_name = body.get('projectName', None)
#         if project_name is None or project_name == '':
#             logger.info(" --- 消息内容不完整, 不处理 --- ")
#         else:
#             logger.info(" --- 消息处理中 --- ")
#             # 根据项目名称,查询任务id
#             api_test_task = model_config['ApiTestPlan'.lower()]
#             task = api_test_task.objects.get(api_test_plan_name=project_name)
#             # 调用当前服务接口
#             headers = {
#                 'Content-Type': 'application/json'
#             }
#             if task is not None:
#                 result = requests.request("POST", "http://localhost:8081/api/backend/api_test_plan/perform_api_test_plan/" + str(task.id) + "/", headers=headers)
#                 logger.info(result)
#
#
# class MQListenerThread(threading.Thread):
#
#     def __init__(self, config):
#         super().__init__()
#         self.consumer = None
#         self.config = config
#
#     def run(self):
#         logger.info(str(threading.Thread.name) + " --- 启动MQ监听线程 --- ")
#         self.consumer = PushConsumer(group_id=GROUP_ID, message_model=MESSAGE_MODEL)
#         self.consumer.set_name_server_address(addr=NAME_SERVER_ADDRESS)
#         self.consumer.subscribe(topic=TOPIC, callback=self.callback)
#         self.consumer.start()
#         while True:
#             time.sleep(3600)  # 一睡就是一天, 保持监听线程不挂掉, 有消息就会持续消费
#
#     def callback(self, msg):
#         try:
#             body = json.loads(str(msg))
#             if not isinstance(body, dict):
#                 logger.info(" 非发布消息格式, 暂不处理! ")
#             else:
#                 # 将消息丢到异步线程处理
#                 # pool.submit(PublishConsumer.consumer, self.config, body)
#                 PublishConsumer.consumer(self.config, body)
#         except Exception:
#             traceback.print_exc()
#             # 无论消息是否消费成功, 都不在处理返回消费成功
#         return ConsumeStatus.CONSUME_SUCCESS
