|
@@ -25,7 +25,7 @@ log_ = Log()
|
|
|
config_ = set_config()
|
|
|
|
|
|
|
|
|
-def title_generate_main():
|
|
|
+def title_generate_main0():
|
|
|
# log_.info("debug: title_generate_main")
|
|
|
# 初始化client
|
|
|
mq_client = MQClient(
|
|
@@ -183,6 +183,130 @@ def title_generate_main():
|
|
|
continue
|
|
|
|
|
|
|
|
|
+def title_generate_main():
|
|
|
+ # log_.info("debug: title_generate_main")
|
|
|
+ # 初始化client
|
|
|
+ mq_client = MQClient(
|
|
|
+ # 设置HTTP协议客户端接入点
|
|
|
+ host=config_.MQ_CONFIG['ENDPOINT'],
|
|
|
+ # AccessKey ID,阿里云身份验证标识
|
|
|
+ access_id=config_.MQ_CONFIG['ACCESS_KEY'],
|
|
|
+ # AccessKey Secret,阿里云身份验证密钥
|
|
|
+ access_key=config_.MQ_CONFIG['SECRET_KEY']
|
|
|
+ )
|
|
|
+ # Topic所属的实例ID,在消息队列RocketMQ版控制台创建。
|
|
|
+ # 若实例有命名空间,则实例ID必须传入;若实例无命名空间,则实例ID传入空字符串。实例的命名空间可以在消息队列RocketMQ版控制台的实例详情页面查看。
|
|
|
+ instance_id = config_.MQ_CONFIG['INSTANCE_ID']
|
|
|
+ # 监听消息所属的Topic
|
|
|
+ consumer_topic_name = config_.MQ_TOPIC_CONFIG['asr_title_todo']['topic_name']
|
|
|
+ # 您在消息队列RocketMQ版控制台创建的Group ID。
|
|
|
+ group_id = config_.MQ_TOPIC_CONFIG['asr_title_todo']['group_id']
|
|
|
+ consumer = mq_client.get_consumer(instance_id, consumer_topic_name, group_id)
|
|
|
+ # 长轮询表示如果Topic没有消息,则客户端请求会在服务端挂起3秒,3秒内如果有消息可以消费则立即返回响应。
|
|
|
+ # 长轮询时间3秒(最多可设置为30秒)。
|
|
|
+ wait_seconds = 3
|
|
|
+ # 一次最多消费1条(最多可设置为16条)。
|
|
|
+ batch = 1
|
|
|
+ print(("%sConsume And Ak Message From Topic%s\nTopicName:%s\nMQConsumer:%s\nWaitSeconds:%s\n" \
|
|
|
+ % (10 * "=", 10 * "=", consumer_topic_name, group_id, wait_seconds)))
|
|
|
+ while True:
|
|
|
+ receipt_handle_list = []
|
|
|
+ try:
|
|
|
+ # 长轮询消费消息。
|
|
|
+ start_time0 = time.time()
|
|
|
+ recv_msgs = consumer.consume_message(batch, wait_seconds)
|
|
|
+ for msg in recv_msgs:
|
|
|
+ start_time = time.time()
|
|
|
+ receive_info = {
|
|
|
+ 'messageId': msg.message_id,
|
|
|
+ 'messageBodyMD5': msg.message_body_md5,
|
|
|
+ 'messageTag': msg.message_tag,
|
|
|
+ 'consumedTimes': msg.consumed_times,
|
|
|
+ 'publishTime': msg.publish_time,
|
|
|
+ 'body': msg.message_body,
|
|
|
+ 'nextConsumeTime': msg.next_consume_time,
|
|
|
+ 'receiptHandle': msg.receipt_handle,
|
|
|
+ 'properties': msg.properties
|
|
|
+ }
|
|
|
+ log_.info(receive_info)
|
|
|
+ message_body = json.loads(msg.message_body)
|
|
|
+ video_id = message_body['videoId']
|
|
|
+ video_path = message_body['videoPath']
|
|
|
+
|
|
|
+ # 1. 添加消息handle到ack_message列表
|
|
|
+ receipt_handle_list.append(msg.receipt_handle)
|
|
|
+
|
|
|
+ # msg.next_consume_time前若不确认消息消费成功,则消息会被重复消费。
|
|
|
+ # 消息句柄有时间戳,同一条消息每次消费拿到的都不一样。
|
|
|
+ consumer.ack_message(receipt_handle_list)
|
|
|
+ log_.info({
|
|
|
+ 'ackMessage': {
|
|
|
+ 'status': 'success',
|
|
|
+ 'receiptHandleList': receipt_handle_list,
|
|
|
+ 'executeTime': (time.time() - start_time0) * 1000
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ # 2. 获取标题
|
|
|
+ title, generate_filepath = title_generate(video_id=video_id, video_path=video_path)
|
|
|
+ log_.info({'titleGenerateInitial': {'videoId': video_id, 'title': title}})
|
|
|
+ # 删除相关文件
|
|
|
+ for _, filepath in generate_filepath.items():
|
|
|
+ try:
|
|
|
+ os.remove(filepath)
|
|
|
+ except:
|
|
|
+ continue
|
|
|
+ if title is None:
|
|
|
+ continue
|
|
|
+ elif title[0] in ['"', "'"] and title[-1] in ['"', "'"]:
|
|
|
+ title = title[1:-1]
|
|
|
+ log_.info({'titleGenerate': {'videoId': video_id, 'title': title}})
|
|
|
+
|
|
|
+ # 3. 发送结果至done消息队列
|
|
|
+ try:
|
|
|
+ msg_content = {
|
|
|
+ 'title': title,
|
|
|
+ 'videoId': video_id
|
|
|
+ }
|
|
|
+ message_body = json.dumps(msg_content)
|
|
|
+ producer_topic_name = config_.MQ_TOPIC_CONFIG['asr_title_done']['topic_name']
|
|
|
+ producer = mq_client.get_producer(instance_id=instance_id, topic_name=producer_topic_name)
|
|
|
+ produce_msg = TopicMessage(message_body=message_body)
|
|
|
+ # 设置消息的Key。
|
|
|
+ produce_msg.set_message_key(f"videoId-{video_id}")
|
|
|
+ re_msg = producer.publish_message(produce_msg)
|
|
|
+ log_.info({
|
|
|
+ 'publish': {
|
|
|
+ 'status': 'success',
|
|
|
+ 'messageID': re_msg.message_id,
|
|
|
+ 'bodyMD5': re_msg.message_body_md5,
|
|
|
+ 'messageContent': msg_content,
|
|
|
+ 'executeTime': (time.time() - start_time) * 1000
|
|
|
+ }
|
|
|
+ })
|
|
|
+ except Exception as publish_e:
|
|
|
+ log_.error({
|
|
|
+ 'errorType': 'publish',
|
|
|
+ 'status': 'fail',
|
|
|
+ 'videoId': video_id,
|
|
|
+ 'exception': publish_e,
|
|
|
+ 'traceback': traceback.format_exc()
|
|
|
+ })
|
|
|
+ # if publish_e.type == "TopicNotExist":
|
|
|
+ # sys.exit(1)
|
|
|
+
|
|
|
+ except Exception as consume_e:
|
|
|
+ log_.error({
|
|
|
+ 'errorType': 'consume',
|
|
|
+ 'status': 'fail',
|
|
|
+ 'exception': consume_e,
|
|
|
+ 'traceback': traceback.format_exc()
|
|
|
+ })
|
|
|
+ # log_.error("Consume Message Fail! Exception:%s\n" % e)
|
|
|
+ time.sleep(30)
|
|
|
+ continue
|
|
|
+
|
|
|
+
|
|
|
class FlaskApp(Flask):
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
super(FlaskApp, self).__init__(*args, **kwargs)
|