refactor: scheduler to JSON+cooldown, update SOP
This commit is contained in:
@@ -1,18 +1,71 @@
|
||||
import os, re
|
||||
from datetime import datetime
|
||||
import os, json
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
INTERVAL = 60 # 原版 55+random*10
|
||||
INTERVAL = 60
|
||||
ONCE = False
|
||||
|
||||
script_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
PENDING = os.path.join(script_dir, '../sche_tasks/pending')
|
||||
_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
TASKS = os.path.join(_dir, '../sche_tasks')
|
||||
DONE = os.path.join(_dir, '../sche_tasks/done')
|
||||
|
||||
def _parse_cooldown(repeat):
|
||||
"""解析repeat为冷却时间(比实际周期略短,防漂移)"""
|
||||
if repeat == 'once': return timedelta(days=999999)
|
||||
if repeat == 'daily': return timedelta(hours=20)
|
||||
if repeat == 'weekly': return timedelta(days=6)
|
||||
if repeat == 'monthly': return timedelta(days=27)
|
||||
if repeat.startswith('every_'):
|
||||
parts = repeat.split('_')
|
||||
n = int(parts[1].rstrip('hdm'))
|
||||
u = parts[1][-1]
|
||||
if u == 'h': return timedelta(hours=n)
|
||||
if u == 'm': return timedelta(minutes=n)
|
||||
if u == 'd': return timedelta(days=n)
|
||||
return timedelta(hours=20)
|
||||
|
||||
def _last_run(tid, done_files):
|
||||
"""找最近一次执行时间"""
|
||||
latest = None
|
||||
for df in done_files:
|
||||
if not df.endswith(f'_{tid}.md'): continue
|
||||
try:
|
||||
t = datetime.strptime(df[:15], '%Y-%m-%d_%H%M')
|
||||
if latest is None or t > latest: latest = t
|
||||
except: continue
|
||||
return latest
|
||||
|
||||
def check():
|
||||
if not os.path.isdir(PENDING): return None
|
||||
if not os.path.isdir(TASKS): return None
|
||||
now = datetime.now()
|
||||
for f in os.listdir(PENDING):
|
||||
m = re.match(r'(\d{4}-\d{2}-\d{2})_(\d{4})_', f)
|
||||
if m and now >= datetime.strptime(f'{m[1]} {m[2]}', '%Y-%m-%d %H%M'):
|
||||
raw = open(os.path.join(PENDING, f), encoding='utf-8').read()
|
||||
return f'按scheduled_task_sop执行任务文件 ../sche_tasks/pending/{f}(立刻移到running)\n内容:\n{raw}'
|
||||
os.makedirs(DONE, exist_ok=True)
|
||||
done_files = set(os.listdir(DONE))
|
||||
for f in sorted(os.listdir(TASKS)):
|
||||
if not f.endswith('.json'): continue
|
||||
tid = f[:-5]
|
||||
try:
|
||||
task = json.loads(open(os.path.join(TASKS, f), encoding='utf-8').read())
|
||||
except: continue
|
||||
if not task.get('enabled', False): continue
|
||||
|
||||
repeat = task.get('repeat', 'daily')
|
||||
sched = task.get('schedule', '00:00')
|
||||
h, m = map(int, sched.split(':'))
|
||||
|
||||
# 还没到schedule时间就跳过
|
||||
if now.hour < h or (now.hour == h and now.minute < m): continue
|
||||
|
||||
# 检查冷却
|
||||
last = _last_run(tid, done_files)
|
||||
cooldown = _parse_cooldown(repeat)
|
||||
if last and (now - last) < cooldown: continue
|
||||
|
||||
# 触发
|
||||
ts = now.strftime('%Y-%m-%d_%H%M')
|
||||
rpt = os.path.join(DONE, f'{ts}_{tid}.md')
|
||||
prompt = task.get('prompt', '')
|
||||
return (f'[定时任务] {tid}\n'
|
||||
f'[报告路径] {rpt}\n\n'
|
||||
f'先读 scheduled_task_sop 了解执行流程,然后执行以下任务:\n\n'
|
||||
f'{prompt}\n\n'
|
||||
f'完成后将执行报告写入 {rpt}。')
|
||||
return None
|
||||
Reference in New Issue
Block a user