This commit is contained in:
HuangHai
2026-01-13 19:29:02 +08:00
parent 9fdbdaa338
commit a9b0d125d2
6 changed files with 106 additions and 6 deletions

View File

@@ -269,7 +269,7 @@ async def get_station_list(d, service, max_stations_count=MAX_STATIONS_COUNT):
await asyncio.gather(*background_tasks, return_exceptions=True)
logger.info(f"采集任务完成,共采集 {total_processed_count} 个场站。")
return True
return total_processed_count
async def analyze_prices_background(service, station_name, image_paths):
"""
@@ -313,21 +313,63 @@ async def main(service=None, do_cleanup=True):
if do_cleanup:
Kit.clear_temp_dir()
should_close_service = False
if service is None:
service = AiTeJiYiChongService()
should_close_service = True
await service.init_db()
d = u2.connect()
# 任务日志记录
from datetime import datetime
task_id = str(uuid.uuid4())
start_time = datetime.now()
operator = "艾特吉易充"
# 记录任务开始
try:
await get_station_list(d, service)
await service.db.save("t_crawl_task_log", {
"task_id": task_id,
"operator": operator,
"start_time": start_time,
"status": "running"
}, "task_id")
except Exception as e:
logger.error(f"无法记录任务开始日志: {e}")
total_count = 0
status = "success"
error_msg = None
try:
total_count = await get_station_list(d, service)
return True
except Exception as e:
logger.error(f"爬取过程中出现异常: {e}")
status = "failed"
error_msg = str(e)
return False
finally:
# 记录任务结束
end_time = datetime.now()
duration = int((end_time - start_time).total_seconds())
try:
await service.db.update("t_crawl_task_log", {
"task_id": task_id,
"end_time": end_time,
"duration_seconds": duration,
"station_count": total_count,
"status": status,
"error_msg": error_msg
}, "task_id")
logger.info(f"任务执行日志已更新: {operator} | 耗时: {duration}s | 数量: {total_count} | 状态: {status}")
except Exception as e:
logger.error(f"无法更新任务执行日志: {e}")
# 如果是内部初始化的 service,则在此关闭
pass
if should_close_service:
await service.close_db()
if __name__ == "__main__":
asyncio.run(main())