mirror of
https://github.com/MarginaliaSearch/MarginaliaSearch.git
synced 2025-02-24 05:18:58 +00:00
(control) Don't spin on process output printing
This is the "correct" way of copying stdout and stderr to the curren't process' output.
This commit is contained in:
parent
46409c4c2d
commit
c7f0276005
@ -11,6 +11,7 @@ import org.slf4j.MarkerFactory;
|
|||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import javax.inject.Singleton;
|
import javax.inject.Singleton;
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
@ -72,24 +73,16 @@ public class ProcessService {
|
|||||||
processes.put(processId, process);
|
processes.put(processId, process);
|
||||||
}
|
}
|
||||||
|
|
||||||
try (var es = new BufferedReader(new InputStreamReader(process.getErrorStream()));
|
try {
|
||||||
var os = new BufferedReader(new InputStreamReader(process.getInputStream()))
|
new Thread(new ProcessLogStderr(process)).start();
|
||||||
) {
|
new Thread(new ProcessLogStdout(process)).start();
|
||||||
eventLog.logEvent("PROCESS-STARTED", processId.toString());
|
|
||||||
|
|
||||||
while (process.isAlive()) {
|
|
||||||
if (es.ready())
|
|
||||||
logger.warn(processMarker, es.readLine());
|
|
||||||
if (os.ready())
|
|
||||||
logger.info(processMarker, os.readLine());
|
|
||||||
}
|
|
||||||
|
|
||||||
final int returnCode = process.waitFor();
|
final int returnCode = process.waitFor();
|
||||||
logger.info("Process {} terminated with code {}", processId, returnCode);
|
logger.info("Process {} terminated with code {}", processId, returnCode);
|
||||||
return 0 == returnCode;
|
return 0 == returnCode;
|
||||||
}
|
}
|
||||||
catch (Exception ex) {
|
catch (Exception ex) {
|
||||||
logger.info("Process {} terminated with code exception", processId);
|
logger.info("Process {} terminated with exception", processId);
|
||||||
throw ex;
|
throw ex;
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
@ -98,6 +91,7 @@ public class ProcessService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private String[] createCommandArguments(String processPath, String[] parameters) {
|
private String[] createCommandArguments(String processPath, String[] parameters) {
|
||||||
final String[] args = new String[parameters.length + 1];
|
final String[] args = new String[parameters.length + 1];
|
||||||
args[0] = processPath;
|
args[0] = processPath;
|
||||||
@ -153,4 +147,49 @@ public class ProcessService {
|
|||||||
return key + "=" + val;
|
return key + "=" + val;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class ProcessLogStderr implements Runnable {
|
||||||
|
private final Process process;
|
||||||
|
|
||||||
|
public ProcessLogStderr(Process process) {
|
||||||
|
this.process = process;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try (var es = new BufferedReader(new InputStreamReader(process.getErrorStream()))) {
|
||||||
|
String line;
|
||||||
|
while (((line = es.readLine()) != null)) {
|
||||||
|
logger.warn(processMarker, line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (IOException ex) {
|
||||||
|
logger.error("Error reading process error stream", ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class ProcessLogStdout implements Runnable {
|
||||||
|
private final Process process;
|
||||||
|
|
||||||
|
public ProcessLogStdout(Process process) {
|
||||||
|
this.process = process;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try (var is = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
|
||||||
|
String line;
|
||||||
|
while (((line = is.readLine()) != null)) {
|
||||||
|
logger.info(processMarker, line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (IOException ex) {
|
||||||
|
logger.error("Error reading process output stream", ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user