refactor: 优化report.py中的主流程和后续状态处理逻辑,添加时间间隔控制以提高性能
This commit is contained in:
parent
81780ecd0a
commit
2aace9ffe9
84
report.py
84
report.py
@ -1,5 +1,4 @@
|
||||
import time
|
||||
|
||||
from DB import DBVidcon, DBSA
|
||||
from report_video import DailymotionClient
|
||||
import logger
|
||||
@ -8,45 +7,56 @@ db = DBVidcon()
|
||||
d = DailymotionClient()
|
||||
|
||||
k = {
|
||||
"open":1,
|
||||
"solved":2,
|
||||
"awaiting your reply":3,
|
||||
"open": 1,
|
||||
"solved": 2,
|
||||
"awaiting your reply": 3,
|
||||
}
|
||||
|
||||
last_main_run = 0
|
||||
last_subsequent_run = 0
|
||||
|
||||
MAIN_INTERVAL = 60 * 5 # 每 5 分钟执行一次
|
||||
SUBSEQUENT_INTERVAL = 60 * 60 # 每 60 分钟执行一次
|
||||
|
||||
while True:
|
||||
start_time = int(time.time())
|
||||
now = int(time.time())
|
||||
|
||||
lis = db.get_report_video()
|
||||
if len(lis) > 0:
|
||||
for li in lis:
|
||||
logger.logger.info(f"name:{li['name_title']},link:{li['link']} ")
|
||||
try:
|
||||
info,report_id,status,report_ts, = d.process_ticket(li['name_title'], li['link'])
|
||||
subsequent_status = k.get(status, 1)
|
||||
db.update_fight_record_status(li['id'],report_id, 2, f"http://123.58.197.91:5000/image/{info}", report_ts, subsequent_status)
|
||||
db.flush()
|
||||
except Exception as e:
|
||||
logger.logger.error(f"ID:{li['id']}, e:{e}")
|
||||
db.update_fight_record_status(li['id'], 0,3, str(e))
|
||||
time.sleep(1 * 60)
|
||||
# 处理主流程
|
||||
if now - last_main_run >= MAIN_INTERVAL:
|
||||
last_main_run = now
|
||||
lis = db.get_report_video()
|
||||
if len(lis) > 0:
|
||||
for li in lis:
|
||||
logger.logger.info(f"name:{li['name_title']},link:{li['link']} ")
|
||||
try:
|
||||
info, report_id, status, report_ts = d.process_ticket(li['name_title'], li['link'])
|
||||
subsequent_status = k.get(status, 1)
|
||||
db.update_fight_record_status(
|
||||
li['id'], report_id, 2, f"http://123.58.197.91:5000/image/{info}",
|
||||
report_ts, subsequent_status
|
||||
)
|
||||
db.flush()
|
||||
except Exception as e:
|
||||
logger.logger.error(f"ID:{li['id']}, e:{e}")
|
||||
db.update_fight_record_status(li['id'], 0, 3, str(e))
|
||||
time.sleep(60) # 出错延迟
|
||||
|
||||
|
||||
subsequent_list = db.get_subsequent_report_video()
|
||||
if len(subsequent_list) == 0:
|
||||
time.sleep(60 * 5)
|
||||
else:
|
||||
for li in subsequent_list:
|
||||
rs_id = li['id']
|
||||
r_id = li['report_id']
|
||||
logger.logger.info(f"subsequent id:{rs_id},report_id:{r_id} ")
|
||||
try:
|
||||
subsequent_status, info = d.report_follow_up(r_id)
|
||||
db.update_subsequent_status_by_id(rs_id, subsequent_status, f"http://123.58.197.91:5000/image/{info}")
|
||||
except Exception as e:
|
||||
logger.logger.error(f"ID:{rs_id}, e:{e}")
|
||||
db.update_subsequent_status_by_id(rs_id, 1, str(e))
|
||||
if now - last_subsequent_run >= SUBSEQUENT_INTERVAL:
|
||||
last_subsequent_run = now
|
||||
subsequent_list = db.get_subsequent_report_video()
|
||||
if len(subsequent_list) > 0:
|
||||
for li in subsequent_list:
|
||||
rs_id = li['id']
|
||||
r_id = li['report_id']
|
||||
logger.logger.info(f"subsequent id:{rs_id},report_id:{r_id} ")
|
||||
try:
|
||||
subsequent_status, info = d.report_follow_up(r_id)
|
||||
db.update_subsequent_status_by_id(
|
||||
rs_id, subsequent_status, f"http://123.58.197.91:5000/image/{info}"
|
||||
)
|
||||
except Exception as e:
|
||||
logger.logger.error(f"ID:{rs_id}, e:{e}")
|
||||
db.update_subsequent_status_by_id(rs_id, 1, str(e))
|
||||
|
||||
used = int(time.time() - start_time)
|
||||
remain = max(0, 60 * 5 - used)
|
||||
if remain > 0:
|
||||
logger.logger.info(f"当前轮耗时 {used:.1f} 秒,休眠 {remain:.1f} 秒")
|
||||
time.sleep(remain)
|
||||
time.sleep(5)
|
Loading…
x
Reference in New Issue
Block a user