Skip to content
Permalink
Browse files

Improve stability of RunJava runner (Issue #35)

  • Loading branch information
jesse-gallagher committed Mar 3, 2020
1 parent aa3649b commit f0c72b4b2ee317fce1664f182a57a5c18fbe5eb3
@@ -23,6 +23,7 @@
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.openntf.openliberty.domino.log.OpenLibertyLog;
import org.openntf.openliberty.domino.runjava.AddInLogBridge;
import org.openntf.openliberty.domino.runjava.AddinLogPrintStream;

@@ -96,42 +97,39 @@ public void runNotes() throws NotesException {
throw new RuntimeException(format(translate("WLP.errorMqOpen"), status)); //$NON-NLS-1$
}

AddInSetStatusLine(taskId, translate("WLP.statusIdle")); //$NON-NLS-1$
StringBuffer buf = new StringBuffer();
while(addInRunning() && status != ERR_MQ_QUITTING) {
OSPreemptOccasionally();

status = mq.get(buf, MQ_MAX_MSGSIZE, MessageQueue.MQ_WAIT_FOR_MSG, 500);
switch(status) {
case NOERROR: {
String line = buf.toString();
if(StringUtil.isNotEmpty(line)) {

commandQueue.submit(() -> {
// Special handling for exit calls
String result;
if("quit".equalsIgnoreCase(line) || "exit".equalsIgnoreCase(line)) { //$NON-NLS-1$ //$NON-NLS-2$
result = delegate.processCommand("stop"); //$NON-NLS-1$
} else {
result = delegate.processCommand(line);
}
try {
AddInSetStatusLine(taskId, translate("WLP.statusIdle")); //$NON-NLS-1$
StringBuffer buf = new StringBuffer();
while(addInRunning() && status != ERR_MQ_QUITTING) {
OSPreemptOccasionally();

status = mq.get(buf, MQ_MAX_MSGSIZE, MessageQueue.MQ_WAIT_FOR_MSG, 500);
switch(status) {
case NOERROR: {
String line = buf.toString();
if(StringUtil.isNotEmpty(line)) {

if(StringUtil.isNotEmpty(result)) {
AddInLogMessageText(result);
}
});
commandQueue.submit(() -> {
String result = delegate.processCommand(line);
if(StringUtil.isNotEmpty(result)) {
OpenLibertyLog.instance.out.println(result);
}
});
}

break;
}
case ERR_MQ_TIMEOUT:
case ERR_MQ_EMPTY:
case ERR_MQ_QUITTING:
break;
default:
AddInLogErrorText(translate("WLP.unexpectedCodeWhilePolling", status)); //$NON-NLS-1$
break;
}

break;
}
case ERR_MQ_TIMEOUT:
case ERR_MQ_QUITTING:
case ERR_MQ_EMPTY:
break;
default:
AddInLogErrorText(translate("WLP.unexpectedCodeWhilePolling", status)); //$NON-NLS-1$
break;
}
} finally {
mq.close(0);
}
} catch (Exception e) {
AddInLogErrorText(e.getLocalizedMessage());
@@ -144,7 +142,11 @@ public void runNotes() throws NotesException {
} catch (InterruptedException e) {
// Ignore
}
delegate.close();
try {
delegate.close();
} catch(Throwable t) {
t.printStackTrace();
}

AddInSetStatusLine(taskId, translate("WLP.statusTerm")); //$NON-NLS-1$
AddInDeleteStatusLine(taskId);
@@ -24,6 +24,10 @@

public enum OpenLibertyLog {
instance;

public static Logger getLog() {
return instance.log;
}

public final PrintStream out;
public final Logger log;
@@ -31,11 +31,13 @@ public void start() {
*
* <p>This method should be called during full shutdown of the runtime.
*/
@Override
public void close() {
DominoThreadFactory.term();
if(this.runner != null) {
this.runner.cancel(true);
this.runner = null;
}
DominoThreadFactory.term();
}

/**
@@ -25,6 +25,7 @@
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import java.nio.file.StandardWatchEventKinds;
@@ -86,12 +87,17 @@
private List<RuntimeService> runtimeServices = StreamSupport.stream(ServiceLoader.load(RuntimeService.class).spliterator(), false).collect(Collectors.toList());

private Set<String> startedServers = Collections.synchronizedSet(new HashSet<>());
private Set<Process> subprocesses = Collections.synchronizedSet(new HashSet<>());
// Flag used by sendCommand to check whether the whole system is shutting down
private boolean terminating;

private Path javaHome;
private Path execDirectory;
private Logger log;

@Override
public void run() {
Logger log = OpenLibertyLog.instance.log;
log = OpenLibertyLog.instance.log;

if(log.isLoggable(Level.INFO)) {
log.info(format("Startup"));
@@ -105,6 +111,7 @@ public void run() {
if(log.isLoggable(Level.INFO)) {
log.info(format("Using Java runtime located at {0}", javaHome));
}
execDirectory = Paths.get(OpenLibertyUtil.getDominoProgramDirectory());

Path wlp = null;
try {
@@ -129,7 +136,7 @@ public void run() {
}
switch(command.type) {
case START:
sendCommand(wlp, "start", command.args).waitFor(); //$NON-NLS-1$
sendCommand(wlp, "start", command.args); //$NON-NLS-1$
watchLog(wlp, (String)command.args[0]);
break;
case STOP:
@@ -189,16 +196,29 @@ public void run() {
t.printStackTrace(OpenLibertyLog.instance.out);
}
} finally {
terminating = true;
if(wlp != null) {
for(String serverName : startedServers) {
try {
if(log.isLoggable(Level.INFO)) {
log.info("Shutting down server " + serverName);
}
sendCommand(wlp, "stop", serverName); //$NON-NLS-1$
} catch (IOException | NotesException e) {
// Nothing to do here
}
}
}

for(Process p : subprocesses) {
if(p.isAlive()) {
try {
p.waitFor();
} catch (InterruptedException e) {
}
}
}

if(log.isLoggable(Level.INFO)) {
log.info("Shutdown");
}
@@ -334,20 +354,23 @@ private Process sendCommand(Path path, String command, Object... args) throws IO

Map<String, String> env = pb.environment();
env.put("JAVA_HOME", javaHome.toString()); //$NON-NLS-1$
String sysPath = System.getenv("PATH"); //$NON-NLS-1$

String execDirectory = OpenLibertyUtil.getDominoProgramDirectory();

if(!StringUtil.isEmpty(sysPath)) {
sysPath += File.pathSeparator + execDirectory;
}
String sysPath = System.getenv("PATH"); //$NON-NLS-1$
sysPath += File.pathSeparator + execDirectory;
env.put("PATH", sysPath); //$NON-NLS-1$

env.put("Domino_HTTP", getServerBase()); //$NON-NLS-1$

if(log.isLoggable(Level.FINE)) {
OpenLibertyLog.getLog().fine(format("Executing command {0}", pb.command()));
}
Process process = pb.start();
subprocesses.add(process);

DominoThreadFactory.executor.submit(new StreamRedirector(process.getInputStream()));
DominoThreadFactory.executor.submit(new StreamRedirector(process.getErrorStream()));
if(!terminating) {
DominoThreadFactory.executor.submit(new StreamRedirector(process.getInputStream()));
DominoThreadFactory.executor.submit(new StreamRedirector(process.getErrorStream()));
}

return process;
}

0 comments on commit f0c72b4

Please sign in to comment.
You can’t perform that action at this time.