ap_scheduler_util.py
4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import datetime # 导入日期时间工具
import time # 导入时间工具
import traceback # 导入异常堆栈打印工具
from apscheduler.schedulers.blocking import BlockingScheduler # 阻塞式调度器(示例/未使用)
from apscheduler.schedulers.background import BackgroundScheduler # 后台调度器(推荐使用)
from apscheduler.jobstores.memory import MemoryJobStore # 使用内存作为任务存储
from apscheduler.executors.pool import ThreadPoolExecutor, ProcessPoolExecutor # 线程/进程执行器
from log.logger import log # 项目统一日志记录器
import json
from main import transform_file
from mysql_service import funasr_db
jobstores = { # 任务存储配置
'default': MemoryJobStore() # 默认使用内存存储任务数据
}
executors = { # 执行器配置
'default': ThreadPoolExecutor(2), # 线程池执行器,最大并发线程数为 2
# 'processpool': ProcessPoolExecutor(3) # 进程池执行器示例(当前不启用)
}
job_defaults = { # 作业默认参数
'coalesce': True, # 合并错过的触发(只执行一次)
'max_instances': 3 # 同一任务允许的最大并发实例数
}
def job_func(): # 示例任务函数:打印当前 UTC 时间(到毫秒)
print(datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]) # 输出格式化时间
def scheduler_start(): # 启动 APScheduler 调度器
import funasr_service # 延迟导入,避免循环依赖或启动开销
log.info("scheduler_starting...") # 打印调度器启动日志
try: # 异常保护,确保错误被记录
scheduler = BackgroundScheduler(executors=executors, job_defaults=job_defaults) # 创建后台调度器实例
scheduler.add_job(funasr_service.task_compensate_send, 'interval', hours=1) # 每 1 小时执行一次补偿任务
scheduler.start() # 启动调度器
except Exception as e: # 捕获并记录异常细节
log.error("funasr scheduler_start Exception: " + str(traceback.format_exc())) # 打印异常堆栈
log.info("scheduler_start success") # 打印调度器启动成功日志
def scheduler1_start(): # 启动 APScheduler 调度器
import funasr_service # 延迟导入,避免循环依赖或启动开销
log.info("scheduler1_starting...") # 打印调度器启动日志
try: # 异常保护,确保错误被记录
scheduler = BackgroundScheduler(executors=executors, job_defaults=job_defaults) # 创建后台调度器实例
scheduler.add_job(transform_file, 'interval', minutes=1) # 每 1 分钟执行一次 transform_file 任务
scheduler.start() # 启动调度器
except Exception as e: # 捕获并记录异常细节
log.error("funasr scheduler1_start Exception: " + str(traceback.format_exc())) # 打印异常堆栈
log.info("scheduler_start success") # 打印调度器启动成功日志
def transform_file_for3():
import funasr_service
try:
task_ids = funasr_db.select_ali_asr_model_wait_deeling()
if task_ids:
for row in task_ids:
task_id = row["task_id"] if isinstance(row, dict) else row
log.info(f"处理任务:{task_id}")
data = {"task_id": task_id}
message = json.dumps(data).encode('utf-8')
funasr_service.handle_process(message)
except Exception as e:
log.error("transform_file_for3 Exception: " + str(traceback.format_exc()))
def transform_file():
import funasr_service
try:
# 查询处理中的任务数量
task_ids = funasr_db.select_ali_asr_model_wait_deeling()
if len(task_ids) > 10:
log.info(f"有{len(task_ids)}个任务正在处理中,不执行新任务")
return
# 查询待处理消息
tasks = funasr_db.select_ali_asr_model_wait_deel()
# 把数据更新为处理中,状态值为3
# 先提取id列表,然后根据id更新
if tasks:
id_list = [task['id'] for task in tasks]
funasr_db.update_ali_asr_model_res_by_id_list(id_list)
log.info(f"待处理任务数量:{len(tasks)}")
if tasks:
for task in tasks:
log.info(f"待处理任务:{task}")
# 处理任务
data = {"task_id": task['task_id']}
message = json.dumps(data).encode('utf-8')
funasr_service.handle_process(message)
except Exception as e: # 捕获并记录异常细节
log.error("transform_file_for3 Exception: " + str(traceback.format_exc())) # 打印异常堆栈