-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
236 additions
and
293 deletions.
There are no files selected for viewing
21 changes: 7 additions & 14 deletions
21
...uk/gov/justice/services/eventstore/management/rebuild/commands/RebuildCommandHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,30 @@ | ||
package uk.gov.justice.services.eventstore.management.rebuild.commands; | ||
|
||
import static java.lang.String.format; | ||
import static uk.gov.justice.services.jmx.api.command.RebuildCommand.REBUILD; | ||
|
||
import uk.gov.justice.services.common.util.UtcClock; | ||
import uk.gov.justice.services.eventstore.management.events.rebuild.RebuildRequestedEvent; | ||
import uk.gov.justice.services.eventstore.management.rebuild.process.RebuildProcessRunner; | ||
import uk.gov.justice.services.jmx.api.command.RebuildCommand; | ||
import uk.gov.justice.services.jmx.command.HandlesSystemCommand; | ||
import uk.gov.justice.services.jmx.logging.MdcLogger; | ||
|
||
import java.time.ZonedDateTime; | ||
import java.util.UUID; | ||
|
||
import javax.enterprise.event.Event; | ||
import javax.inject.Inject; | ||
|
||
import org.slf4j.Logger; | ||
|
||
public class RebuildCommandHandler { | ||
|
||
@Inject | ||
private Event<RebuildRequestedEvent> rebuildRequestedEventEventFirer; | ||
private RebuildProcessRunner rebuildProcessRunner; | ||
|
||
@Inject | ||
private UtcClock clock; | ||
private MdcLogger mdcLogger; | ||
|
||
@Inject | ||
private Logger logger; | ||
|
||
@HandlesSystemCommand(REBUILD) | ||
public void doRebuild(final RebuildCommand rebuildCommand, final UUID commandId) { | ||
|
||
final ZonedDateTime now = clock.now(); | ||
logger.info(format("Received command '%s' at %tr", rebuildCommand, now)); | ||
rebuildRequestedEventEventFirer.fire(new RebuildRequestedEvent(commandId, now, rebuildCommand)); | ||
mdcLogger.mdcLoggerConsumer().accept(() -> { | ||
rebuildProcessRunner.runRebuild(commandId, rebuildCommand); | ||
}); | ||
} | ||
} |
62 changes: 0 additions & 62 deletions
62
...java/uk/gov/justice/services/eventstore/management/rebuild/observers/RebuildObserver.java
This file was deleted.
Oops, something went wrong.
82 changes: 82 additions & 0 deletions
82
...a/uk/gov/justice/services/eventstore/management/rebuild/process/RebuildProcessRunner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package uk.gov.justice.services.eventstore.management.rebuild.process; | ||
|
||
import static java.lang.String.format; | ||
import static java.time.temporal.ChronoUnit.MILLIS; | ||
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 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.eventsourcing.publishedevent.rebuild.PublishedEventRebuilder; | ||
import uk.gov.justice.services.jmx.api.command.RebuildCommand; | ||
import uk.gov.justice.services.jmx.state.events.SystemCommandStateChangedEvent; | ||
|
||
import java.time.ZonedDateTime; | ||
import java.util.UUID; | ||
|
||
import javax.enterprise.event.Event; | ||
import javax.inject.Inject; | ||
|
||
import org.slf4j.Logger; | ||
|
||
public class RebuildProcessRunner { | ||
|
||
@Inject | ||
private PublishedEventRebuilder publishedEventRebuilder; | ||
|
||
@Inject | ||
private Event<SystemCommandStateChangedEvent> systemCommandStateChangedEventFirer; | ||
|
||
@Inject | ||
private UtcClock clock; | ||
|
||
@Inject | ||
private Logger logger; | ||
|
||
public void runRebuild(final UUID commandId, final RebuildCommand rebuildCommand) { | ||
|
||
final String commandName = rebuildCommand.getName(); | ||
final ZonedDateTime rebuildStartedAt = clock.now(); | ||
final String startMessage = format("%s started at %tc", commandName, rebuildStartedAt); | ||
|
||
systemCommandStateChangedEventFirer.fire(new SystemCommandStateChangedEvent( | ||
commandId, | ||
rebuildCommand, | ||
COMMAND_IN_PROGRESS, | ||
rebuildStartedAt, | ||
startMessage | ||
)); | ||
|
||
logger.info(startMessage); | ||
|
||
try { | ||
publishedEventRebuilder.rebuild(); | ||
|
||
final ZonedDateTime rebuildCompletedAt = clock.now(); | ||
logger.info(format("%s command completed at %tc", commandName, rebuildCompletedAt)); | ||
final String endMessage = format("Rebuild took %d milliseconds", MILLIS.between(rebuildStartedAt, rebuildCompletedAt)); | ||
|
||
systemCommandStateChangedEventFirer.fire(new SystemCommandStateChangedEvent( | ||
commandId, | ||
rebuildCommand, | ||
COMMAND_COMPLETE, | ||
rebuildCompletedAt, | ||
endMessage | ||
)); | ||
|
||
logger.info(endMessage); | ||
|
||
} catch (final Exception e) { | ||
|
||
logger.error(format("%s failed", commandName), e); | ||
|
||
systemCommandStateChangedEventFirer.fire(new SystemCommandStateChangedEvent( | ||
commandId, | ||
rebuildCommand, | ||
COMMAND_FAILED, | ||
clock.now(), | ||
format("Rebuild failed: %s: %s", e.getClass().getSimpleName(), e.getMessage()) | ||
)); | ||
} | ||
} | ||
} |
26 changes: 9 additions & 17 deletions
26
...ov/justice/services/eventstore/management/rebuild/commands/RebuildCommandHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,46 @@ | ||
package uk.gov.justice.services.eventstore.management.rebuild.commands; | ||
|
||
import static java.time.ZoneOffset.UTC; | ||
import static java.time.ZonedDateTime.of; | ||
import static java.util.UUID.randomUUID; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import uk.gov.justice.services.common.util.UtcClock; | ||
import uk.gov.justice.services.eventstore.management.events.rebuild.RebuildRequestedEvent; | ||
import uk.gov.justice.services.eventstore.management.rebuild.process.RebuildProcessRunner; | ||
import uk.gov.justice.services.jmx.api.command.RebuildCommand; | ||
import uk.gov.justice.services.jmx.logging.MdcLogger; | ||
|
||
import java.time.ZonedDateTime; | ||
import java.util.UUID; | ||
|
||
import javax.enterprise.event.Event; | ||
import java.util.function.Consumer; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.runners.MockitoJUnitRunner; | ||
import org.slf4j.Logger; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class RebuildCommandHandlerTest { | ||
|
||
@Mock | ||
private Event<RebuildRequestedEvent> rebuildRequestedEventEventFirer; | ||
|
||
@Mock | ||
private UtcClock clock; | ||
private RebuildProcessRunner rebuildProcessRunner; | ||
|
||
@Mock | ||
private Logger logger; | ||
private MdcLogger mdcLogger; | ||
|
||
@InjectMocks | ||
private RebuildCommandHandler rebuildCommandHandler; | ||
|
||
private Consumer<Runnable> testConsumer = Runnable::run; | ||
|
||
@Test | ||
public void shouldFireRebuildEvent() throws Exception { | ||
|
||
final UUID commandId = randomUUID(); | ||
final RebuildCommand rebuildCommand = new RebuildCommand(); | ||
final ZonedDateTime now = of(2019, 8, 23, 11, 22, 1, 0, UTC); | ||
|
||
when(clock.now()).thenReturn(now); | ||
when(mdcLogger.mdcLoggerConsumer()).thenReturn(testConsumer); | ||
|
||
rebuildCommandHandler.doRebuild(rebuildCommand, commandId); | ||
|
||
verify(logger).info("Received command 'REBUILD' at 11:22:01 AM"); | ||
verify(rebuildRequestedEventEventFirer).fire(new RebuildRequestedEvent(commandId, now, rebuildCommand)); | ||
verify(rebuildProcessRunner).runRebuild(commandId, rebuildCommand); | ||
} | ||
} |
82 changes: 0 additions & 82 deletions
82
.../uk/gov/justice/services/eventstore/management/rebuild/observers/RebuildObserverTest.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.