def commit_book_parts(self): params = self.body uid = self.get_user_session_id(params['session_id']) book_owner_id = MyAes().decrypt(params['token']) images_str = '|'.join(self.body['event_images']) sql_insert = "INSERT INTO book_parts (book_owner_id, editor_id, event_date, event_address, " \ "event_description, event_images) VALUES ('%d', '%d', '%s', '%s', '%s', '%s')" % \ (int(book_owner_id), int(uid), params['event_date'], params['event_address'], params['event_description'], images_str) try: self.cursor.execute(sql_insert) self.db.commit() # cmq todo endpoint = self.config['cmq']['request_url'] try: my_account = Account(endpoint, self.config['cos']['secret_id'], self.config['cos']['secret_key'], debug=True) my_account.set_log_level(logging.DEBUG) topic_name = sys.argv[1] if len(sys.argv) > 1 else "lifebook" my_topic = my_account.get_topic(topic_name) self.cursor.execute( "select id,open_id,nickname from fans where id = %s", int(book_owner_id)) u_info = self.cursor.fetchone() message_body = { "openid": u_info['open_id'], "nickname": u_info['nickname'], "created_at": params['event_date'] } message = Message() message.msgBody = json.dumps(message_body) my_topic.publish_message(message) except CMQExceptionBase as e: print("cmqException:%s\n" % e) return self.public_return(True, 'success', {}) except Exception: # self.db.rollback() raise InfoException()
def Send_CMQ(body): # 初始化 my_account my_account = Account(endpoint, secret_id, secret_key, debug=True) my_account.set_log_level(logging.DEBUG) my_topic = my_account.get_topic(topic_name) for toAddr in email_notify_list: try: message = Message() sendbody = { "fromAddr": fromAddr, "toAddr": toAddr, "title": u"Please note: PlayCheck Error 拨测地址异常,请检查", "body": body } message.msgBody = json.dumps(sendbody) print("send message [%s] to [%s]" % (body, toAddr)) my_topic.publish_message(message) except CMQExceptionBase, e: print "Exception:%s\n" % e
def __init__(self, _path): with open(_path, 'r') as f: self._data = json.load(f) self._actions = self._data['actions'] secretId = '' secretKey = '' # 使用广州地域的消息服务 endpoint = 'http://cmq-queue-gz.api.qcloud.com' # 初始化 my_account, my_queue # Account类对象不是线程安全的,如果多线程使用,需要每个线程单独初始化Account类对象 my_account = Account(endpoint, secretId, secretKey, debug=True) my_account.set_log_level(logging.DEBUG) queue_name = sys.argv[1] if len(sys.argv) > 1 else "MySampleQueue" self.my_queue = my_account.get_queue(queue_name) queue_meta = QueueMeta() queue_meta.queueName = queue_name queue_meta.pollingWaitSeconds = 10 queue_meta.visibilityTimeout = 10 queue_meta.maxMsgSize = 1024 queue_meta.msgRetentionSeconds = 3600
import logging from cmq.account import Account from cmq.queue import QueueMeta, Message from cmq.cmq_exception import CMQExceptionBase # 从腾讯云官网查看云api的密钥信息 secretId = 'AKID2ua4MRFxPerreERnlW5ScZKsDakhMgdT' secretKey = 'oQqEbtyoY6OPJ5lLlvrXx6D8xa3KYeia' # 使用广州地域的消息服务 endpoint = 'https://cmq-queue-sh.api.qcloud.com' # 初始化 my_account, my_queue # Account类对象不是线程安全的,如果多线程使用,需要每个线程单独初始化Account类对象 my_account = Account(endpoint, secretId, secretKey, debug=True) my_account.set_log_level(logging.DEBUG) queue_name = sys.argv[1] if len(sys.argv) > 1 else "MySampleQueue" my_queue = my_account.get_queue(queue_name) # 创建队列, 具体属性请参考cmq/queue.py中的QueueMeta结构 queue_meta = QueueMeta() queue_meta.queueName = queue_name queue_meta.pollingWaitSeconds = 10 queue_meta.visibilityTimeout = 10 queue_meta.maxMsgSize = 1024 queue_meta.msgRetentionSeconds = 3600 try: my_queue.create(queue_meta) print "Create Queue Succeed! QueueName:%s\n" % (queue_name) totalCount, queueList, next_offset= my_account.list_queue()