Commit 74f0e51a by guominglei

修改运行的key和报错问题

parent 5b9da215
...@@ -53,11 +53,6 @@ ...@@ -53,11 +53,6 @@
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.3.2.RELEASE</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
......
...@@ -4,10 +4,17 @@ import com.alibaba.fastjson.JSON; ...@@ -4,10 +4,17 @@ import com.alibaba.fastjson.JSON;
import com.byit.dto.plugin.RunLog; import com.byit.dto.plugin.RunLog;
import com.byit.dto.plugin.RunNode; import com.byit.dto.plugin.RunNode;
import com.byit.enums.NodePropertyEnum; import com.byit.enums.NodePropertyEnum;
import com.byit.job.WorkRoulette;
import com.byit.job.utils.PlaceholderUtils;
import com.byit.mapper.JobTaskRunLogMapper;
import com.byit.mapper.JobTaskScheduleMapper; import com.byit.mapper.JobTaskScheduleMapper;
import com.byit.model.JobTaskRunLogWithBLOBs;
import com.byit.model.JobTaskSchedule; import com.byit.model.JobTaskSchedule;
import com.byit.service.ApiNodeService; import com.byit.service.ApiNodeService;
import com.byit.task.ScriptExecutorJobTask;
import com.byit.utils.ValidationUtil; import com.byit.utils.ValidationUtil;
import io.netty.util.TimerTask;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -22,7 +29,7 @@ import static com.alibaba.fastjson.serializer.SerializerFeature.WriteClassName; ...@@ -22,7 +29,7 @@ import static com.alibaba.fastjson.serializer.SerializerFeature.WriteClassName;
public class ApiNodeServiceImpl implements ApiNodeService { public class ApiNodeServiceImpl implements ApiNodeService {
@Resource @Resource
private JobTaskScheduleMapper jobTaskScheduleMapper; private JobTaskRunLogMapper jobTaskRunLogMapper;
@Resource @Resource
private StringRedisTemplate stringRedisTemplate; private StringRedisTemplate stringRedisTemplate;
...@@ -39,8 +46,10 @@ public class ApiNodeServiceImpl implements ApiNodeService { ...@@ -39,8 +46,10 @@ public class ApiNodeServiceImpl implements ApiNodeService {
ValidationUtil.dataNotBank(runNode.getRunCmd(), "运行命令不允许为空!"); ValidationUtil.dataNotBank(runNode.getRunCmd(), "运行命令不允许为空!");
ValidationUtil.dataNotBank(runNode.getJobType(), "节点类型不允许为空!"); ValidationUtil.dataNotBank(runNode.getJobType(), "节点类型不允许为空!");
JobTaskSchedule schedule = new JobTaskSchedule(); Long triggerTime = System.currentTimeMillis();
JobTaskSchedule schedule = new JobTaskSchedule();
schedule.setNodeId(Integer.valueOf(runNode.getNodeId())); schedule.setNodeId(Integer.valueOf(runNode.getNodeId()));
schedule.setRunId(runId); schedule.setRunId(runId);
schedule.setNodeName(runNode.getNodeName()); schedule.setNodeName(runNode.getNodeName());
...@@ -50,13 +59,23 @@ public class ApiNodeServiceImpl implements ApiNodeService { ...@@ -50,13 +59,23 @@ public class ApiNodeServiceImpl implements ApiNodeService {
schedule.setRunSource(runNode.getRunSource()); schedule.setRunSource(runNode.getRunSource());
schedule.setScriptUrls(runNode.getScriptUrl()); schedule.setScriptUrls(runNode.getScriptUrl());
schedule.setJobType(runNode.getJobType()); schedule.setJobType(runNode.getJobType());
schedule.setTriggerTime(System.currentTimeMillis()); schedule.setTriggerTime(triggerTime);
jobTaskScheduleMapper.saveJobTaskSchedule(schedule); schedule.setRunParam(PlaceholderUtils.formatParam(schedule.getRunParam()));
JobTaskRunLogWithBLOBs jobTaskRunLog = new JobTaskRunLogWithBLOBs();
BeanUtils.copyProperties(schedule, jobTaskRunLog);
jobTaskRunLogMapper.saveJobTaskRunLog(jobTaskRunLog);
//获取日志id
Integer logId= jobTaskRunLog.getLogId();
TimerTask timerTask = new ScriptExecutorJobTask(schedule);
WorkRoulette.addJob(timerTask, triggerTime);
RunLog runLog = RunLog.builder().isEnd(false).runLog("等待服务器分配资源").build(); RunLog runLog = RunLog.builder().isEnd(false).runLog("等待服务器分配资源").build();
stringRedisTemplate.convertAndSend(runId, JSON.toJSONString(runLog, WriteClassName)); String runKey = "REAL-EXEC-" + logId;
stringRedisTemplate.convertAndSend(runKey, JSON.toJSONString(runLog, WriteClassName));
return runId; return runKey;
} }
} }
...@@ -70,6 +70,12 @@ ...@@ -70,6 +70,12 @@
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.3.2.RELEASE</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
......
package com.byit.config; package com.byit.conf;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
......
package com.byit.task; package com.byit.task;
import com.alibaba.fastjson.JSON;
import com.byit.conf.MythJobAutoConfigure; import com.byit.conf.MythJobAutoConfigure;
import com.byit.dto.plugin.RunLog;
import com.byit.enums.EmailEnum; import com.byit.enums.EmailEnum;
import com.byit.enums.NodePropertyEnum; import com.byit.enums.NodePropertyEnum;
import com.byit.enums.NodeRunStatusPropertyEnum; import com.byit.enums.NodeRunStatusPropertyEnum;
...@@ -19,9 +21,12 @@ import com.byit.util.SpringUtil; ...@@ -19,9 +21,12 @@ import com.byit.util.SpringUtil;
import io.netty.util.Timeout; import io.netty.util.Timeout;
import io.netty.util.TimerTask; import io.netty.util.TimerTask;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import java.util.Date; import java.util.Date;
import static com.alibaba.fastjson.serializer.SerializerFeature.WriteClassName;
/** /**
* @author huangfu * @author huangfu
*/ */
...@@ -101,7 +106,10 @@ public class ScriptExecutorJobTask implements TimerTask { ...@@ -101,7 +106,10 @@ public class ScriptExecutorJobTask implements TimerTask {
try{ try{
dispatchResponseDto = runScriptService.runScript(scriptDto); dispatchResponseDto = runScriptService.runScript(scriptDto);
}catch (RpcException rpcException){ }catch (RpcException rpcException){
log.error("------执行机异常{}-----",rpcException.getMessage()); StringRedisTemplate stringRedisTemplate = (StringRedisTemplate) SpringUtil.getBean("stringRedisTemplate");
RunLog runLog = RunLog.builder().isEnd(true).isSuccess(false).runLog("执行资源异常" + rpcException.getMessage()).build();
stringRedisTemplate.convertAndSend("REAL-EXEC-" + mythJobTaskSchedule.getLogId() , JSON.toJSONString(runLog, WriteClassName));
log.error("------执行机异常{}-----", rpcException.getMessage());
dispatchResponseDto.setMsg(rpcException.getMessage()); dispatchResponseDto.setMsg(rpcException.getMessage());
dispatchResponseDto.setCode(JobResultEnum.DISPATCH_FAIL.getRes()); dispatchResponseDto.setCode(JobResultEnum.DISPATCH_FAIL.getRes());
} }
......
...@@ -14,6 +14,7 @@ import com.byit.thread.BaseThreadRunHelper; ...@@ -14,6 +14,7 @@ import com.byit.thread.BaseThreadRunHelper;
import com.byit.util.SpringUtil; import com.byit.util.SpringUtil;
import io.netty.util.TimerTask; import io.netty.util.TimerTask;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.sql.DataSource; import javax.sql.DataSource;
...@@ -95,16 +96,8 @@ public class ScheduleThreadRunHelper extends BaseThreadRunHelper { ...@@ -95,16 +96,8 @@ public class ScheduleThreadRunHelper extends BaseThreadRunHelper {
private Integer saveLog(JobTaskSchedule mythJobTaskSchedule){ private Integer saveLog(JobTaskSchedule mythJobTaskSchedule){
JobTaskRunLogWithBLOBs jobTaskRunLog = new JobTaskRunLogWithBLOBs(); JobTaskRunLogWithBLOBs jobTaskRunLog = new JobTaskRunLogWithBLOBs();
BeanUtils.copyProperties(mythJobTaskSchedule, jobTaskRunLog);
jobTaskRunLog.setRunId(mythJobTaskSchedule.getRunId()); jobTaskRunLog.setLogId(null);
jobTaskRunLog.setIsVirtual(mythJobTaskSchedule.getIsVirtual());
jobTaskRunLog.setFlowId(mythJobTaskSchedule.getFlowId());
jobTaskRunLog.setFlowName(mythJobTaskSchedule.getFlowName());
jobTaskRunLog.setNodeId(mythJobTaskSchedule.getNodeId());
jobTaskRunLog.setNodeName(mythJobTaskSchedule.getNodeName());
jobTaskRunLog.setRunParams(mythJobTaskSchedule.getRunParam());
jobTaskRunLog.setFailedRemainingCount(mythJobTaskSchedule.getFailedRetryCount());
jobTaskRunLog.setJobType(mythJobTaskSchedule.getJobType());
JobTaskRunLogServiceImpl mythJobTaskRunLogService = SpringUtil.getBean(JobTaskRunLogServiceImpl.class); JobTaskRunLogServiceImpl mythJobTaskRunLogService = SpringUtil.getBean(JobTaskRunLogServiceImpl.class);
mythJobTaskRunLogService.saveJobTaskRunLog(jobTaskRunLog); mythJobTaskRunLogService.saveJobTaskRunLog(jobTaskRunLog);
......
...@@ -53,15 +53,15 @@ public class MythJobProcess implements Callable<String>{ ...@@ -53,15 +53,15 @@ public class MythJobProcess implements Callable<String>{
private String effectiveUser = null; private String effectiveUser = null;
//本次任务对应的日志id //本次任务对应的日志id
private Integer logId; private Integer logId;
//redis的发布key
private String runId;
//执行状态,默认是失败 //执行状态,默认是失败
private int exitCode = 1; private int exitCode = 1;
//redis模板 //redis模板
private StringRedisTemplate stringRedisTemplate; private StringRedisTemplate stringRedisTemplate;
//redis的key
private String runKey;
public MythJobProcess(final List<String> cmd, final Map<String, String> env, public MythJobProcess(final List<String> cmd, final Map<String, String> env,
final String workingDir, final Integer logId, final String runId, final StringRedisTemplate stringRedisTemplate) { final String workingDir, final Integer logId, final StringRedisTemplate stringRedisTemplate) {
this.cmd = cmd; this.cmd = cmd;
this.env = env; this.env = env;
this.workingDir = workingDir; this.workingDir = workingDir;
...@@ -69,14 +69,14 @@ public class MythJobProcess implements Callable<String>{ ...@@ -69,14 +69,14 @@ public class MythJobProcess implements Callable<String>{
this.startupLatch = new CountDownLatch(1); this.startupLatch = new CountDownLatch(1);
this.completeLatch = new CountDownLatch(1); this.completeLatch = new CountDownLatch(1);
this.logId = logId; this.logId = logId;
this.runId = runId;
this.stringRedisTemplate = stringRedisTemplate; this.stringRedisTemplate = stringRedisTemplate;
this.runKey = "REAL-EXEC-" + logId;
} }
public MythJobProcess(final List<String> cmd, final Map<String, String> env, public MythJobProcess(final List<String> cmd, final Map<String, String> env,
final String workingDir, final String executeAsUserBinary, final String workingDir, final String executeAsUserBinary,
final String effectiveUser, Integer logId, final String runId, final StringRedisTemplate stringRedisTemplate) { final String effectiveUser, Integer logId, final String runId, final StringRedisTemplate stringRedisTemplate) {
this(cmd, env, workingDir, logId, runId, stringRedisTemplate); this(cmd, env, workingDir, logId, stringRedisTemplate);
this.isExecuteAsUser = true; this.isExecuteAsUser = true;
this.executeAsUserBinary = executeAsUserBinary; this.executeAsUserBinary = executeAsUserBinary;
this.effectiveUser = effectiveUser; this.effectiveUser = effectiveUser;
...@@ -121,8 +121,8 @@ public class MythJobProcess implements Callable<String>{ ...@@ -121,8 +121,8 @@ public class MythJobProcess implements Callable<String>{
log.info("进程的id " + this.processId); log.info("进程的id " + this.processId);
} }
outputGobbler = new LogGobbler(new InputStreamReader(this.process.getInputStream(), "utf-8"), Boolean.FALSE, 30, runId, stringRedisTemplate); outputGobbler = new LogGobbler(new InputStreamReader(this.process.getInputStream(), "utf-8"), Boolean.FALSE, 30, runKey, stringRedisTemplate);
errorGobbler = new LogGobbler(new InputStreamReader(this.process.getErrorStream(), "utf-8"), Boolean.TRUE, 30, runId, stringRedisTemplate); errorGobbler = new LogGobbler(new InputStreamReader(this.process.getErrorStream(), "utf-8"), Boolean.TRUE, 30, runKey, stringRedisTemplate);
//开始获取进程的运行日志 //开始获取进程的运行日志
outputGobbler.start(); outputGobbler.start();
errorGobbler.start(); errorGobbler.start();
...@@ -154,7 +154,7 @@ public class MythJobProcess implements Callable<String>{ ...@@ -154,7 +154,7 @@ public class MythJobProcess implements Callable<String>{
this.process.destroy(); this.process.destroy();
} }
RunLog runLog = RunLog.builder().isEnd(true).isSuccess(exitCode == 0 ? true : false).build(); RunLog runLog = RunLog.builder().isEnd(true).isSuccess(exitCode == 0 ? true : false).build();
stringRedisTemplate.convertAndSend(runId, JSON.toJSONString(runLog, WriteClassName)); stringRedisTemplate.convertAndSend(runKey, JSON.toJSONString(runLog, WriteClassName));
return outputGobbler.getRecentLog() + "\n" + errorGobbler.getRecentLog(); return outputGobbler.getRecentLog() + "\n" + errorGobbler.getRecentLog();
} }
} }
......
...@@ -22,14 +22,14 @@ public class LogGobbler extends Thread { ...@@ -22,14 +22,14 @@ public class LogGobbler extends Thread {
private final BufferedReader inputReader; private final BufferedReader inputReader;
private final boolean isError; private final boolean isError;
private final StringBuilder buffer; private final StringBuilder buffer;
private final String runId; private final String runKey;
private final StringRedisTemplate stringRedisTemplate; private final StringRedisTemplate stringRedisTemplate;
public LogGobbler(final Reader inputReader, final boolean isError, final int bufferLines, final String runId, final StringRedisTemplate stringRedisTemplate) { public LogGobbler(final Reader inputReader, final boolean isError, final int bufferLines, final String runKey, final StringRedisTemplate stringRedisTemplate) {
this.inputReader = new BufferedReader(inputReader); this.inputReader = new BufferedReader(inputReader);
this.isError = isError; this.isError = isError;
this.buffer = new StringBuilder(bufferLines); this.buffer = new StringBuilder(bufferLines);
this.runId = runId; this.runKey = runKey;
this.stringRedisTemplate = stringRedisTemplate; this.stringRedisTemplate = stringRedisTemplate;
} }
...@@ -44,7 +44,7 @@ public class LogGobbler extends Thread { ...@@ -44,7 +44,7 @@ public class LogGobbler extends Thread {
} }
this.buffer.append(line + "\n"); this.buffer.append(line + "\n");
runLog.setRunLog(line); runLog.setRunLog(line);
stringRedisTemplate.convertAndSend(runId, JSON.toJSONString(runLog, WriteClassName)); stringRedisTemplate.convertAndSend(runKey, JSON.toJSONString(runLog, WriteClassName));
printLog(line); printLog(line);
} }
} catch (final IOException e) { } catch (final IOException e) {
......
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