Commit a7396bf6 by guo_minglei@163.com

执行器添加上下文

parent ef9aa1ad
package com.byit.executor.jobExecutor.process;
import com.byit.executor.util.JobContentUtil;
import com.byit.executor.util.LogGobbler;
import com.google.common.base.Joiner;
import lombok.extern.slf4j.Slf4j;
......@@ -44,9 +45,11 @@ public class MythJobProcess {
private String executeAsUserBinary = null;
//有效用户
private String effectiveUser = null;
//本次任务对应的日志id
private Integer logId;
public MythJobProcess(final List<String> cmd, final Map<String, String> env,
final String workingDir) {
final String workingDir, final Integer logId) {
this.cmd = cmd;
this.env = env;
this.workingDir = workingDir;
......@@ -57,8 +60,8 @@ public class MythJobProcess {
public MythJobProcess(final List<String> cmd, final Map<String, String> env,
final String workingDir, final String executeAsUserBinary,
final String effectiveUser) {
this(cmd, env, workingDir);
final String effectiveUser, Integer logId) {
this(cmd, env, workingDir, logId);
this.isExecuteAsUser = true;
this.executeAsUserBinary = executeAsUserBinary;
this.effectiveUser = effectiveUser;
......@@ -75,6 +78,8 @@ public class MythJobProcess {
//标记为开始运行
this.startupLatch.countDown();
//记录上下文内容
JobContentUtil.doPut(logId, this);
final ProcessBuilder builder = new ProcessBuilder(this.cmd);
if (!Objects.isNull(workingDir)){
......@@ -126,6 +131,8 @@ public class MythJobProcess {
this.process.getInputStream().close();
this.process.getOutputStream().close();
this.process.getErrorStream().close();
//运行完毕将本任务移除上下文内容
JobContentUtil.remove(logId);
if (this.process.isAlive()){
this.process.destroy();
}
......
package com.byit.executor.util;
import com.byit.executor.jobExecutor.process.MythJobProcess;
import java.util.HashMap;
/**
* 脚本运行的上下文内容存储
*/
public class JobContentUtil {
//任务总数
private static Integer jobNum = 0;
//正在运行的任务总数
private static Integer runIngJob = 0;
//正在等待运行的任务总数
private static Integer watingJob = 0;
//任务对应的日志id和对应的线程对象
private static HashMap<Integer, MythJobProcess> jobContent = new HashMap<>(10);
public static synchronized void doPut(Integer logId, MythJobProcess jobProcess){
jobNum++;
watingJob++;
jobContent.put(logId, jobProcess);
}
public static synchronized void remove(Integer logId){
jobNum--;
runIngJob--;
MythJobProcess jobProcess = jobContent.get(logId);
if (jobProcess != null){
jobContent.remove(logId, jobProcess);
}
}
public static synchronized MythJobProcess getJobThread(Integer logId){
MythJobProcess jobProcess = jobContent.get(logId);
if (jobProcess != null){
return jobProcess;
}
return null;
}
public static synchronized void runJobThread(Integer logId){
MythJobProcess jobProcess = jobContent.get(logId);
if (jobProcess != null){
watingJob--;
jobNum++;
}
}
}
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