示例#1
0
    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()
示例#2
0
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
示例#3
0
 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
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)) + "/..")

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)
示例#5
0
def sign_task_post(check_time=None):
    conn = app.mysql_pool.connection()
    admin_token = request.args.get("token", "")
    verify_token = Kit.get_key_val(conn, "token")
    if admin_token != verify_token:
        return abort(400)

    sms_control = request.args.get("sms", "Yes")
    if check_time is None:
        time_now = Kit.str_time("%H:%M")
    else:
        unix_time = Kit.timestamp2unix("2020-01-01 " + check_time + ":00")
        time_now = Kit.unix2timestamp(unix_time, pattern="%H:%M")
    app.logger.info("Service check time {}".format(time_now))

    # 连接至数据库
    cursor = conn.cursor(pymysql.cursors.DictCursor)

    if time_now.endswith(":00"):
        # 读取本时间段内随机时间用户
        sql = "SELECT * FROM `user` WHERE `online`='Yes' AND `rand`='Yes' AND `time` LIKE %s"
        cursor.execute(sql, args=[time_now.split(":")[0] + ":%"])
        user_list = cursor.fetchall()
    else:
        # 读取时间点需要打卡用户
        sql = "SELECT * FROM `user` WHERE `online`='Yes' AND `rand`='No' AND `time`=%s"
        cursor.execute(sql, args=[time_now])
        user_list = cursor.fetchall()

    # 连接至消息队列中间件
    user_config = {
        "host": app.config["CMQ"]["endpoint"],
        "secretId": app.config["CMQ"]["secret_id"],
        "secretKey": app.config["CMQ"]["secret_key"],
        "debug": False,
    }

    # 计算时间延迟并发布任务
    for count in range(3):
        retry = False
        queue_client = CMQ_Account(**user_config)
        queue_client.logger.setLevel(logging.CRITICAL)
        queue_client.cmq_client.logger.setLevel(logging.CRITICAL)
        sign_queue = queue_client.get_queue(app.config["CMQ"]["queue_name"])
        for user_data in user_list:
            # 设置短信发送参数
            if sms_control == "No":
                user_data["sms"] = "No"
            # 设置随机时间参数
            if user_data["rand"] == "Yes":
                if time_now.split(":")[0] == "00":
                    delay = random.randint(600, 3600)
                else:
                    delay = random.randint(0, 3600)
            else:
                delay = 0
            # 设置打卡重试计数
            user_data["trace"] = 0
            # 打包消息(共用消息队列)
            message = CMQ_Message(
                json.dumps({
                    "type": "task",
                    "data": user_data,
                    "user": user_data["username"]
                }))

            try:
                msg_res = sign_queue.send_message(message, delayTime=delay)
            except CMQExceptionBase as e:
                Kit.write_log(logging.ERROR, "sign_task_post",
                              user_data["username"], "error",
                              "Send to CMQ failed", str(e))
                time.sleep(1)
                retry = True
                break
            Kit.write_log(logging.INFO,
                          "sign_task_post",
                          user_data["username"],
                          "success",
                          "Send sign task to CMQ",
                          "ID:{} Delay:{}".format(msg_res.msgId, delay),
                          to_stream=False)
        if retry:
            continue
        break

    app.logger.info("Post {} task to CMQ".format(len(user_list)))

    return "Post message num: {}".format(len(user_list))
