Skip to content

Commit

Permalink
Added other python commands to use when creating the server.py proces…
Browse files Browse the repository at this point in the history
…s fails.
  • Loading branch information
noah1013 committed Jun 4, 2024
1 parent 52cb54e commit 1293ba9
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions app/femr/util/translation/TranslationServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ public static String makeServerRequest(String jsonString, String from, String to
con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
con.connect();

// con = makePOST(portNumber);

try(OutputStream os = con.getOutputStream()) {
os.write(json);
}
Expand All @@ -101,14 +103,35 @@ public static void start(String timeout) {

String logPath = "translator/server.log";
String absPath = "translator/server.py";

String[] validPythonCommands = {"python", "python3", "py", "py3"};
if(serverNotRunning(logPath)){
File log = new File(logPath);


try {
log.createNewFile();
ProcessBuilder pb = new ProcessBuilder("python", absPath, timeout);
pb.redirectOutput(log);
pb.redirectErrorStream(true);
pb.start();
boolean serverStarted = false;

for (String pythonCommand : validPythonCommands){
ProcessBuilder pb = new ProcessBuilder(pythonCommand, absPath, timeout);
pb.redirectOutput(log);
pb.redirectErrorStream(true);

try{
pb.start();
serverStarted = true;
break;
}
catch (IOException e){
System.out.println("Failed to start process with command: " + pythonCommand);
System.out.println(e.getMessage());
}
}

if (!serverStarted){
throw new IOException("All attempts to start the process failed.");
}
} catch (IOException e) {
System.out.println("An I/O error has occurred.");
System.out.println(e.getMessage());
Expand Down

0 comments on commit 1293ba9

Please sign in to comment.