Skip to content
This repository has been archived by the owner on Aug 13, 2020. It is now read-only.

Commit

Permalink
Return the status of a system command using the JMX command client
Browse files Browse the repository at this point in the history
  • Loading branch information
amckenzie committed Oct 9, 2019
1 parent fa878f1 commit 47d37bd
Show file tree
Hide file tree
Showing 43 changed files with 740 additions and 330 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ on [Keep a CHANGELOG](http://keepachangelog.com/). This project adheres to

## [Unreleased]

## [6.2.0-M3] - 2018-10-09
### Added
- Refactored Shuttering to no longer use events.
- All Shuttering Executors now implement the ShutteringExecutor interface
- JMX bean can now be pinged to get command status:
- COMMAND_RECEIVED
- COMMAND_IN_PROGRESS
- COMMAND_COMPLETE
- COMMAND_FAILED

## [6.2.0-M2] - 2018-10-02
### Added
- Added commandId to all SystemEvents
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ default boolean shouldUnshutter() {
return false;
}

default ShutteringResult shutter(final UUID commandId, final SystemCommand systemCommand) {
default ShutteringResult shutter(final UUID commandId, final SystemCommand systemCommand) throws ShutteringFailedException {
throw new UnsupportedOperationException("Method not implemented");
}

default ShutteringResult unshutter(final UUID commandId, final SystemCommand systemCommand) {
default ShutteringResult unshutter(final UUID commandId, final SystemCommand systemCommand) throws ShutteringFailedException {
throw new UnsupportedOperationException("Method not implemented");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package uk.gov.justice.services.management.shuttering.api;

public class ShutteringFailedException extends Exception {

public ShutteringFailedException(final String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import static java.util.Optional.empty;
import static java.util.Optional.of;
import static uk.gov.justice.services.jmx.state.domain.CommandState.COMPLETE;
import static uk.gov.justice.services.jmx.state.domain.CommandState.FAILED;
import static uk.gov.justice.services.jmx.api.domain.CommandState.COMMAND_COMPLETE;
import static uk.gov.justice.services.jmx.api.domain.CommandState.COMMAND_FAILED;

import uk.gov.justice.services.jmx.api.command.SystemCommand;
import uk.gov.justice.services.jmx.state.domain.CommandState;
import uk.gov.justice.services.jmx.api.domain.CommandState;

import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -45,7 +45,7 @@ public static ShutteringResult shutteringSucceeded(
return new ShutteringResult(
shutteringExecutorName,
message,
COMPLETE,
COMMAND_COMPLETE,
commandId,
systemCommand,
empty()
Expand All @@ -61,7 +61,7 @@ public static ShutteringResult shutteringFailed(
return new ShutteringResult(
shutteringExecutorName,
message,
FAILED,
COMMAND_FAILED,
commandId,
systemCommand,
empty()
Expand All @@ -78,7 +78,7 @@ public static ShutteringResult shutteringFailed(
return new ShutteringResult(
shutteringExecutorName,
message,
FAILED,
COMMAND_FAILED,
commandId,
systemCommand,
of(exception)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package uk.gov.justice.services.management.shuttering.executors;

import static uk.gov.justice.services.management.shuttering.api.ShutteringResult.shutteringFailed;
import static uk.gov.justice.services.management.shuttering.api.ShutteringResult.shutteringSucceeded;

import uk.gov.justice.services.jmx.api.command.SystemCommand;
Expand Down Expand Up @@ -34,61 +33,34 @@ public boolean shouldUnshutter() {
@Override
public ShutteringResult shutter(final UUID commandId, final SystemCommand systemCommand) {

final String name = getClass().getSimpleName();

try {
logger.info("Shuttering Command API");

commandApiShutteringBean.shutter();

logger.info("Shuttering of Command API complete");

return shutteringSucceeded(
name,
commandId,
"Command API shuttered with no errors",
systemCommand
);
} catch (final Exception e) {
logger.error("Shuttering of command API failed", e);
return shutteringFailed(
name,
commandId,
"Shuttering of Command API failed: " + e.getMessage(),
systemCommand,
e
);
}
logger.info("Shuttering Command API");

commandApiShutteringBean.shutter();

logger.info("Shuttering of Command API complete");

return shutteringSucceeded(
getName(),
commandId,
"Command API shuttered with no errors",
systemCommand
);
}

@Override
public ShutteringResult unshutter(final UUID commandId, final SystemCommand systemCommand) {

final String name = getClass().getSimpleName();

try {

logger.info("Unshuttering Command API");
logger.info("Unshuttering Command API");

commandApiShutteringBean.unshutter();
commandApiShutteringBean.unshutter();

logger.info("Unshuttering of Command API complete");
logger.info("Unshuttering of Command API complete");

return shutteringSucceeded(
name,
commandId,
"Command API unshuttered with no errors",
systemCommand
);
} catch (final Exception e) {
logger.error("Unshuttering of command API failed", e);
return shutteringFailed(
name,
commandId,
"Unshuttering of Command API failed: " + e.getMessage(),
systemCommand,
e
);
}
return shutteringSucceeded(
getName(),
commandId,
"Command API unshuttered with no errors",
systemCommand
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static uk.gov.justice.services.jmx.api.command.UnshutterCommand.UNSHUTTER;

import uk.gov.justice.services.jmx.api.command.ShutterCommand;
import uk.gov.justice.services.jmx.api.command.SystemCommand;
import uk.gov.justice.services.jmx.api.command.UnshutterCommand;
import uk.gov.justice.services.jmx.command.HandlesSystemCommand;

Expand All @@ -24,15 +25,16 @@ public class ShutteringSystemCommandHandler {

@HandlesSystemCommand(SHUTTER)
public void onShutterRequested(final ShutterCommand shutterCommand, final UUID commandId) {

logger.info(format("Received %s system command", shutterCommand.getName()));
runShutteringBean.runShuttering(commandId, shutterCommand);
doRun(shutterCommand, commandId);
}

@HandlesSystemCommand(UNSHUTTER)
public void onUnshutterRequested(final UnshutterCommand unshutterCommand, final UUID commandId) {
doRun(unshutterCommand, commandId);
}

logger.info(format("Received %s system command", unshutterCommand.getName()));
runShutteringBean.runShuttering(commandId, unshutterCommand);
private void doRun(final SystemCommand systemCommand, final UUID commandId) {
logger.info(format("Received %s system command", systemCommand.getName()));
runShutteringBean.runShuttering(commandId, systemCommand);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public class ShutterRunner {
@Inject
private ShutteringExecutorProvider shutteringExecutorProvider;

@Inject
private ShutteringFailedHandler shutteringFailedHandler;

@Inject
private Logger logger;

Expand All @@ -33,6 +36,10 @@ private ShutteringResult shutter(final UUID commandId, final SystemCommand syste

logger.info(format("Shuttering %s", shutteringExecutor.getName()));

return shutteringExecutor.shutter(commandId, systemCommand);
try {
return shutteringExecutor.shutter(commandId, systemCommand);
} catch (final Throwable e) {
return shutteringFailedHandler.onShutteringFailed(commandId, systemCommand, shutteringExecutor, e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
package uk.gov.justice.services.management.shuttering.process;

import static java.lang.String.format;
import static java.util.stream.Collectors.toList;
import static uk.gov.justice.services.jmx.api.command.ShutterCommand.SHUTTER;
import static uk.gov.justice.services.jmx.api.command.UnshutterCommand.UNSHUTTER;

import uk.gov.justice.services.jmx.api.SystemCommandException;
import uk.gov.justice.services.jmx.api.command.SystemCommand;
import uk.gov.justice.services.management.shuttering.api.ShutteringExecutor;
import uk.gov.justice.services.management.shuttering.api.ShutteringResult;

import java.util.List;
import java.util.UUID;

import javax.inject.Inject;

import org.slf4j.Logger;

public class ShutteringExecutorsRunner {

@Inject
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package uk.gov.justice.services.management.shuttering.process;

import static java.lang.String.format;
import static uk.gov.justice.services.management.shuttering.api.ShutteringResult.shutteringFailed;

import uk.gov.justice.services.jmx.api.command.SystemCommand;
import uk.gov.justice.services.management.shuttering.api.ShutteringExecutor;
import uk.gov.justice.services.management.shuttering.api.ShutteringResult;

import java.util.UUID;

import javax.inject.Inject;

import org.slf4j.Logger;

public class ShutteringFailedHandler {

@Inject
private Logger logger;

public ShutteringResult onShutteringFailed(
final UUID commandId,
final SystemCommand systemCommand,
final ShutteringExecutor shutteringExecutor,
final Throwable exception) {

final String message = format(
"%s failed for %s. %s: %s",
systemCommand.getName(),
shutteringExecutor.getName(),
exception.getClass().getName(),
exception.getMessage());

logger.error(message, exception);

return shutteringFailed(
shutteringExecutor.getName(),
commandId,
message,
systemCommand,
exception
);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package uk.gov.justice.services.management.shuttering.process;

import static java.lang.String.format;
import static uk.gov.justice.services.jmx.state.domain.CommandState.COMPLETE;
import static uk.gov.justice.services.jmx.state.domain.CommandState.FAILED;
import static uk.gov.justice.services.jmx.api.domain.CommandState.COMMAND_COMPLETE;
import static uk.gov.justice.services.jmx.api.domain.CommandState.COMMAND_FAILED;

import uk.gov.justice.services.common.util.UtcClock;
import uk.gov.justice.services.jmx.api.command.SystemCommand;
Expand Down Expand Up @@ -45,7 +45,7 @@ public void completeShutteringSuccessfully(final List<ShutteringResult> successf
systemCommandStateChangedEventFirer.fire(new SystemCommandStateChangedEvent(
commandId,
systemCommand,
COMPLETE,
COMMAND_COMPLETE,
clock.now(),
message
));
Expand All @@ -70,7 +70,7 @@ public void completeShutteringWithFailures(final List<ShutteringResult> failureR
systemCommandStateChangedEventFirer.fire(new SystemCommandStateChangedEvent(
commandId,
systemCommand,
FAILED,
COMMAND_FAILED,
clock.now(),
message
));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package uk.gov.justice.services.management.shuttering.process;

import static java.lang.String.format;
import static uk.gov.justice.services.jmx.state.domain.CommandState.IN_PROGRESS;
import static uk.gov.justice.services.jmx.api.domain.CommandState.COMMAND_IN_PROGRESS;

import uk.gov.justice.services.common.util.UtcClock;
import uk.gov.justice.services.jmx.api.command.SystemCommand;
Expand Down Expand Up @@ -45,7 +45,7 @@ public void runShuttering(final UUID commandId, final SystemCommand systemComman
systemCommandStateChangedEventFirer.fire(new SystemCommandStateChangedEvent(
commandId,
systemCommand,
IN_PROGRESS,
COMMAND_IN_PROGRESS,
clock.now(),
format("%s started", systemCommandName)
));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package uk.gov.justice.services.management.shuttering.process;

import static java.util.stream.Collectors.toList;
import static uk.gov.justice.services.jmx.state.domain.CommandState.COMPLETE;
import static uk.gov.justice.services.jmx.state.domain.CommandState.FAILED;
import static uk.gov.justice.services.jmx.api.domain.CommandState.COMMAND_COMPLETE;
import static uk.gov.justice.services.jmx.api.domain.CommandState.COMMAND_FAILED;

import uk.gov.justice.services.management.shuttering.api.ShutteringResult;

Expand All @@ -13,14 +13,14 @@ public class ShutteringResultsMapper {
public List<ShutteringResult> getSuccessfulResults(final List<ShutteringResult> shutteringResults) {

return shutteringResults.stream()
.filter(shutteringResult -> shutteringResult.getCommandState() == COMPLETE)
.filter(shutteringResult -> shutteringResult.getCommandState() == COMMAND_COMPLETE)
.collect(toList());
}

public List<ShutteringResult> getFailedResults(final List<ShutteringResult> shutteringResults) {

return shutteringResults.stream()
.filter(shutteringResult -> shutteringResult.getCommandState() == FAILED)
.filter(shutteringResult -> shutteringResult.getCommandState() == COMMAND_FAILED)
.collect(toList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,27 @@ public class UnshutterRunner {
@Inject
private ShutteringExecutorProvider shutteringExecutorProvider;

@Inject
private ShutteringFailedHandler shutteringFailedHandler;

@Inject
private Logger logger;

public List<ShutteringResult> runUnshuttering(final UUID commandId, final SystemCommand systemCommand) {
return shutteringExecutorProvider.getShutteringExecutors().stream()
.filter(ShutteringExecutor::shouldUnshutter)
.map(shutteringExecutor -> uhshutter(commandId, systemCommand, shutteringExecutor))
.map(shutteringExecutor -> unshutter(commandId, systemCommand, shutteringExecutor))
.collect(toList());
}

private ShutteringResult uhshutter(final UUID commandId, final SystemCommand systemCommand, final ShutteringExecutor shutteringExecutor) {
private ShutteringResult unshutter(final UUID commandId, final SystemCommand systemCommand, final ShutteringExecutor shutteringExecutor) {

logger.info(format("Unshuttering %s", shutteringExecutor.getName()));

return shutteringExecutor.unshutter(commandId, systemCommand);
try {
return shutteringExecutor.unshutter(commandId, systemCommand);
} catch (final Throwable e) {
return shutteringFailedHandler.onShutteringFailed(commandId, systemCommand, shutteringExecutor, e);
}
}
}
Loading

0 comments on commit 47d37bd

Please sign in to comment.