创建定时任务时支持每小时每天等,修改get_next_run逻辑从schedule.jobs中获取#366
Conversation
longxiucai
commented
Mar 26, 2026
- 定时巡检任务创建ui支持每天、每小时、每周、每月
- 获取任务的下次运行时间从schedule.jobs中读取
Signed-off-by: longyuxiang <longyuxiang@kylinos.cn>
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! 此拉取请求旨在增强定时任务管理功能。通过在用户界面中引入更细粒度的调度频率选项(如每小时、每天等),用户可以更灵活地配置任务。同时,优化了任务下次运行时间的计算方式,使其直接从调度器中获取,提高了准确性和可靠性。 Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request expands the scheduled scan feature by adding new scheduling options: hourly, daily, weekly, and monthly, alongside the existing single and cron expression-based schedules. This update includes changes to the UI selection and the internal mapping of these new task types. The get_next_run method in utils/schedule_manager.py was also refactored to query the schedule library's job objects directly for the next run time. However, the new implementation of get_next_run introduces critical thread safety and significant performance issues due to unprotected concurrent access to the schedule.jobs list and an O(N²) complexity when used in task list rendering. Additionally, a minor style suggestion was made to add spaces after commas in a list in components/scheduled_scan.py to adhere to PEP 8.
| for job in schedule.jobs: | ||
| if self.task_id in job.tags: | ||
| return job.next_run | ||
| return None |
There was a problem hiding this comment.
此函数的新实现存在严重的线程安全和性能问题。
-
线程安全问题 (Critical):
schedule库本身不是线程安全的。此函数在 UI 线程中读取schedule.jobs,而后台的调度器线程 (_scheduler_loop) 会通过schedule.run_pending()修改它。这种并发访问没有加锁,会导致竞争条件,可能引发程序崩溃或返回不正确的数据。这是一个严重的设计缺陷。 -
性能问题 (High): 此函数会遍历整个
schedule.jobs列表。在render_task_list_tab函数中,它在一个循环内为每个任务都被调用。如果有 N 个任务,UI 渲染的复杂度将达到 O(N²),在任务数量多时会导致严重的性能瓶颈和界面卡顿。
建议:
- 必须引入线程锁来保护所有对
schedule对象(特别是schedule.jobs)的并发访问。 - 应该重构
render_task_list_tab,一次性获取所有任务的下次运行时间,以避免 O(N²) 的循环。
由于此变更引入了线程安全风险,我将其标记为 critical。
|
|
||
| # --- 调度类型选择放到表单外部 --- | ||
| schedule_types = ["单次定时", "周期定时(Cron表达式)"] | ||
| schedule_types = ["单次定时","每小时","每天","每周","每月", "周期定时(Cron表达式)"] |
There was a problem hiding this comment.
为了保持代码风格一致性和可读性,建议在列表元素的逗号后面添加一个空格。这符合 PEP 8 风格指南的建议。
| schedule_types = ["单次定时","每小时","每天","每周","每月", "周期定时(Cron表达式)"] | |
| schedule_types = ["单次定时", "每小时", "每天", "每周", "每月", "周期定时(Cron表达式)"] |
References
- 根据PEP 8,逗号后面应该跟一个空格以提高可读性。 (link)