use of org.apache.commons.exec.PumpStreamHandler in project Saturn by vipshop.
the class ScriptPidUtils method exeCmdWithoutPipe.
public static String exeCmdWithoutPipe(CommandLine cmdLine, ByteArrayInputStream input, Map<String, String> env) {
DefaultExecutor executor = new DefaultExecutor();
ExecuteWatchdog dog = new ExecuteWatchdog(3 * 1000);
executor.setWatchdog(dog);
executor.setExitValue(0);
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
SaturnLogOutputStream errorOS = new SaturnLogOutputStream(log, SaturnLogOutputStream.LEVEL_ERROR);
PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream, errorOS, input);
executor.setStreamHandler(streamHandler);
LogUtils.info(log, LogEvents.ExecutorEvent.COMMON, "exec command: {}", cmdLine);
int value = executor.execute(cmdLine, env);
if (value == 0) {
String out = outputStream.toString();
return out;
} else {
return null;
}
} catch (Exception e) {
LogUtils.error(log, LogEvents.ExecutorEvent.COMMON, e.getMessage(), e);
return null;
}
}
use of org.apache.commons.exec.PumpStreamHandler in project Saturn by vipshop.
the class ScriptJobRunner method execute.
private SaturnJobReturn execute(long timeoutSeconds) {
SaturnJobReturn saturnJobReturn;
ProcessOutputStream processOutputStream = new ProcessOutputStream(1);
DefaultExecutor executor = new DefaultExecutor();
PumpStreamHandler streamHandler = new PumpStreamHandler(processOutputStream);
// 关闭线程等待时间, (注意commons-exec会固定增加2秒的addition)
streamHandler.setStopTimeout(timeoutSeconds * 1000);
executor.setExitValue(0);
executor.setStreamHandler(streamHandler);
executor.setWatchdog(getWatchdog());
// filter env key in execParameter. like cd ${mypath} -> cd /root/my.
Map<String, String> env = ScriptPidUtils.loadEnv();
CommandLine commandLine = createCommandLine(env);
try {
long start = System.currentTimeMillis();
LogUtils.info(log, jobName, "Begin executing {}-{} {}", jobName, item, commandLine);
int exitValue = executor.execute(commandLine, env);
long end = System.currentTimeMillis();
LogUtils.info(log, jobName, "Finish executing {}-{} {}, the exit value is {}, cost={}ms", jobName, item, commandLine, exitValue, (end - start));
SaturnJobReturn tmp = readSaturnJobReturn();
if (tmp == null) {
tmp = new SaturnJobReturn("the exit value is " + exitValue);
}
saturnJobReturn = tmp;
} catch (Exception e) {
saturnJobReturn = handleException(timeoutSeconds, e);
} finally {
try {
// 将日志set进jobLog, 写不写zk再由ExecutionService控制
handleJobLog(processOutputStream.getJobLog());
processOutputStream.close();
} catch (Exception ex) {
LogUtils.error(log, jobName, "{}-{} Error at closing output stream. Should not be concern: {}", jobName, item, ex.getMessage(), ex);
}
stopStreamHandler(streamHandler);
ScriptPidUtils.removePidFile(job.getExecutorName(), jobName, "" + item, watchdog.getPid());
}
return saturnJobReturn;
}
use of org.apache.commons.exec.PumpStreamHandler in project ddf by codice.
the class VideoThumbnailPlugin method getVideoDuration.
private Duration getVideoDuration(final String videoFilePath) throws IOException, InterruptedException {
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
final PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
final CommandLine command = getFFmpegInfoCommand(videoFilePath);
final DefaultExecuteResultHandler resultHandler = executeFFmpeg(command, 15, streamHandler);
resultHandler.waitFor();
return parseVideoDuration(outputStream.toString(StandardCharsets.UTF_8.name()));
}
use of org.apache.commons.exec.PumpStreamHandler in project stanbol by apache.
the class JarExecutor method start.
/**
* Start the jar if not done yet, and setup runtime hook to stop it.
*/
public void start() throws Exception {
final ExecuteResultHandler h = new ExecuteResultHandler() {
@Override
public void onProcessFailed(ExecuteException ex) {
log.error("Process execution failed:" + ex, ex);
}
@Override
public void onProcessComplete(int result) {
log.info("Process execution complete, exit code=" + result);
}
};
final String vmOptions = System.getProperty("jar.executor.vm.options");
final Executor e = new DefaultExecutor();
if (this.workingDirectory != null) {
e.setWorkingDirectory(this.workingDirectory);
}
final CommandLine cl = new CommandLine(javaExecutable);
if (vmOptions != null && vmOptions.length() > 0) {
// not the case for common usage patterns
for (String option : StringUtils.split(vmOptions, " ")) {
cl.addArgument(option);
}
}
cl.addArgument("-jar");
cl.addArgument(jarToExecute.getAbsolutePath());
cl.addArgument("-p");
cl.addArgument(String.valueOf(serverPort));
log.info("Executing " + cl);
e.setStreamHandler(new PumpStreamHandler());
e.setProcessDestroyer(new ShutdownHookProcessDestroyer());
e.execute(cl, h);
}
use of org.apache.commons.exec.PumpStreamHandler in project project-build-plugin by axonivy.
the class EngineControl method createEngineLogStreamForwarder.
private PumpStreamHandler createEngineLogStreamForwarder(Consumer<String> logLineHandler) throws FileNotFoundException {
OutputStream output = getEngineLogTarget();
OutputStream engineLogStream = new LineOrientedOutputStreamRedirector(output) {
@Override
protected void processLine(byte[] b) throws IOException {
// write file log
super.processLine(b);
String line = new String(b);
context.log.debug("engine: " + line);
if (logLineHandler != null) {
logLineHandler.accept(line);
}
}
};
PumpStreamHandler streamHandler = new PumpStreamHandler(engineLogStream, System.err) {
@Override
public void stop() throws IOException {
super.stop();
// we opened the stream - we're responsible to close it!
engineLogStream.close();
}
};
return streamHandler;
}
Aggregations