Skip to content

Commit

Permalink
#348 when java exec sh script, we should redirect the error stream to…
Browse files Browse the repository at this point in the history
… log
  • Loading branch information
heziai committed Jan 25, 2018
1 parent 794ba47 commit 0f75ae9
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 94 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package com.vip.saturn.job.basic;

import java.util.concurrent.atomic.AtomicInteger;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.vip.saturn.job.SaturnJobExecutionContext;
import com.vip.saturn.job.SaturnJobReturn;
import com.vip.saturn.job.SaturnSystemErrorGroup;
import com.vip.saturn.job.SaturnSystemReturnCode;
import com.vip.saturn.job.java.SaturnJavaJob;
import com.vip.saturn.job.utils.SaturnLogOutputStream;
import com.vip.saturn.job.utils.SaturnSystemOutputStream;
import java.util.concurrent.atomic.AtomicInteger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* @author xiaopeng.he
Expand All @@ -28,15 +26,13 @@ public JavaShardingItemCallable(String jobName, Integer item, String itemValue,

/**
* 获取执行作业分片的线程
* @return
*/
public Thread getCurrentThread() {
return currentThread;
}

/**
* 设置执行作业分片的线程
* @param currentThread
*/
public void setCurrentThread(Thread currentThread) {
this.currentThread = currentThread;
Expand All @@ -55,10 +51,6 @@ public void setCurrentThread(Thread currentThread) {

/**
* 复制对象
* @param source
* @param classLoader
* @return
* @throws Exception
*/
public static Object cloneObject(Object source, ClassLoader classLoader) throws Exception {
if (source == null) {
Expand All @@ -72,9 +64,6 @@ public static Object cloneObject(Object source, ClassLoader classLoader) throws

/**
* 生成分片上下文对象
* @param jobClassLoader
* @return
* @throws Exception
*/
public Object getContextForJob(ClassLoader jobClassLoader) throws Exception {
if (contextForJob == null) {
Expand Down Expand Up @@ -104,31 +93,27 @@ public boolean setTimeout() {

/**
* 该分片执行是否TIMEOUT
* @return
*/
public boolean isTimeout() {
return status.get() == TIMEOUT;
}

/**
* 设置该分片的状态为FORCE_STOP
* @return
*/
public boolean forceStop() {
return status.compareAndSet(INIT, FORCE_STOP);
}

/**
* 作业执行是否被中止
* @return
*/
public boolean isBreakForceStop() {
return breakForceStop;
}

/**
* 该分片是否FORCE_STOP状态
* @return
*/
public boolean isForceStop() {
return status.get() == FORCE_STOP;
Expand Down Expand Up @@ -165,7 +150,7 @@ public void afterExecution() {
public SaturnJobReturn call() {
reset();

SaturnLogOutputStream.initLogger();
SaturnSystemOutputStream.initLogger();
currentThread = Thread.currentThread();
SaturnJobReturn temp = null;
try {
Expand All @@ -192,7 +177,7 @@ public SaturnJobReturn call() {
saturnJobReturn = temp;
}

String jobLog = SaturnLogOutputStream.clearAndGetLog();
String jobLog = SaturnSystemOutputStream.clearAndGetLog();

if (saturnJob != null && saturnJob.getConfigService().showNormalLog()) {
this.shardingContext.putJobLog(this.item, jobLog);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,61 +1,29 @@
package com.vip.saturn.job.utils;

import java.io.PrintStream;

import org.apache.commons.exec.LogOutputStream;

/**
* 捕获System.out输出
* @author dylan.xue
*
*/
public class SaturnLogOutputStream extends LogOutputStream {
private static final int MAX_LINE = 100;

private static ThreadLocal<LRUList<String>> lists = new InheritableThreadLocal<LRUList<String>>() {
@Override
protected LRUList<String> initialValue() {
return new LRUList<String>(MAX_LINE);
}
};
private static PrintStream catchedOut = new PrintStream(new SaturnLogOutputStream(1));
private static PrintStream out = System.out; // NOSONAR

static {
System.setOut(catchedOut);
}

private SaturnLogOutputStream(int level) {
super(level);
}

protected void processLine(String line, int level) {
LRUList<String> lruList = (LRUList<String>) lists.get();
lruList.put(line);
out.println(line);
}

public static void initLogger() {
lists.get().clear();
}

private static void clearCache() {
lists.remove();
}

public static String clearAndGetLog() {
try {
StringBuilder sb = new StringBuilder();
LRUList<String> lruList = (LRUList<String>) lists.get();
for (String line : lruList) {
sb.append(line).append(System.lineSeparator());
}
lruList.clear();
clearCache();
return sb.toString();
} catch (Exception e) {// NOSONAR
return "";
}
}

}
package com.vip.saturn.job.utils;

import org.apache.commons.exec.LogOutputStream;
import org.slf4j.Logger;

/**
* @author hebelala
*/
public class SaturnLogOutputStream extends LogOutputStream {

public static final int LEVEL_INFO = 1;
public static final int LEVEL_ERROR = 2;

private Logger log;

public SaturnLogOutputStream(Logger log, int level) {
super(level);
this.log = log;
}

@Override
protected void processLine(String line, int logLevel) {
if (logLevel == LEVEL_INFO) {
log.info(line);
} else if (logLevel == LEVEL_ERROR) {
log.error(line);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.vip.saturn.job.utils;

import java.io.PrintStream;
import org.apache.commons.exec.LogOutputStream;

/**
* 捕获System.out输出
*
* @author dylan.xue
*/
public class SaturnSystemOutputStream extends LogOutputStream {

private static final int MAX_LINE = 100;

private static ThreadLocal<LRUList<String>> lists = new InheritableThreadLocal<LRUList<String>>() {
@Override
protected LRUList<String> initialValue() {
return new LRUList<String>(MAX_LINE);
}
};
private static PrintStream catchedOut = new PrintStream(new SaturnSystemOutputStream(1));
private static PrintStream out = System.out; // NOSONAR

static {
System.setOut(catchedOut);
}

private SaturnSystemOutputStream(int level) {
super(level);
}

protected void processLine(String line, int level) {
LRUList<String> lruList = (LRUList<String>) lists.get();
lruList.put(line);
out.println(line);
}

public static void initLogger() {
lists.get().clear();
}

private static void clearCache() {
lists.remove();
}

public static String clearAndGetLog() {
try {
StringBuilder sb = new StringBuilder();
LRUList<String> lruList = (LRUList<String>) lists.get();
for (String line : lruList) {
sb.append(line).append(System.lineSeparator());
}
lruList.clear();
clearCache();
return sb.toString();
} catch (Exception e) {// NOSONAR
return "";
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,6 @@
import com.vip.saturn.job.internal.execution.ExecutionNode;
import com.vip.saturn.job.internal.storage.JobNodePath;
import com.vip.saturn.job.reg.base.CoordinatorRegistryCenter;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteWatchdog;
import org.apache.commons.exec.PumpStreamHandler;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
Expand All @@ -27,10 +18,18 @@
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteWatchdog;
import org.apache.commons.exec.PumpStreamHandler;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* 用于处理Shell的相关pid功能
*
*
* @author linzhaoming
*
*/
Expand Down Expand Up @@ -334,8 +333,8 @@ public static String exeCmdWithoutPipe(CommandLine cmdLine, ByteArrayInputStream
executor.setExitValue(0);
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream, errorStream, input);
SaturnLogOutputStream errorOS = new SaturnLogOutputStream(log, SaturnLogOutputStream.LEVEL_ERROR);
PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream, errorOS, input);
executor.setStreamHandler(streamHandler);
int value = executor.execute(cmdLine, env);
if (value == 0) {
Expand Down

0 comments on commit 0f75ae9

Please sign in to comment.