Commit aba58c21 authored by 黎博's avatar 黎博

优化编辑监控接口,新增quartz接口

parent 7c3c416c
package cn.qg.holmes.controller.monitor;
import cn.qg.holmes.common.JsonResult;
import cn.qg.holmes.entity.monitor.SqlMonitorTask;
import cn.qg.holmes.service.monitor.SqlMonitorTaskService;
import com.alibaba.fastjson.JSON;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* Quartz相关接口
*/
@RestController
@RequestMapping("/quartz")
public class QuartzController {
@Autowired
Scheduler scheduler;
@Autowired
SqlMonitorTaskService sqlMonitorTaskService;
@GetMapping("/job/list")
public JsonResult getQuartzList() throws SchedulerException {
List<Map<String, String>> jobDetailList = new ArrayList<>();
List<SqlMonitorTask> sqlMonitorTaskList = sqlMonitorTaskService.list();
for (SqlMonitorTask sqlMonitorTask: sqlMonitorTaskList) {
JobKey jobKey = JobKey.jobKey(sqlMonitorTask.getTaskName());
JobDetail jobDetail = scheduler.getJobDetail(jobKey);
if (jobDetail != null) {
jobDetailList.add(JSON.parseObject(JSON.toJSONString(jobDetail), Map.class));
}
}
return JsonResult.buildSuccessResult(jobDetailList);
}
}
...@@ -111,7 +111,22 @@ public class SqlMonitorTaskController { ...@@ -111,7 +111,22 @@ public class SqlMonitorTaskController {
if (sqlMonitorTaskDb != null && !sqlMonitorTaskDb.getId().equals(sqlMonitorTask.getId())) { if (sqlMonitorTaskDb != null && !sqlMonitorTaskDb.getId().equals(sqlMonitorTask.getId())) {
return JsonResult.buildErrorStateResult("监控名称重复", false); return JsonResult.buildErrorStateResult("监控名称重复", false);
} }
return JsonResult.buildSuccessResult(sqlMonitorTaskService.saveOrUpdate(sqlMonitorTask)); boolean updateResult = sqlMonitorTaskService.saveOrUpdate(sqlMonitorTask);
if (updateResult) {
// 如果状态发生了变更,则根据改变之后的状态来决定是暂停任务还是重启任务
if (!sqlMonitorTask.getStatus().equals(sqlMonitorTaskDb.getStatus())) {
// 如果变更后的状态为0
if (sqlMonitorTask.getStatus() == 0) {
QuartzUtils.pauseScheduleJob(scheduler, sqlMonitorTask.getTaskName());
}
// 如果变更后的状态为1
if (sqlMonitorTask.getStatus() == 1) {
QuartzUtils.resumeScheduleJob(scheduler, sqlMonitorTask.getTaskName());
}
}
return JsonResult.buildSuccessResult("监控修改成功!");
}
return JsonResult.buildErrorStateResult("监控修改失败", false);
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment