zhaoguangqin opened a new issue, #4157:
URL: https://github.com/apache/hertzbeat/issues/4157
### Is there an existing issue for this?
- [x] I have searched the existing issues
### Current Behavior
# Bug报告:
## 问题现象
设置了 cron 表达式 `0 55 7 * * ?`(每天 7:55
执行),但任务每分钟都在执行。修改监控后恢复正常,重启hertzbeat项目后,该定时任务直接执行了,且每分钟执行一次。
## 根因分析
在 `TimerDispatcher.getNextExecutionInterval()` 中,调度逻辑通过以下条件判断是否使用 cron 调度:
```java
if (ScheduleTypeEnum.CRON.getType().equals(job.getScheduleType())
&& job.getCronExpression() != null
&& !job.getCronExpression().isEmpty())
```
但 `Job.scheduleType` 字段默认值为 `"interval"`,`cronExpression` 默认为 `null`。多个构建
Job 的代码路径**遗漏了**从 Monitor 复制这两个字段,导致条件永远不满足,代码走入 `else` 分支按固定间隔调度。
## 修复内容
共修复 **2 个类、3 个方法**,均在 `setTimestamp` 之后补充:
```java
appDefine.setScheduleType(monitor.getScheduleType());
appDefine.setCronExpression(monitor.getCronExpression());
```
| # | 类 | 方法 | 触发场景 |
|---|---|------|---------|
| 1 | `SchedulerInit` | `run()` | 项目启动时加载所有 Monitor 并下发 Job |
| 2 | `MonitorServiceImpl` | `addMonitor()` | 新建监控时构建 Job |
| 3 | `MonitorServiceImpl` | `updateAppCollectJob()` | 监控模板更新时刷新关联监控的 Job |
## 现象解释
| 操作 | 调用方法 | 是否设置 cron 字段 | 结果 |
|------|---------|------------------|------|
| 项目启动 | `SchedulerInit.run()` | 缺失 | cron 失效,按 interval 执行 |
| 新建监控 | `addMonitor()` | 缺失 | cron 失效,按 interval 执行 |
| 模板更新 | `updateAppCollectJob()` | 缺失 | cron 失效,按 interval 执行 |
| 修改监控 | `modifyMonitor()` | **正确** | cron 生效,恢复正常 |
### Expected Behavior
_No response_
### Steps To Reproduce
_No response_
### Environment
```markdown
HertzBeat version(s):v1.8.0
```
### Debug logs
_No response_
### Anything else?
_No response_
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail:
[email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]