Skip to content

Commit

Permalink
Implement build cancelation, fixes #127
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet authored and ppalaga committed Nov 10, 2020
1 parent 02ef4a2 commit c52dfa3
Show file tree
Hide file tree
Showing 12 changed files with 168 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.jboss.fuse.mvnd.common.DaemonException.StaleAddressException;
import org.jboss.fuse.mvnd.common.DaemonInfo;
import org.jboss.fuse.mvnd.common.Message;
import org.jboss.fuse.mvnd.common.Message.Prompt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -86,6 +85,14 @@ public void dispatch(Message message) throws DaemonException.ConnectException {
}
throw new DaemonException.ConnectException("Could not dispatch a message to the daemon.", e);
}
// in case we dispatch a cancelation request, also forward it to the main thread to exit asap
try {
if (message.getType() == Message.CANCEL_BUILD) {
queue.put(message);
}
} catch (InterruptedException e) {
throw new DaemonException.InterruptedException(e);
}
}

public List<Message> receive() throws ConnectException, StaleAddressException {
Expand Down Expand Up @@ -130,10 +137,6 @@ protected void doReceive() {
if (m == null) {
break;
}
if (m.getType() == Message.PROMPT) {
final Prompt prompt = (Prompt) m;
m = prompt.withCallback(response -> dispatch(prompt.response(response)));
}
queue.put(m);
}
} catch (Exception e) {
Expand Down
34 changes: 19 additions & 15 deletions client/src/main/java/org/jboss/fuse/mvnd/client/DefaultClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.List;
import org.fusesource.jansi.Ansi;
import org.jboss.fuse.mvnd.common.BuildProperties;
import org.jboss.fuse.mvnd.common.DaemonException;
import org.jboss.fuse.mvnd.common.DaemonInfo;
import org.jboss.fuse.mvnd.common.DaemonRegistry;
import org.jboss.fuse.mvnd.common.Environment;
Expand All @@ -32,13 +33,13 @@
import org.jboss.fuse.mvnd.common.OsUtils;
import org.jboss.fuse.mvnd.common.logging.ClientOutput;
import org.jboss.fuse.mvnd.common.logging.TerminalOutput;
import org.jline.utils.AttributedString;
import org.jline.utils.AttributedStyle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DefaultClient implements Client {

public static final int CANCEL_TIMEOUT = 10 * 1000;

private static final Logger LOGGER = LoggerFactory.getLogger(DefaultClient.class);

private final DaemonParameters parameters;
Expand All @@ -62,7 +63,13 @@ public static void main(String[] argv) throws Exception {
}

try (TerminalOutput output = new TerminalOutput(logFile)) {
new DefaultClient(new DaemonParameters()).execute(output, args);
try {
new DefaultClient(new DaemonParameters()).execute(output, args);
} catch (DaemonException.InterruptedException e) {
final AttributedStyle s = new AttributedStyle().bold().foreground(AttributedStyle.RED);
String str = new AttributedString(System.lineSeparator() + "Canceled by user", s).toAnsi();
output.accept(Message.display(str));
}
}
}

Expand Down Expand Up @@ -157,7 +164,7 @@ public ExecutionResult execute(ClientOutput output, List<String> argv) {
if (stop) {
DaemonInfo[] dis = registry.getAll().toArray(new DaemonInfo[0]);
if (dis.length > 0) {
output.accept(Message.log("Stopping " + dis.length + " running daemons"));
output.accept(Message.display("Stopping " + dis.length + " running daemons"));
for (DaemonInfo di : dis) {
try {
ProcessHandle.of(di.getPid()).ifPresent(ProcessHandle::destroyForcibly);
Expand Down Expand Up @@ -191,6 +198,7 @@ public ExecutionResult execute(ClientOutput output, List<String> argv) {

final DaemonConnector connector = new DaemonConnector(parameters, registry);
try (DaemonClientConnection daemon = connector.connect(output)) {
output.setDeamonDispatch(daemon::dispatch);
output.accept(Message.buildStatus("Connected to daemon"));

daemon.dispatch(new Message.BuildRequest(
Expand All @@ -203,24 +211,20 @@ public ExecutionResult execute(ClientOutput output, List<String> argv) {

while (true) {
final List<Message> messages = daemon.receive();
for (int i = 0; i < messages.size(); i++) {
Message m = messages.get(i);
output.accept(messages);
for (Message m : messages) {
switch (m.getType()) {
case Message.BUILD_EXCEPTION: {
output.accept(messages.subList(0, i + 1));
case Message.CANCEL_BUILD:
return new DefaultResult(argv,
new InterruptedException("The build was canceled"));
case Message.BUILD_EXCEPTION:
final BuildException e = (BuildException) m;
return new DefaultResult(argv,
new Exception(e.getClassName() + ": " + e.getMessage() + "\n" + e.getStackTrace()));
}
case Message.BUILD_STOPPED: {
output.accept(messages.subList(0, i));
case Message.BUILD_STOPPED:
return new DefaultResult(argv, null);
}
default:
break;
}
}
output.accept(messages);
}
}
}
Expand Down
26 changes: 11 additions & 15 deletions common/src/main/java/org/jboss/fuse/mvnd/common/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

public abstract class Message {
public static final int BUILD_REQUEST = 0;
Expand All @@ -44,10 +43,12 @@ public abstract class Message {
public static final int PROMPT_RESPONSE = 12;
public static final int BUILD_STATUS = 13;
public static final int KEYBOARD_INPUT = 14;
public static final int CANCEL_BUILD = 15;

public static final SimpleMessage KEEP_ALIVE_SINGLETON = new SimpleMessage(KEEP_ALIVE);
public static final SimpleMessage STOP_SINGLETON = new SimpleMessage(STOP);
public static final SimpleMessage BUILD_STOPPED_SINGLETON = new SimpleMessage(BUILD_STOPPED);
public static final SimpleMessage CANCEL_BUILD_SINGLETON = new SimpleMessage(CANCEL_BUILD);

final int type;

Expand Down Expand Up @@ -87,6 +88,8 @@ public static Message read(DataInputStream input) throws IOException {
return PromptResponse.read(input);
case BUILD_STATUS:
return StringMessage.read(BUILD_STATUS, input);
case CANCEL_BUILD:
return SimpleMessage.CANCEL_BUILD_SINGLETON;
}
throw new IllegalStateException("Unexpected message type: " + type);
}
Expand Down Expand Up @@ -454,6 +457,7 @@ public void write(DataOutputStream output) throws IOException {
output.writeInt(projectCount);
output.writeInt(maxThreads);
}

}

public static class BuildMessage extends Message {
Expand Down Expand Up @@ -511,6 +515,8 @@ public String toString() {
return "BuildStopped";
case STOP:
return "Stop";
case CANCEL_BUILD:
return "BuildCanceled";
default:
throw new IllegalStateException("Unexpected type " + type);
}
Expand Down Expand Up @@ -607,7 +613,6 @@ public static class Prompt extends Message {
final String uid;
final String message;
final boolean password;
final Consumer<String> callback;

public static Prompt read(DataInputStream input) throws IOException {
String projectId = Message.readUTF(input);
Expand All @@ -618,16 +623,11 @@ public static Prompt read(DataInputStream input) throws IOException {
}

public Prompt(String projectId, String uid, String message, boolean password) {
this(projectId, uid, message, password, null);
}

public Prompt(String projectId, String uid, String message, boolean password, Consumer<String> callback) {
super(PROMPT);
this.projectId = projectId;
this.uid = uid;
this.message = message;
this.password = password;
this.callback = callback;
}

public String getProjectId() {
Expand Down Expand Up @@ -665,18 +665,10 @@ public void write(DataOutputStream output) throws IOException {
output.writeBoolean(password);
}

public Prompt withCallback(Consumer<String> callback) {
return new Prompt(projectId, uid, message, password, callback);
}

public PromptResponse response(String message) {
return new PromptResponse(projectId, uid, message);
}

public Consumer<String> getCallback() {
return callback;
}

}

public static class PromptResponse extends Message {
Expand Down Expand Up @@ -737,6 +729,10 @@ public static StringMessage buildStatus(String payload) {
return new StringMessage(BUILD_STATUS, payload);
}

public static Display display(String message) {
return new Display(null, message);
}

public static BuildMessage log(String message) {
return new BuildMessage(null, message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@
package org.jboss.fuse.mvnd.common.logging;

import java.util.List;
import java.util.function.Consumer;
import org.jboss.fuse.mvnd.common.Message;

/**
* A sink for various kinds of events sent by the daemon.
*/
public interface ClientOutput extends AutoCloseable {

void setDeamonDispatch(Consumer<Message> sink);

void accept(Message message);

void accept(List<Message> messages);
Expand Down
Loading

0 comments on commit c52dfa3

Please sign in to comment.