Skip to content

Commit

Permalink
Launcher/CommandExecutor: improved the logic for Command return value.
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin Duda committed Mar 29, 2018
1 parent d5aea6c commit 399b2a9
Show file tree
Hide file tree
Showing 15 changed files with 104 additions and 56 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'maven'

project.version = "1.0.24-SNAPSHOT"
project.version = "1.0.26-SNAPSHOT"
project.group = 'com.github.codingchili.chili-core'

subprojects {
Expand Down
41 changes: 25 additions & 16 deletions core/main/java/com/codingchili/core/Launcher.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package com.codingchili.core;

import com.codingchili.core.configuration.CoreStrings;
import com.codingchili.core.context.*;
import com.codingchili.core.files.Configurations;
import com.codingchili.core.listener.CoreHandler;
import com.codingchili.core.listener.CoreListener;
import com.codingchili.core.listener.CoreService;
import com.codingchili.core.logging.ConsoleLogger;
import com.codingchili.core.logging.Level;
import io.vertx.core.Future;
import io.vertx.core.Verticle;

import java.util.ArrayList;
import java.util.List;

import com.codingchili.core.configuration.CoreStrings;
import com.codingchili.core.context.*;
import com.codingchili.core.files.Configurations;
import com.codingchili.core.listener.*;
import com.codingchili.core.logging.ConsoleLogger;
import com.codingchili.core.logging.Level;

import static com.codingchili.core.configuration.CoreStrings.*;
import static com.codingchili.core.files.Configurations.system;

Expand All @@ -33,23 +32,33 @@ public class Launcher implements CoreService {
* @param context the launcher context to use.
*/
public Launcher(LaunchContext context) {
Future<Boolean> future = Future.future();
Future<CommandResult> future = Future.future();

logger.log(CoreStrings.getStartupText(), Level.STARTUP);

context.getExecutor().execute(future, context.args());
future.setHandler(done -> {
CommandResult result = done.result();
try {
// the command was executed and returned false = no abort.
if (done.failed() || !done.result()) {
nodes = context.block(context.args());
nodes = new ArrayList<>(nodes);
clusterIfEnabled(context);
if (done.succeeded()) {
if (CommandResult.CONTINUE.equals(result)) {
nodes = context.block(context.args());
nodes = new ArrayList<>(nodes);
clusterIfEnabled(context);
}
if (CommandResult.SHUTDOWN.equals(result)) {
exit();
}
} else {
exit();
if (done.cause() != null) {
throw done.cause();
} else {
throw new CoreRuntimeException("Unknown cause: ");
}
}
// else: the future succeeded with "true" - no action.
} catch (Throwable e) {
logger.log(e.getMessage(), Level.ERROR);
logger.log(throwableToString(e), Level.ERROR);
logger.log(getCommandError(context.getCommand()), Level.INFO);
exit();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.codingchili.core.benchmarking;

import com.codingchili.core.context.CommandExecutor;
import com.codingchili.core.context.CoreContext;
import com.codingchili.core.context.SystemContext;
import com.codingchili.core.context.*;
import com.codingchili.core.storage.*;
import io.vertx.core.Future;

Expand All @@ -27,7 +25,7 @@ public class BenchmarkSuite {
* @param future callback on completion
* @param executor executor to invoke this as a command.
*/
public Void execute(Future<Boolean> future, CommandExecutor executor) {
public Void execute(Future<CommandResult> future, CommandExecutor executor) {
executor.getProperty(PARAM_ITERATIONS).ifPresent(iterations ->
this.iterations = Integer.parseInt(iterations));

Expand All @@ -44,7 +42,7 @@ public Void execute(Future<Boolean> future, CommandExecutor executor) {
return null;
}

private void createReport(Future<Boolean> future, List<BenchmarkGroup> result, CommandExecutor executor) {
private void createReport(Future<CommandResult> future, List<BenchmarkGroup> result, CommandExecutor executor) {
Optional<String> template = executor.getProperty(PARAM_TEMPLATE);
BenchmarkReport report;

Expand All @@ -55,7 +53,7 @@ private void createReport(Future<Boolean> future, List<BenchmarkGroup> result, C
}
template.ifPresent(report::template);
report.display();
future.complete(true);
future.complete(CommandResult.SHUTDOWN);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public abstract class CoreStrings {
public static final String PARAM_ITERATIONS = getCommand("iterations");
public static final String PARAM_HTML = getCommand("html");
public static final String PARAM_TEMPLATE = getCommand("template");
public static final String DEPLOY = getCommand("deploy");

// keys used in json objects.
public static final String ID_TOKEN = "token";
Expand Down Expand Up @@ -467,6 +468,10 @@ public static String getReconfigureDescription() {
return "resets system configuration files.";
}

public static String getDeployDescription() {
return "deploys the services in the given block. example: --deploy blockName";
}

public static String getDescriptionMissing() {
return "No description provided.";
}
Expand Down
9 changes: 4 additions & 5 deletions core/main/java/com/codingchili/core/context/BaseCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import io.vertx.core.Future;

import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;

/**
Expand All @@ -12,7 +11,7 @@
* A basic
*/
public class BaseCommand implements Command {
private BiFunction<Future<Boolean>, CommandExecutor, Void> command;
private BiFunction<Future<CommandResult>, CommandExecutor, Void> command;
private boolean visible = true;
private String name;
private String description;
Expand All @@ -24,7 +23,7 @@ public class BaseCommand implements Command {
* @param name the handler of the command
* @param description the command description
*/
public BaseCommand(BiFunction<Future<Boolean>, CommandExecutor, Void> consumer, String name, String description) {
public BaseCommand(BiFunction<Future<CommandResult>, CommandExecutor, Void> consumer, String name, String description) {
this.command = consumer;
this.name = name;
this.description = description;
Expand All @@ -37,7 +36,7 @@ public BaseCommand(BiFunction<Future<Boolean>, CommandExecutor, Void> consumer,
* @param name the handler of the command
* @param description the command description
*/
public BaseCommand(Function<CommandExecutor, Boolean> runnable, String name, String description) {
public BaseCommand(Function<CommandExecutor, CommandResult> runnable, String name, String description) {
this((future, executor) -> {
future.complete(runnable.apply(executor));
return null;
Expand All @@ -55,7 +54,7 @@ public boolean isVisible() {
}

@Override
public void execute(Future<Boolean> future, CommandExecutor executor) {
public void execute(Future<CommandResult> future, CommandExecutor executor) {
command.apply(future, executor);
}

Expand Down
2 changes: 1 addition & 1 deletion core/main/java/com/codingchili/core/context/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ default boolean isVisible() {
* @param future callback: complete with true to abort startup.
* @param executor the executor executing the command, can be used to get properties.
*/
void execute(Future<Boolean> future, CommandExecutor executor);
void execute(Future<CommandResult> future, CommandExecutor executor);

/**
* @return the command description.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ public interface CommandExecutor {
* @param commandLine the commands/properties to execute.
* @return fluent
*/
CommandExecutor execute(Future<Boolean> future, String... commandLine);
CommandExecutor execute(Future<CommandResult> future, String... commandLine);

/**
* Executes the given command synchronously.
*
* @param command the command to execute
* @return true if startup is to be aborted.
*/
Boolean execute(String... command);
CommandResult execute(String... command);

/**
* Get the first command passed to the executor.
Expand Down Expand Up @@ -77,7 +77,7 @@ public interface CommandExecutor {
* @param description the description of the command
* @return fluent
*/
CommandExecutor add(BiFunction<Future<Boolean>, CommandExecutor, Void> executor, String name, String
CommandExecutor add(BiFunction<Future<CommandResult>, CommandExecutor, Void> executor, String name, String
description);

/**
Expand All @@ -88,7 +88,7 @@ CommandExecutor add(BiFunction<Future<Boolean>, CommandExecutor, Void> executor,
* @param description the description of the command
* @return fluent
*/
CommandExecutor add(Function<CommandExecutor, Boolean> executor, String name, String description);
CommandExecutor add(Function<CommandExecutor, CommandResult> executor, String name, String description);

/**
* Lists all commands added to the executor.
Expand Down
32 changes: 32 additions & 0 deletions core/main/java/com/codingchili/core/context/CommandResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.codingchili.core.context;

/**
* @author Robin Duda
*
* Results returned from an #{@link Command} when it is
* executed by a #{@link CommandExecutor}.
*
* If the command fails and wants to display the error message
* (stacktrace) that generated the failure, then the future
* passed from the #{@link CommandExecutor} should be failed,
* with the exception.
*/
public enum CommandResult {
/**
* Indicates that the command has executed successfully
* and that the system should shut down.
*/
SHUTDOWN,
/**
* Indicates that the command has executed successfully and
* that the #{@link com.codingchili.core.Launcher} should continue
* processing any deployment blocks.
*/
CONTINUE,
/**
* Indicates that the command has executed successfully and that the
* command is to handle deployment. The #{@link com.codingchili.core.Launcher}
* should not process any deployment blocks.
*/
STARTED
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.codingchili.core.context.exception.CommandAlreadyExistsException;
import com.codingchili.core.context.exception.NoSuchCommandException;
import com.codingchili.core.files.Configurations;
import com.codingchili.core.logging.ConsoleLogger;
import com.codingchili.core.logging.Logger;
import io.vertx.core.Future;

Expand Down Expand Up @@ -44,12 +43,12 @@ public DefaultCommandExecutor(Logger logger) {
}

@Override
public CommandExecutor execute(Future<Boolean> future, String... commandLine) {
public CommandExecutor execute(Future<CommandResult> future, String... commandLine) {
parseCommandLine(commandLine);
Optional<String> command = getCommand();

if (command.isPresent() && commands.containsKey(command.get())) {
Future<Boolean> execution = Future.future();
Future<CommandResult> execution = Future.future();
commands.get(command.get()).execute(execution, this);
execution.setHandler(future);
} else {
Expand All @@ -59,8 +58,8 @@ public CommandExecutor execute(Future<Boolean> future, String... commandLine) {
}

@Override
public Boolean execute(String... command) {
Future<Boolean> future = Future.future();
public CommandResult execute(String... command) {
Future<CommandResult> future = Future.future();
execute(future, command);

if (future.failed()) {
Expand Down Expand Up @@ -134,13 +133,13 @@ public CommandExecutor add(Command command) {
}

@Override
public CommandExecutor add(BiFunction<Future<Boolean>, CommandExecutor, Void> executor, String name, String
public CommandExecutor add(BiFunction<Future<CommandResult>, CommandExecutor, Void> executor, String name, String
description) {
return add(new BaseCommand(executor, name, description));
}

@Override
public CommandExecutor add(Function<CommandExecutor, Boolean> executor, String name, String description) {
public CommandExecutor add(Function<CommandExecutor, CommandResult> executor, String name, String description) {
return add(new BaseCommand(executor, name, description));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public String[] args() {
}

public List<String> block(String[] args) throws CoreException {
return block((args.length == 0) ? findBlockByEnvironment() : args[0]);
return block((args.length < 2) ? findBlockByEnvironment() : args[1]);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ private void registerCommands() {
AuthenticationGenerator generator = new AuthenticationGenerator(logger);
BenchmarkSuite suite = new BenchmarkSuite();

super.add((executor) -> CommandResult.CONTINUE, DEPLOY, getDeployDescription());
add(Configurations::reset, RECONFIGURE, getReconfigureDescription());
add(generator::preshare, GENERATE_PRESHARED, getGeneratePresharedDescription());
add(generator::secrets, GENERATE_SECRETS, getGenerateSecretsDescription());
Expand All @@ -51,7 +52,7 @@ private void registerCommands() {
private void add(Runnable runnable, String name, String description) {
super.add((executor) -> {
runnable.run();
return true; // abort startup.
return CommandResult.SHUTDOWN;
}, name, description);
}

Expand Down
3 changes: 2 additions & 1 deletion core/test/java/com/codingchili/core/LauncherIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.function.Consumer;
import java.util.function.Supplier;

import static com.codingchili.core.configuration.CoreStrings.DEPLOY;
import static com.codingchili.core.files.Configurations.system;

/**
Expand Down Expand Up @@ -125,7 +126,7 @@ void exit() {
}

public LaunchContext getLaunchContextFor(String node) {
return new LaunchContext() {
return new LaunchContext(DEPLOY) {
@Override
protected List<String> block(String block) {
List<String> list = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.codingchili.core.benchmarking;

import com.codingchili.core.context.CommandExecutor;
import com.codingchili.core.context.CoreContext;
import com.codingchili.core.context.DefaultCommandExecutor;
import com.codingchili.core.context.SystemContext;
import com.codingchili.core.context.*;

import io.vertx.core.Future;
import io.vertx.ext.unit.Async;
import io.vertx.ext.unit.TestContext;
Expand Down Expand Up @@ -76,7 +74,7 @@ public void testRunBenchmarkSuites(TestContext test) {
public void testExecuteSuiteAsCommand(TestContext test) {
CommandExecutor executor = new DefaultCommandExecutor();
Async async = test.async();
Future<Boolean> future = Future.<Boolean>future().setHandler(done -> {
Future<CommandResult> future = Future.<CommandResult>future().setHandler(done -> {
test.assertTrue(done.succeeded());
async.complete();
});
Expand Down
Loading

0 comments on commit 399b2a9

Please sign in to comment.