fix(cron): 分离调度器状态与数据库同步状态,修复持久化任务在提前启动场景下未加载的问题 - #9419
Open
gomico wants to merge 2 commits into
Open
Conversation
Contributor
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- Consider resetting
_db_synced(e.g., inshutdownor a dedicated reset method) so that a manager reused after shutdown will correctly resync from the database rather than skippingsync_from_db()on the nextstart()call. - The introduction of
_db_syncedchanges the state machine forCronJobManager; adding a brief comment or diagram of the expected_started/_db_syncedtransitions nearstart()would help future maintainers reason about edge cases like early starts and restarts.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider resetting `_db_synced` (e.g., in `shutdown` or a dedicated reset method) so that a manager reused after shutdown will correctly resync from the database rather than skipping `sync_from_db()` on the next `start()` call.
- The introduction of `_db_synced` changes the state machine for `CronJobManager`; adding a brief comment or diagram of the expected `_started`/`_db_synced` transitions near `start()` would help future maintainers reason about edge cases like early starts and restarts.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
修复了插件在核心启动前通过
add_basic_job(enabled=True)注册 BasicCronJob 时,导致CronJobManager.start()跳过sync_from_db(),数据库中已启用的持久化 CronJob 未加载到 APScheduler 的问题。根因:
_started变量同时兼任两个职责——"APScheduler 是否已运行"和"sync_from_db()是否已执行"。插件在plugin_manager.reload()阶段调用add_basic_job()时,_schedule_job()会提前启动 scheduler 并置_started = True;随后核心生命周期调用start()看到_started为真直接返回,sync_from_db()被永久跳过。关联 issue: #9418
Modifications / 改动点
astrbot/core/cron/manager.py:引入独立状态_db_synced,将"APScheduler 运行状态"与"数据库同步状态"解耦。start()的提前返回条件改为_db_synced,而非_started。_started保护,避免重复调用scheduler.start()。sync_from_db()完成后才置_db_synced = True,确保无论 scheduler 是否被提前启动,数据库中的持久化任务都能被加载。This is NOT a breaking change. / 这不是一个破坏性变更。
Screenshots or Test Results / 运行截图或测试结果
修复前(上次重启):cost_control 注册 CronJob →
_started=True→start()直接 return →sync_from_db()未执行,所有持久化 job 的updated_at未被更新,原有定时任务静默错过。修复后(本次重启验证):
(时间戳为 UTC,日志为 CST;数据库使用 sqlite3 直查确认。)
Checklist / 检查清单
😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
/ 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。
👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
/ 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”。
🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in
requirements.txtandpyproject.toml./ 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到
requirements.txt和pyproject.toml文件相应位置。😮 My changes do not introduce malicious code.
/ 我的更改没有引入恶意代码。
Summary by Sourcery
Decouple the cron scheduler running state from the database sync state to ensure persistent jobs are always loaded, even when the scheduler is started early.
Bug Fixes:
Tests: