Skip to content

创建定时任务时支持每小时每天等,修改get_next_run逻辑从schedule.jobs中获取#366

Open
longxiucai wants to merge 1 commit into
kubesphere:release-2.0from
longxiucai:release-2.0-schedule-all-task-tpye
Open

创建定时任务时支持每小时每天等,修改get_next_run逻辑从schedule.jobs中获取#366
longxiucai wants to merge 1 commit into
kubesphere:release-2.0from
longxiucai:release-2.0-schedule-all-task-tpye

Conversation

@longxiucai

Copy link
Copy Markdown
  • 定时巡检任务创建ui支持每天、每小时、每周、每月
  • 获取任务的下次运行时间从schedule.jobs中读取

@gemini-code-assist

Copy link
Copy Markdown

Summary of Changes

Hello, 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

  • 调度类型扩展: 在定时任务创建界面中,新增了“每小时”、“每天”、“每周”、“每月”等调度类型选项,提升了任务调度的灵活性。
  • 下次运行时间获取逻辑优化: 任务下次运行时间的获取逻辑已更新,现在直接从schedule.jobs中读取,确保了与实际调度状态的一致性。
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread utils/schedule_manager.py
Comment on lines +109 to 112
for job in schedule.jobs:
if self.task_id in job.tags:
return job.next_run
return None

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

此函数的新实现存在严重的线程安全和性能问题。

  1. 线程安全问题 (Critical): schedule 库本身不是线程安全的。此函数在 UI 线程中读取 schedule.jobs,而后台的调度器线程 (_scheduler_loop) 会通过 schedule.run_pending() 修改它。这种并发访问没有加锁,会导致竞争条件,可能引发程序崩溃或返回不正确的数据。这是一个严重的设计缺陷。

  2. 性能问题 (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表达式)"]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

为了保持代码风格一致性和可读性,建议在列表元素的逗号后面添加一个空格。这符合 PEP 8 风格指南的建议。

Suggested change
schedule_types = ["单次定时","每小时","每天","每周","每月", "周期定时(Cron表达式)"]
schedule_types = ["单次定时", "每小时", "每天", "每周", "每月", "周期定时(Cron表达式)"]
References
  1. 根据PEP 8,逗号后面应该跟一个空格以提高可读性。 (link)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant