Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AD20-16: Support to cancel docker execution #371

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.github.dockerjava.api.async.ResultCallback;
import com.github.dockerjava.api.command.LogContainerCmd;
import com.github.dockerjava.api.command.WaitContainerResultCallback;
import com.github.dockerjava.api.exception.DockerException;
import com.github.dockerjava.api.exception.NotFoundException;
import com.github.dockerjava.api.model.Frame;
import com.odysseusinc.arachne.execution_engine_common.api.v1.dto.AnalysisRequestTypeDTO;
Expand Down Expand Up @@ -66,9 +67,40 @@ public DockerOverseer(

@Override
public CompletableFuture<ExecutionOutcome> abort() {
return result.isDone() ? result : CompletableFuture.completedFuture(
new ExecutionOutcome(Stage.ABORT, "Abort is not yet supported for docker analysis", null)
);
if (!init.isDone()) {
init.cancel(true);
}

return init.handle((containerId, throwable) -> {
if (containerId == null) {
if (throwable != null) {
log.error("Error during initialization: {}", throwable.getMessage(), throwable);
stdout.append("\r\n\"Initialization failed: ").append(throwable.getMessage());
return new ExecutionOutcome(Stage.INITIALIZE, "Initialization failed: " + throwable.getMessage(), stdout.toString());
} else {
stdout.append("\r\nContainer initialization was canceled");
return new ExecutionOutcome(Stage.ABORTED, null, stdout.toString());
}
} else {
if (result.isDone()) {
return result.join();
} else {
try {
client.stopContainerCmd(containerId).exec();
stdout.append("\r\nDocker container aborted successfully");
return new ExecutionOutcome(Stage.ABORTED, null, stdout.toString());
} catch (NotFoundException e) {
log.info("Container not found or already stopped: " + e.getMessage());
return result.join();
} catch (DockerException e) {
log.error("Error stopping the Docker container: {}", e.getMessage());
stdout.append("\r\n\"Error aborting Docker container:").append(e.getMessage());
return new ExecutionOutcome(Stage.ABORT, "Error aborting Docker container: " + e.getMessage(), stdout.toString());
}
}
}
});

}

private static ResultCallback.Adapter<Frame> logAdapter(long id, StringBuffer stdout) {
Expand Down