🔨 定时执行.
This commit is contained in:
@@ -31,6 +31,10 @@ public interface Const extends com.orion.lang.constant.Const, FieldConst, CnCons
|
||||
|
||||
Integer DEFAULT_VERSION = 1;
|
||||
|
||||
Long SYSTEM_USER_ID = 0L;
|
||||
|
||||
String SYSTEM_USERNAME = "system";
|
||||
|
||||
String ERROR_LOG = "error.log";
|
||||
|
||||
}
|
||||
|
||||
@@ -45,6 +45,8 @@ public interface FieldConst {
|
||||
|
||||
String TOKEN = "token";
|
||||
|
||||
String SEQ = "seq";
|
||||
|
||||
String PATH = "path";
|
||||
|
||||
String ADDRESS = "address";
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.orion.ops.framework.banner.configuration;
|
||||
|
||||
import com.orion.ops.framework.banner.core.BannerApplicationRunner;
|
||||
import com.orion.ops.framework.banner.core.runner.BannerApplicationRunner;
|
||||
import com.orion.ops.framework.common.constant.AutoConfigureOrderConst;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.orion.ops.framework.banner.core;
|
||||
package com.orion.ops.framework.banner.core.runner;
|
||||
|
||||
import com.orion.lang.utils.Threads;
|
||||
import com.orion.lang.utils.ansi.AnsiAppender;
|
||||
@@ -47,7 +47,7 @@ public class OrionQuartzAutoConfiguration {
|
||||
* @param schedulerFactoryBean 调度器工厂
|
||||
* @return 调度器
|
||||
*/
|
||||
@Bean(initMethod = "start")
|
||||
@Bean
|
||||
public Scheduler scheduler(SchedulerFactoryBean schedulerFactoryBean) {
|
||||
// 获取 scheduler
|
||||
Scheduler scheduler = schedulerFactoryBean.getScheduler();
|
||||
|
||||
@@ -21,7 +21,7 @@ Authorization: {{token}}
|
||||
{
|
||||
"id": 5,
|
||||
"name": "测试 1",
|
||||
"expression": "0 */1 * * * ?",
|
||||
"expression": "0 */10 * * * ?",
|
||||
"timeout": "0",
|
||||
"command": "echo 123",
|
||||
"parameterSchema": "[]",
|
||||
|
||||
@@ -95,7 +95,7 @@ public class ExecJobController {
|
||||
@Operation(summary = "手动触发计划任务")
|
||||
@PreAuthorize("@ss.hasPermission('asset:exec-job:trigger')")
|
||||
public HttpWrapper<?> triggerExecJob(@Validated @RequestBody ExecJobTriggerRequest request) {
|
||||
execJobService.triggerExecJob(request);
|
||||
execJobService.manualTriggerExecJob(request.getId());
|
||||
return HttpWrapper.ok();
|
||||
}
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ import java.io.Serializable;
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Schema(name = "ExecJobCreateRequest", description = "计划执行任务 手动触发请求对象")
|
||||
@Schema(name = "ExecJobTriggerRequest", description = "计划执行任务 手动触发请求对象")
|
||||
public class ExecJobTriggerRequest implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
@@ -29,4 +29,10 @@ public class ExecJobTriggerRequest implements Serializable {
|
||||
@Schema(description = "id")
|
||||
private Long id;
|
||||
|
||||
@Schema(description = "执行用户 id")
|
||||
private Long userId;
|
||||
|
||||
@Schema(description = "执行用户名")
|
||||
private String username;
|
||||
|
||||
}
|
||||
|
||||
@@ -14,6 +14,11 @@ public enum ExecSourceEnum {
|
||||
*/
|
||||
BATCH,
|
||||
|
||||
/**
|
||||
* 计划任务
|
||||
*/
|
||||
JOB,
|
||||
|
||||
;
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package com.orion.ops.module.asset.handler.host.exec.job;
|
||||
|
||||
import com.orion.ops.framework.biz.operator.log.core.utils.OperatorLogs;
|
||||
import com.orion.ops.framework.common.constant.Const;
|
||||
import com.orion.ops.framework.common.constant.FieldConst;
|
||||
import com.orion.ops.module.asset.entity.request.exec.ExecJobTriggerRequest;
|
||||
import com.orion.ops.module.asset.service.ExecJobService;
|
||||
import com.orion.spring.SpringHolder;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -21,8 +24,17 @@ public class ExecCommandJob implements Job {
|
||||
|
||||
@Override
|
||||
public void execute(JobExecutionContext context) {
|
||||
long jobId = context.getMergedJobDataMap().getLong(FieldConst.KEY);
|
||||
// TODO
|
||||
long id = context.getMergedJobDataMap().getLong(FieldConst.KEY);
|
||||
log.info("ExecCommandJob.execute id: {}", id);
|
||||
// 执行命令
|
||||
ExecJobTriggerRequest request = ExecJobTriggerRequest.builder()
|
||||
.id(id)
|
||||
.userId(Const.SYSTEM_USER_ID)
|
||||
.username(Const.SYSTEM_USERNAME)
|
||||
.build();
|
||||
execJobService.triggerExecJob(request);
|
||||
// 清理日志上下文
|
||||
OperatorLogs.clear();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -72,6 +72,13 @@ public interface ExecJobService {
|
||||
/**
|
||||
* 手动触发任务
|
||||
*
|
||||
* @param id id
|
||||
*/
|
||||
void manualTriggerExecJob(Long id);
|
||||
|
||||
/**
|
||||
* 触发任务
|
||||
*
|
||||
* @param request request
|
||||
*/
|
||||
void triggerExecJob(ExecJobTriggerRequest request);
|
||||
|
||||
@@ -18,7 +18,9 @@ import com.orion.ops.module.asset.entity.domain.ExecJobDO;
|
||||
import com.orion.ops.module.asset.entity.domain.ExecLogDO;
|
||||
import com.orion.ops.module.asset.entity.request.exec.*;
|
||||
import com.orion.ops.module.asset.entity.vo.ExecJobVO;
|
||||
import com.orion.ops.module.asset.entity.vo.ExecLogVO;
|
||||
import com.orion.ops.module.asset.enums.ExecJobStatusEnum;
|
||||
import com.orion.ops.module.asset.enums.ExecSourceEnum;
|
||||
import com.orion.ops.module.asset.enums.HostConfigTypeEnum;
|
||||
import com.orion.ops.module.asset.handler.host.exec.job.ExecCommandJob;
|
||||
import com.orion.ops.module.asset.service.AssetAuthorizedDataService;
|
||||
@@ -33,6 +35,7 @@ import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -50,7 +53,7 @@ public class ExecJobServiceImpl implements ExecJobService {
|
||||
// TODO 测试 SSH 禁用后是什么样子的
|
||||
// TODO 操作日志 菜单
|
||||
// TODO 执行日志抽象
|
||||
// TODO 手动执行 测试 quartz
|
||||
// TODO 测试 quartz
|
||||
|
||||
// 内置参数 params.put("source", request.getSource());
|
||||
// params.put("sourceId", request.getSourceId());
|
||||
@@ -230,9 +233,59 @@ public class ExecJobServiceImpl implements ExecJobService {
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void manualTriggerExecJob(Long id) {
|
||||
log.info("ExecJobService.manualTriggerExecJob start id: {}", id);
|
||||
ExecJobTriggerRequest request = new ExecJobTriggerRequest();
|
||||
request.setId(id);
|
||||
// 设置执行用户
|
||||
Optional.ofNullable(SecurityUtils.getLoginUser())
|
||||
.ifPresent(s -> {
|
||||
request.setUserId(s.getId());
|
||||
request.setUsername(s.getUsername());
|
||||
});
|
||||
// 触发任务
|
||||
this.triggerExecJob(request);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void triggerExecJob(ExecJobTriggerRequest request) {
|
||||
|
||||
|
||||
Long id = request.getId();
|
||||
// 查询任务
|
||||
ExecJobDO job = execJobDAO.selectById(id);
|
||||
Valid.notNull(job, ErrorMessage.DATA_ABSENT);
|
||||
// 查询任务主机
|
||||
List<Long> hostIdList = execJobHostService.getHostIdByJobId(id);
|
||||
if (hostIdList.isEmpty()) {
|
||||
log.info("ExecJobService.triggerExecJob host empty id: {}", id);
|
||||
return;
|
||||
}
|
||||
// 获取执行序列
|
||||
Integer execSeq = this.getNextExecSeq(id);
|
||||
// 设置执行参数
|
||||
OperatorLogs.add(OperatorLogs.ID, id);
|
||||
OperatorLogs.add(OperatorLogs.NAME, job.getName());
|
||||
OperatorLogs.add(OperatorLogs.SEQ, execSeq);
|
||||
// 执行命令
|
||||
ExecCommandExecRequest exec = ExecCommandExecRequest.builder()
|
||||
.userId(request.getUserId())
|
||||
.username(request.getUsername())
|
||||
.source(ExecSourceEnum.JOB.name())
|
||||
.sourceId(id)
|
||||
.execSeq(execSeq)
|
||||
.description(job.getName())
|
||||
.timeout(job.getTimeout())
|
||||
.command(job.getCommand())
|
||||
.parameterSchema(job.getParameterSchema())
|
||||
.hostIdList(hostIdList)
|
||||
.build();
|
||||
ExecLogVO execResult = execService.execCommandWithSource(exec);
|
||||
// 更新最近执行的任务id
|
||||
ExecJobDO updateRecent = new ExecJobDO();
|
||||
updateRecent.setId(id);
|
||||
updateRecent.setRecentLogId(execResult.getId());
|
||||
execJobDAO.updateById(updateRecent);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user