示例#6
0
def multithread_slave(config, ids, conn):
    # 初始化ELK日志组件
    elk_logger = logging.getLogger(str(uuid.uuid1()))
    while elk_logger.hasHandlers():
        elk_logger.removeHandler(elk_logger.handlers[0])
    elk_logger.addHandler(logstash.LogstashHandler(config["ELK"]["host"], int(config["ELK"]["port"]), version=1))
    elk_logger.setLevel(logging.INFO)
    extra = json.loads(config["ELK"]["extra"])

    # 初始化消息队列
    user_config = {
        "host": config["CMQ"]["endpoint"],
        "secretId": config["CMQ"]["secret_id"],
        "secretKey": config["CMQ"]["secret_key"],
        "debug": False
    }
    queue_client = CMQ_Account(**user_config)
    sign_queue = queue_client.get_queue(config["CMQ"]["queue_name"])

    # 初始化风险地区
    risk_area = read_risk_area(conn)
    risk_expire = Kit.unix_time() + 600

    while True:
        # 建立连接并等待消息
        try:
            recv_msg = sign_queue.receive_message(random.randint(10, 30))
        except CMQExceptionBase:
            # Kit.print_white("<P:{0} G:{1} T:{2}> No message received".format(*ids))
            continue

        # 接收消息并上报流水
        message = json.loads(recv_msg.msgBody)
        log_data = {
            "function": "message_receiver",
            "username": message["user"],
            "result": "success",
            "status": "Receive message from CMQ",
            "message": str(recv_msg.msgId)
        }
        elk_logger.info(json.dumps(log_data), extra=extra)
        Kit.print_white("<P:{0} G:{1} T:{2}> {3} Receive message {4}".format(*ids, Kit.str_time(), recv_msg.msgId))

        if message["type"] == "task":
            # 检查风险地区更新
            if Kit.unix_time() > risk_expire:
                risk_area = read_risk_area(conn)
                risk_expire = Kit.unix_time() + 600

            # 处理打卡任务
            next_flow = handle_sign_task(config, risk_area, message["data"], elk_logger)

            # 确认接收消息并决定是否重试
            sign_queue.delete_message(recv_msg.receiptHandle)

            if next_flow is None:
                continue
            message = CMQ_Message(json.dumps(next_flow))
            for count in range(3):
                try:
                    msg_res = sign_queue.send_message(message)
                    Kit.print_white("<P:{0} G:{1} T:{2}> {3} Send message to "
                                    "CMQ {4}".format(*ids, Kit.str_time(), msg_res.msgId))
                    break
                except CMQExceptionBase as e:
                    Kit.print_red("<P:{0} G:{1} T:{2}> {3} Send CMQ message error. "
                                  "Retry {4} times".format(*ids, Kit.str_time(), count))
                    time.sleep(1)
        elif message["type"] == "done":
            # 处理打卡后续流程
            handle_sign_result(config, conn, message["data"], elk_logger)
            sign_queue.delete_message(recv_msg.receiptHandle)
示例#7
0
        },
        "at": {
            "isAtAll": True
        }
    }

    headers = {'Content-Type': 'application/json;charset=utf-8'}
    request = urllib2.Request(url=url, headers=headers, data=json.dumps(data))
    response = urllib2.urlopen(request)


secretId = 'your id'
secretKey = 'your key'
endpoint = 'http://cmq-queue-region.api.tencentyun.com'

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"

#print dir(my_account)

total_queues = my_account.list_queue()[0]

#print 'total_queues: %d' % total_queues

queues_list = my_account.list_queue(limit=total_queues)[1]

#print 'queues_list: %s' % queues_list

i = 0
示例#8
0
import time
import logging
from cmq.account import Account
from cmq.cmq_exception import *
from cmq.topic import *
from cmq.subscription import *
# 从腾讯云官网查看云api的密钥信息
secretId = '您的secretId'
secretKey = '您的secretKey'
# 使用广州地域的消息服务
endpoint = 'http://cmq-queue-gz.api.tencentyun.com'

try:
# 初始化 my_account
# Account类对象不是线程安全的,如果多线程使用,需要每个线程单独初始化Account类对象
    my_account = Account(endpoint, secretId, secretKey, debug=True)
    my_account.set_log_level(logging.DEBUG)
    topic_name = sys.argv[1] if len(sys.argv) > 1 else "Topic-test"
    my_topic = my_account.get_topic(topic_name)


# create topic
    topic_meta = TopicMeta()
    my_topic.create(topic_meta)
# set and get topic meta
    topic_meta.maxMsgSize = 32768
    my_topic.set_attributes(topic_meta)
    topic_meta = my_topic.get_attributes();

# list topic
    totalCount, topicList, next_off = my_account.list_topic();