feat: Persist the scheduler's enabled state to config.toml and preserve its in-memory status during config reloads.
This commit is contained in:
@@ -1376,8 +1376,12 @@ class ProvisionerBot:
|
||||
f.write('{"teams": {}}')
|
||||
cleaned.append("team_tracker.json")
|
||||
|
||||
# 重载配置
|
||||
# 重载配置 (保护调度器内存状态,reload 会从 toml 重置)
|
||||
import config as cfg
|
||||
scheduler_was_enabled = cfg.SCHEDULER_ENABLED
|
||||
_reload()
|
||||
if self._scheduler_active or scheduler_was_enabled:
|
||||
cfg.SCHEDULER_ENABLED = scheduler_was_enabled
|
||||
|
||||
if cleaned:
|
||||
log.info(f"自动清理完成: {', '.join(cleaned)}")
|
||||
@@ -6369,8 +6373,9 @@ class ProvisionerBot:
|
||||
)
|
||||
return
|
||||
|
||||
# 更新 config
|
||||
# 更新 config 并持久化
|
||||
cfg.SCHEDULER_ENABLED = True
|
||||
self._persist_scheduler_enabled(True)
|
||||
|
||||
# 检查是否在时间窗口内
|
||||
now = datetime.now()
|
||||
@@ -6397,6 +6402,7 @@ class ProvisionerBot:
|
||||
|
||||
elif action == "off":
|
||||
cfg.SCHEDULER_ENABLED = False
|
||||
self._persist_scheduler_enabled(False)
|
||||
if self._scheduler_active:
|
||||
self._scheduler_stop_event.set()
|
||||
await update.message.reply_text(
|
||||
@@ -6547,6 +6553,20 @@ class ProvisionerBot:
|
||||
|
||||
await update.message.reply_text(text, parse_mode="HTML")
|
||||
|
||||
def _persist_scheduler_enabled(self, enabled: bool):
|
||||
"""持久化 scheduler.enabled 到 config.toml"""
|
||||
try:
|
||||
import tomllib, tomli_w
|
||||
with open(CONFIG_FILE, "rb") as f:
|
||||
config = tomllib.load(f)
|
||||
if "scheduler" not in config:
|
||||
config["scheduler"] = {}
|
||||
config["scheduler"]["enabled"] = enabled
|
||||
with open(CONFIG_FILE, "wb") as f:
|
||||
tomli_w.dump(config, f)
|
||||
except Exception as e:
|
||||
log.warning(f"持久化 scheduler.enabled 失败: {e}")
|
||||
|
||||
def _register_scheduler_daily_job(self):
|
||||
"""注册每日定时触发 Job"""
|
||||
import config as cfg
|
||||
@@ -6923,7 +6943,11 @@ class ProvisionerBot:
|
||||
with open(team_file, "w", encoding="utf-8") as f:
|
||||
json.dump(existing, f, ensure_ascii=False, indent=2)
|
||||
|
||||
import config as cfg_inner
|
||||
scheduler_was_enabled = cfg_inner.SCHEDULER_ENABLED
|
||||
reload_config()
|
||||
if self._scheduler_active or scheduler_was_enabled:
|
||||
cfg_inner.SCHEDULER_ENABLED = scheduler_was_enabled
|
||||
log.success(f"调度器: {success_count} 个账号已写入 team.json (总计 {len(existing)})")
|
||||
except Exception as e:
|
||||
log.error(f"调度器: 保存 team.json 失败: {e}")
|
||||
|
||||
Reference in New Issue
Block a user