public static IStreamProcessor wrap(IStreamProcessor destination) { return new StreamToLineProcessor(destination); } }
@Override public void onProcessed() { if (buffer.length() > 0) { destination.process(getAndClearBuffer()); } destination.onProcessed(); }
public static boolean isProcessRunning(Platform platform, long pid) { try { final Process pidof; if (platform.isUnixLike()) { pidof = Runtime.getRuntime().exec( new String[] { "kill", "-0", "" + pid }); return pidof.waitFor() == 0; } else { // windows // process might be in either NOT RESPONDING due to // firewall blocking, or could be RUNNING final String[] cmd = { "tasklist.exe", "/FI", "PID eq " + pid ,"/FO", "CSV" }; logger.trace("Command: {}", Arrays.asList(cmd)); ProcessBuilder processBuilder = ProcessControl .newProcessBuilder(Arrays.asList(cmd), true); Process process = processBuilder.start(); // look for the PID in the output, pass it in for 'success' state LogWatchStreamProcessor logWatch = new LogWatchStreamProcessor(""+pid, new HashSet<String>(), StreamToLineProcessor.wrap(Processors.silent())); Processors.connect(new InputStreamReader(process.getInputStream()), logWatch); logWatch.waitForResult(2000); logger.trace("logWatch output: {}", logWatch.getOutput()); return logWatch.isInitWithSuccess(); } } catch (IOException | InterruptedException e) { logger.error("Trying to get process status", e); } return false; }
new HashSet<>(), StreamToLineProcessor.wrap(Processors.silent())); Processors.connect(new InputStreamReader(process.getInputStream()), logWatch); logWatch.waitForResult(2000);
void run(Process p, IRuntimeConfig runtimeConfig, long timeoutNanos) throws IOException { CollectingAndForwardingStreamProcessor wrapped = new CollectingAndForwardingStreamProcessor(runtimeConfig.getProcessOutput().getOutput()); IStreamProcessor loggingWatch = StreamToLineProcessor.wrap(wrapped); try { ReaderProcessor processorOne = Processors.connect(new InputStreamReader(p.getInputStream()), loggingWatch); ReaderProcessor processorTwo = Processors.connect(new InputStreamReader(p.getErrorStream()), loggingWatch); int retCode = tope.waitFor(p, timeoutNanos); if (retCode != 0) { processorOne.join(10000); processorTwo.join(10000); resolveException(retCode, wrapped.getOutput()); } } catch (InterruptedException e) { throw new RuntimeException(e); } }
@Override public void process(String block) { int newLineEnd = block.indexOf('\n'); if (newLineEnd == -1) { buffer.append(block); } else { buffer.append(block.substring(0, newLineEnd + 1)); destination.process(getAndClearBuffer()); do { int lastEnd = newLineEnd; newLineEnd = block.indexOf('\n', newLineEnd + 1); if (newLineEnd != -1) { destination.process(block.substring(lastEnd + 1, newLineEnd + 1)); } else { buffer.append(block.substring(lastEnd + 1)); } } while (newLineEnd != -1); } }
public static StreamProcessor wrap(StreamProcessor destination) { return new StreamToLineProcessor(destination); } }
protected boolean tryKillToProcess() { if (processId > 0) { return Processes.tryKillProcess(config.supportConfig(), distribution.getPlatform(), StreamToLineProcessor.wrap(runtimeConfig.getProcessOutput().getCommands()), processId); } return false; }
@Override public void onProcessed() { if (buffer.length() > 0) { destination.process(getAndClearBuffer()); } destination.onProcessed(); }
protected boolean sendKillToProcess() { if (processId > 0) { return Processes.killProcess(config.supportConfig(), distribution.getPlatform(), StreamToLineProcessor.wrap(runtimeConfig.getProcessOutput().getCommands()), processId); } return false; }
@Override public void process(String block) { int newLineEnd = block.indexOf('\n'); if (newLineEnd == -1) { buffer.append(block); } else { buffer.append(block.substring(0, newLineEnd + 1)); destination.process(getAndClearBuffer()); do { int lastEnd = newLineEnd; newLineEnd = block.indexOf('\n', newLineEnd + 1); if (newLineEnd != -1) { destination.process(block.substring(lastEnd + 1, newLineEnd + 1)); } else { buffer.append(block.substring(lastEnd + 1)); } } while (newLineEnd != -1); } }
@Override protected void onAfterProcessStart(ProcessControl process, IRuntimeConfig runtimeConfig) throws IOException { final ProcessOutput outputConfig = runtimeConfig.getProcessOutput(); final LogWatchStreamProcessor logWatch = new LogWatchStreamProcessor( "performing post-bootstrap initialization", singleton("[initdb error]"), StreamToLineProcessor.wrap(outputConfig.getOutput())); Processors.connect(process.getReader(), logWatch); Processors.connect(process.getError(), StreamToLineProcessor.wrap(outputConfig.getError())); logWatch.waitForResult(getConfig().timeout().startupTimeout()); } }
protected boolean sendTermToProcess() { if (processId > 0) { return Processes.termProcess(config.supportConfig(), distribution.getPlatform(), StreamToLineProcessor.wrap(runtimeConfig.getProcessOutput().getCommands()), processId); } return false; }
protected void onAfterProcessStart(ProcessControl process, RuntimeConfig runtimeConfig) { ProcessOutput outputConfig = runtimeConfig.getProcessOutput(); Processors.connect(process.getReader(), outputConfig.getOutput()); Processors.connect(process.getError(), StreamToLineProcessor.wrap(outputConfig.getError())); }
protected boolean tryKillToProcess() { return getProcessId() > 0 && Processes.tryKillProcess(config.supportConfig(), distribution.platform(), StreamToLineProcessor.wrap(runtimeConfig.getProcessOutput().getCommands()), getProcessId()); }
protected void onAfterProcessStart(ProcessControl process, IRuntimeConfig runtimeConfig) throws IOException { ProcessOutput outputConfig = runtimeConfig.getProcessOutput(); Processors.connect(process.getReader(), outputConfig.getOutput()); Processors.connect(process.getError(), StreamToLineProcessor.wrap(outputConfig.getError())); }
protected boolean sendKillToProcess() { return getProcessId() > 0 && Processes.killProcess(config.supportConfig(), distribution.platform(), StreamToLineProcessor.wrap(runtimeConfig.getProcessOutput().getCommands()), getProcessId()); }
protected boolean sendTermToProcess() { return getProcessId() > 0 && Processes.termProcess(config.supportConfig(), distribution.platform(), StreamToLineProcessor.wrap(runtimeConfig.getProcessOutput().getCommands()), getProcessId()); }
@Override public void onAfterProcessStart(final ProcessControl process, final IRuntimeConfig runtimeConfig) throws IOException { outputWatch = new NotifyingStreamProcessor(StreamToLineProcessor.wrap(runtimeConfig.getProcessOutput().getOutput())); Processors.connect(process.getReader(), outputWatch); Processors.connect(process.getError(), outputWatch); ResultMatchingListener startupListener = outputWatch.addListener(new ResultMatchingListener("ready for connections")); try { startupListener.waitForResult(getConfig().getTimeout(MILLISECONDS)); if (!startupListener.isInitWithSuccess()) { throw new RuntimeException("mysql start failed with error: " + startupListener.getFailureFound()); } } catch (Exception e) { // emit IO exception for {@link AbstractProcess} would try to stop running process gracefully throw new IOException(e); } }