Commit 7c3c416c authored by 黎博's avatar 黎博

新增启用/禁用监控接口,新增接口增加定时任务启动,删除去掉定时任务监控

parent da19d313
......@@ -7,11 +7,13 @@ import cn.qg.holmes.service.monitor.SqlMonitorDataSourceService;
import cn.qg.holmes.service.monitor.SqlMonitorTaskService;
import cn.qg.holmes.utils.DingdingUtils;
import cn.qg.holmes.utils.JdbcUtils;
import cn.qg.holmes.utils.QuartzUtils;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;
......@@ -38,6 +40,9 @@ public class SqlMonitorTaskController {
@Autowired
JdbcTemplate jdbcTemplate;
@Autowired
private Scheduler scheduler;
/**
* 获取监控列表,带搜索和分页
* @param pageNum 第几页
......@@ -84,7 +89,12 @@ public class SqlMonitorTaskController {
if (sqlMonitorTaskService.getOne(queryWrapper) != null) {
return JsonResult.buildErrorStateResult("监控名称重复", false);
}
return JsonResult.buildSuccessResult(sqlMonitorTaskService.save(sqlMonitorTask));
boolean result = sqlMonitorTaskService.save(sqlMonitorTask);
if (result) {
QuartzUtils.createSqlScheduleJob(scheduler, sqlMonitorTask);
return JsonResult.buildSuccessResult("监控创建成功!");
}
return JsonResult.buildErrorStateResult("监控创建失败!", false);
}
/**
......@@ -118,6 +128,7 @@ public class SqlMonitorTaskController {
}
boolean delResult = sqlMonitorTaskService.removeById(taskId);
if (delResult) {
QuartzUtils.deleteScheduleJob(scheduler, sqlMonitorTask.getTaskName());
return JsonResult.buildSuccessResult("删除成功!");
} else {
return JsonResult.buildErrorStateResult("删除失败!", false);
......@@ -164,6 +175,10 @@ public class SqlMonitorTaskController {
}
}
/**
* 定时任务执行策略
* @return
*/
@GetMapping("/strategy")
public JsonResult getSqlMonitorStrategy() {
List<Map<String, String>> strategyList = new ArrayList<>();
......@@ -181,4 +196,39 @@ public class SqlMonitorTaskController {
strategyList.add(weeklyMap);
return JsonResult.buildSuccessResult(strategyList);
}
/**
* 启用/禁用定时任务
* @param taskId 定时任务id
* @param status 状态,0-禁用,1-启用
* @return
*/
@PostMapping("/enable")
public JsonResult modifyTaskStatus(Integer taskId, Integer status) {
SqlMonitorTask sqlMonitorTask = sqlMonitorTaskService.getById(taskId);
if (sqlMonitorTask == null) {
return JsonResult.buildErrorStateResult("监控不存在!", false);
}
if (status == 1) {
if (sqlMonitorTask.getStatus() == 1) {
return JsonResult.buildErrorStateResult("监控已是启用状态", false);
} else {
sqlMonitorTask.setStatus(1);
sqlMonitorTaskService.updateById(sqlMonitorTask);
QuartzUtils.resumeScheduleJob(scheduler, sqlMonitorTask.getTaskName());
return JsonResult.buildSuccessResult("启用成功!", true);
}
}
if (status == 0) {
if (sqlMonitorTask.getStatus() == 0) {
return JsonResult.buildErrorStateResult("监控已是禁用状态", false);
} else {
sqlMonitorTask.setStatus(0);
sqlMonitorTaskService.updateById(sqlMonitorTask);
QuartzUtils.pauseScheduleJob(scheduler, sqlMonitorTask.getTaskName());
return JsonResult.buildSuccessResult("禁用成功!", true);
}
}
return JsonResult.buildErrorStateResult("status只能传0或1", false);
}
}
......@@ -38,6 +38,7 @@ public class QuartzUtils {
public static void pauseScheduleJob(Scheduler scheduler, String jobName) {
JobKey jobKey = JobKey.jobKey(jobName);
try {
log.info("暂停定时任务:{}", jobName);
scheduler.pauseJob(jobKey);
} catch (SchedulerException e) {
log.info("暂停定时任务出错:" + e.getMessage());
......@@ -53,6 +54,7 @@ public class QuartzUtils {
public static void resumeScheduleJob(Scheduler scheduler, String jobName) {
JobKey jobKey = JobKey.jobKey(jobName);
try {
log.info("启动定时任务:{}", jobName);
scheduler.resumeJob(jobKey);
} catch (SchedulerException e) {
log.info("启动定时任务出错:" + e.getMessage());
......
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