Skip to content

Commit

Permalink
feat (cli): Introduce --server (still un-tested & un-documented)
Browse files Browse the repository at this point in the history
  • Loading branch information
vorburger committed Sep 30, 2023
1 parent 884ab9a commit a9a3774
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 15 deletions.
41 changes: 31 additions & 10 deletions cli/src/main/java/dev/enola/cli/CommandWithModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@

import dev.enola.common.io.resource.ResourceProviders;
import dev.enola.core.EnolaServiceProvider;
import dev.enola.core.grpc.EnolaGrpcClientProvider;
import dev.enola.core.grpc.EnolaGrpcInProcess;
import dev.enola.core.meta.EntityKindRepository;
import dev.enola.core.proto.EnolaServiceGrpc.EnolaServiceBlockingStub;

import picocli.CommandLine.ArgGroup;
import picocli.CommandLine.Model.CommandSpec;
import picocli.CommandLine.Option;
import picocli.CommandLine.Spec;
Expand All @@ -36,24 +38,28 @@ public abstract class CommandWithModel implements CheckedRunnable {
protected EnolaServiceProvider esp;
@Spec CommandSpec spec;

@Option(
names = {"--model"},
required = true,
description = "URI to EntityKinds (e.g. file:model.yaml)")
private URI model;
@ArgGroup(multiplicity = "1")
ModelOrServer group;

private EnolaServiceBlockingStub service;

@Override
public final void run() throws Exception {
out = spec.commandLine().getOut();

// TODO Fix design; as-is, this may stay null if --server instead of --model is used
EntityKindRepository ekr = null;

// TODO Move elsewhere for continuous ("shell") mode, as this is "expensive".
var modelResource = new ResourceProviders().getReadableResource(model);
var ekr = new EntityKindRepository();
ekr.load(modelResource);
esp = new EnolaServiceProvider();
service = new EnolaGrpcInProcess(esp.get(ekr)).get();
if (group.model != null) {
var modelResource = new ResourceProviders().getReadableResource(group.model);
ekr = new EntityKindRepository();
ekr.load(modelResource);
esp = new EnolaServiceProvider();
service = new EnolaGrpcInProcess(esp.get(ekr)).get();
} else if (group.server != null) {
service = new EnolaGrpcClientProvider(group.server).get();
}

run(ekr, service);
}
Expand All @@ -62,4 +68,19 @@ public final void run() throws Exception {
// here
protected abstract void run(EntityKindRepository ekr, EnolaServiceBlockingStub service)
throws Exception;

static class ModelOrServer {

@Option(
names = {"--model"},
required = true,
description = "URI to EntityKinds (e.g. file:model.yaml)")
private URI model;

@Option(
names = {"--server"},
required = true,
description = "Target of an Enola gRPC Server (e.g. localhost:7070)")
private String server;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
*/
package dev.enola.core.grpc;

import static java.util.concurrent.TimeUnit.SECONDS;

import dev.enola.core.EnolaService;
import dev.enola.core.proto.EnolaServiceGrpc;

Expand Down Expand Up @@ -49,7 +47,8 @@ public EnolaGrpcInProcess(EnolaService service) throws IOException {
channelBuilder.directExecutor(); // as above
channel = channelBuilder.build();

client = EnolaServiceGrpc.newBlockingStub(channel).withDeadlineAfter(3, SECONDS);
// .withDeadlineAfter(13, SECONDS) doesn't seem to work will with InProcessChannelBuilder?!
client = EnolaServiceGrpc.newBlockingStub(channel);
}

public EnolaServiceGrpc.EnolaServiceBlockingStub get() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.grpc.ServerBuilder;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

public class EnolaGrpcServer implements AutoCloseable {

Expand All @@ -46,7 +47,7 @@ public int getPort() {
}

@Override
public void close() throws Exception {
server.shutdown().awaitTermination();
public void close() throws InterruptedException {
server.shutdown().awaitTermination(7, TimeUnit.SECONDS);
}
}

0 comments on commit a9a3774

Please sign in to comment.