Repository navigation
Expand file tree
/
Copy pathfix_schedule_task_refs.py
More file actions
105 lines (82 loc) · 3.35 KB
/
Copy pathfix_schedule_task_refs.py
File metadata and controls
105 lines (82 loc) · 3.35 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
96
97
98
99
100
101
102
103
104
105
"""
一次性校正计划任务索引的工具 (fix_schedule_task_refs)
背景
----
ok-script 的 Windows 计划任务通过 `-t N`(1-based 索引)定位 onetime_tasks。
当 config.py 中 onetime_tasks 顺序被重排后,已创建的计划任务仍指向旧索引。
本工具复用 schedule_index_sync.sync_schedule_task_indexes(),
与启动时自动校正逻辑为单一实现;通常在 GUI 无法正常启动时手动运行。
用法(在应用根目录运行)
------------------------
python fix_schedule_task_refs.py
python fix_schedule_task_refs.py --config src.config:config
它会:
1. 加载应用配置,实例化 onetime_tasks(读取每个任务的 name);
2. 读取 configs/schedule_tasks_cache.json;
3. 只处理本应用(如 \\ok-ef\\)下的任务;
4. 定位每个 -t 目标对应的当前任务,把 `-t X`(数字索引 / 历史任务名 / 过期
模块路径)统一迁移为稳定标识(模块路径.类名),并回填缓存元数据,
同步更新缓存 / Windows 计划任务(COM)。
"""
import argparse
import sys
from ok.cli import load_config
from ok.util.clazz import init_class_by_name
class _ExecutorStub:
"""仅用于实例化任务以读取 name 的最小 executor 桩。
ExecutorOperation.__init__ 只访问 executor.scene,因此足够。
"""
def __init__(self):
self.scene = None
self.onetime_tasks = []
self.trigger_tasks = []
class _AppStub:
"""任务实例化所需的最小 app 桩。"""
debug = False
headless = True
po_translation = "Failed"
def tr(self, key):
return key
def build_onetime_tasks(config) -> list:
"""从配置的 onetime_tasks 列表实例化任务(顺序即当前索引顺序)。"""
tasks = []
executor = _ExecutorStub()
app = _AppStub()
for task_class in config.get("onetime_tasks", []):
try:
task = init_class_by_name(task_class[0], task_class[1], executor=executor, app=app)
# after_init/post_init 只用于读取 name,失败不阻塞(name 在 __init__ 已就绪)
try:
task.after_init(executor=executor, scene=executor.scene)
task.post_init()
except Exception:
pass
tasks.append(task)
print(f" loaded task #{len(tasks)}: {getattr(task, 'name', '?')}")
except Exception as e:
print(f" skipped task {task_class}: {e}")
return tasks
def main(argv=None):
parser = argparse.ArgumentParser(
description="Fix schedule task -t indexes after onetime_tasks reorder"
)
parser.add_argument(
"-c",
"--config",
help="Config import target, e.g. src.config:config or config:config",
)
args = parser.parse_args(argv)
config = load_config(args.config)
# 同步器通过 og.config 解析缓存路径与任务根路径,需在调用前注入实际配置
from ok import og
og.config = config
tasks = build_onetime_tasks(config)
if not tasks:
print("No onetime_tasks loaded, nothing to correct.")
return 1
from ok.ui.qt.tasks.schedule_index_sync import sync_schedule_task_indexes
corrected = sync_schedule_task_indexes(onetime_tasks=tasks)
print(f"Corrected {corrected} schedule task(s).")
return 0
if __name__ == "__main__":
raise SystemExit(main())