mq.py 1.4 KB

1234567891011121314151617181920212223242526272829
  1. import json
  2. from mq_http_sdk.mq_exception import MQExceptionBase
  3. from mq_http_sdk.mq_producer import TopicMessage
  4. from mq_http_sdk.mq_client import MQClient
  5. from common.common import Common
  6. class MQ:
  7. instance_id = "MQ_INST_1894469520484605_BXhXuzkZ"
  8. def __init__(self, topic_name) -> None:
  9. self.mq_client = MQClient("http://1894469520484605.mqrest.cn-qingdao-public.aliyuncs.com", "LTAI4G7puhXtLyHzHQpD6H7A",
  10. "nEbq3xWNQd1qLpdy2u71qFweHkZjSG")
  11. topic_name = topic_name+"_v2"
  12. self.producer = self.mq_client.get_producer(self.instance_id, topic_name)
  13. def send_msg(self, video_dict):
  14. strategy = video_dict["strategy"]
  15. platform = video_dict["platform"]
  16. try:
  17. msg = TopicMessage(json.dumps(video_dict))
  18. message_key = "{}-{}-{}".format(platform, strategy, video_dict['out_video_id'])
  19. # msg.set_message_key(platform + "-" + strategy + "-" + video_dict["out_video_id"])
  20. msg.set_message_key(message_key)
  21. re_msg = self.producer.publish_message(msg)
  22. Common.logger(strategy, platform).info("Publish Message Succeed. MessageID:%s, BodyMD5:%s\n" %
  23. (re_msg.message_id, re_msg.message_body_md5))
  24. except MQExceptionBase as e:
  25. Common.logger(strategy, platform).error("Publish Message Fail. Exception:%s\n" % e)