Implement external program call

This commit is contained in:
Nathan McRae 2025-04-15 22:41:09 -07:00
parent c8aa18338a
commit a7148e247c

View File

@ -17,6 +17,7 @@ import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;
import java.util.Date;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
@ -29,6 +30,7 @@ import javafx.scene.image.Image;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.util.Pair;
import org.apache.commons.cli.*;
public class Main extends Application {
@ -174,6 +176,14 @@ public class Main extends Application {
public StationRunException(String message) {
super(message);
}
public StationRunException(Throwable cause) {
super(cause);
}
public StationRunException(String message, Throwable cause) {
super(message, cause);
}
}
public static void runStation(MainParsedArguments parsedArgs) throws StationRunException {
@ -223,6 +233,21 @@ public class Main extends Application {
}
switch (loadedStation.getMessageMethod()) {
case StationSettings.MessageMethod.EXTERNAL_PROGRAM:
Process process = new ProcessBuilder(loadedStation.getExternalProgramCommand(), nextMessagePath.toString()).start();
// TODO: make timeout a setting
if (!process.waitFor(5, TimeUnit.SECONDS)) {
throw new StationRunException("Timeout while running external program " + loadedStation.getExternalProgramCommand());
}
Pair<String, String> output = WindowsScheduler.captureProcessOutput(process);
if(process.exitValue() != 0) {
String message = "External program non-zero exit code. Exit code: " + process.exitValue() + ". stdout: " + output.getKey() + "\n\tstderr: " + output.getValue();
logger.log(Level.SEVERE, message);
throw new StationRunException(message);
}
break;
case StationSettings.MessageMethod.SFTP:
//loadedStation.uploadNextMessageToSFTP();
JSch jsch = new JSch();