From 889b4c7756ab7c988a199e33f56deb516cacf5a5 Mon Sep 17 00:00:00 2001 From: Derek Wasinger Date: Fri, 8 May 2020 13:59:52 -0700 Subject: [PATCH] switched to bus and added db populate (#64) --- .../labs/omp/model/event/BackendEvent.java | 42 +++++++++ .../labs/omp/model/event/EventType.java | 31 +++++++ .../labs/omp/resource/GitSyncResource.java | 14 ++- .../omp/service/ActiveGitSyncService.java | 18 +++- .../labs/omp/service/EngagementService.java | 87 +++++++++++++++++-- .../labs/omp/service/GitSyncService.java | 60 +++++++------ 6 files changed, 209 insertions(+), 43 deletions(-) create mode 100644 src/main/java/com/redhat/labs/omp/model/event/BackendEvent.java create mode 100644 src/main/java/com/redhat/labs/omp/model/event/EventType.java diff --git a/src/main/java/com/redhat/labs/omp/model/event/BackendEvent.java b/src/main/java/com/redhat/labs/omp/model/event/BackendEvent.java new file mode 100644 index 00000000..4fb26931 --- /dev/null +++ b/src/main/java/com/redhat/labs/omp/model/event/BackendEvent.java @@ -0,0 +1,42 @@ +package com.redhat.labs.omp.model.event; + +import java.util.List; + +import com.redhat.labs.omp.model.Engagement; + +import lombok.Builder; +import lombok.Data; + +@Data +@Builder +public class BackendEvent { + + private EventType eventType; + private List engagementList; + @Builder.Default + private boolean forceUpdate = false; + + public static BackendEvent createDatabaseRefreshRequestedEvent(boolean forceUpdate) { + return BackendEvent.builder().eventType(EventType.DB_REFRESH_REQUESTED).forceUpdate(forceUpdate).build(); + } + + public static BackendEvent createDatabaseRefreshEvent(List engagmentList, boolean forceUpdate) { + return BackendEvent.builder().eventType(EventType.DB_REFRESH).engagementList(engagmentList) + .forceUpdate(forceUpdate).build(); + } + + public static BackendEvent createPushToGitRequestedEvent() { + return BackendEvent.builder().eventType(EventType.PUSH_TO_GIT_REQUESTED).build(); + } + + public static BackendEvent createUpdateEngagementsInDbRequestedEvent(List engagementList) { + return BackendEvent.builder().eventType(EventType.UPDATE_ENGAGEMENTS_IN_DB_REQUESTED) + .engagementList(engagementList).build(); + } + + public static BackendEvent createUpdateEngagementsInGitRequestedEvent(List engagementList) { + return BackendEvent.builder().eventType(EventType.UPDATE_ENGAGEMENTS_IN_GIT_REQUESTED) + .engagementList(engagementList).build(); + } + +} \ No newline at end of file diff --git a/src/main/java/com/redhat/labs/omp/model/event/EventType.java b/src/main/java/com/redhat/labs/omp/model/event/EventType.java new file mode 100644 index 00000000..eb3c94b3 --- /dev/null +++ b/src/main/java/com/redhat/labs/omp/model/event/EventType.java @@ -0,0 +1,31 @@ +package com.redhat.labs.omp.model.event; + +public enum EventType { + + DB_REFRESH_REQUESTED(Constants.DB_REFRESH_REQUESTED_ADDRESS), + DB_REFRESH(Constants.DB_REFRESH_ADDRESS), + PUSH_TO_GIT_REQUESTED(Constants.PUSH_TO_GIT_REQUESTED_ADDRESS), + UPDATE_ENGAGEMENTS_IN_DB_REQUESTED(Constants.UPDATE_ENGAGEMENTS_IN_DB_REQUESTED_ADDRESS), + UPDATE_ENGAGEMENTS_IN_GIT_REQUESTED(Constants.UPDATE_ENGAGEMENTS_IN_GIT_REQUESTED_ADDRESS); + + private String eventBusAddress; + + EventType(String eventBusAddress) { + this.eventBusAddress = eventBusAddress; + } + + public String getEventBusAddress() { + return this.eventBusAddress; + } + + public class Constants { + + public static final String DB_REFRESH_REQUESTED_ADDRESS = "db.refresh.requested.event"; + public static final String DB_REFRESH_ADDRESS = "db.refresh.event"; + public static final String PUSH_TO_GIT_REQUESTED_ADDRESS = "push.to.git.requested.event"; + public static final String UPDATE_ENGAGEMENTS_IN_DB_REQUESTED_ADDRESS = "update.engagements.in.db.requested.event"; + public static final String UPDATE_ENGAGEMENTS_IN_GIT_REQUESTED_ADDRESS = "update.engagements.in.git.requested.event"; + + } + +} \ No newline at end of file diff --git a/src/main/java/com/redhat/labs/omp/resource/GitSyncResource.java b/src/main/java/com/redhat/labs/omp/resource/GitSyncResource.java index b5b5aac4..affb0e75 100644 --- a/src/main/java/com/redhat/labs/omp/resource/GitSyncResource.java +++ b/src/main/java/com/redhat/labs/omp/resource/GitSyncResource.java @@ -15,7 +15,9 @@ import org.eclipse.microprofile.openapi.annotations.responses.APIResponses; import org.eclipse.microprofile.openapi.annotations.security.SecurityRequirement; -import com.redhat.labs.omp.service.GitSyncService; +import com.redhat.labs.omp.model.event.BackendEvent; + +import io.vertx.mutiny.core.eventbus.EventBus; @RequestScoped @Path("/engagements") @@ -24,7 +26,7 @@ public class GitSyncResource { @Inject - GitSyncService service; + EventBus eventBus; @Inject JsonWebToken jwt; @@ -38,7 +40,9 @@ public class GitSyncResource { @Operation(summary = "Purges the database and refreshes it with data in git.") public Response refresh() { - service.refreshBackedFromGit(); + // send request event with force set to true + BackendEvent event = BackendEvent.createDatabaseRefreshRequestedEvent(true); + eventBus.sendAndForget(event.getEventType().getEventBusAddress(), event); return Response.ok().build(); } @@ -52,7 +56,9 @@ public Response refresh() { @Operation(summary = "Sends all modified engagements to git to be stored.") public Response push() { - service.processModifiedEngagements(); + // send time elapsed event to start push to git from db + BackendEvent event = BackendEvent.createPushToGitRequestedEvent(); + eventBus.sendAndForget(event.getEventType().getEventBusAddress(), event); return Response.ok().build(); } diff --git a/src/main/java/com/redhat/labs/omp/service/ActiveGitSyncService.java b/src/main/java/com/redhat/labs/omp/service/ActiveGitSyncService.java index 9a76132e..eb537b90 100644 --- a/src/main/java/com/redhat/labs/omp/service/ActiveGitSyncService.java +++ b/src/main/java/com/redhat/labs/omp/service/ActiveGitSyncService.java @@ -11,11 +11,13 @@ import org.slf4j.LoggerFactory; import com.redhat.labs.omp.model.ActiveSync; +import com.redhat.labs.omp.model.event.BackendEvent; import com.redhat.labs.omp.repository.ActiveSyncRepository; import io.quarkus.panache.common.Sort; import io.quarkus.runtime.StartupEvent; import io.quarkus.scheduler.Scheduled; +import io.vertx.mutiny.core.eventbus.EventBus; public class ActiveGitSyncService { @@ -25,7 +27,7 @@ public class ActiveGitSyncService { ActiveSyncRepository activeSyncRepository; @Inject - GitSyncService gitSyncService; + EventBus eventBus; private final UUID uuid = UUID.randomUUID(); @@ -44,6 +46,13 @@ void onStart(@Observes StartupEvent event) { // try to set active flag checkIfActive(); + // sync mongo with git if no engagements found in mongo + if (active) { + LOGGER.debug("populating database from git..."); + BackendEvent refreshDbEvent = BackendEvent.createDatabaseRefreshRequestedEvent(false); + eventBus.sendAndForget(refreshDbEvent.getEventType().getEventBusAddress(), refreshDbEvent); + } + } /** @@ -122,8 +131,11 @@ void checkIfActive() { void pushModififedEngagementsToGit() { if (active) { - LOGGER.debug("scheduled job for send process time elapsed event triggered. {}", active); - gitSyncService.processModifiedEngagements(); + + LOGGER.debug("{} emitting a process time elapsed event.", uuid); + BackendEvent event = BackendEvent.createPushToGitRequestedEvent(); + eventBus.sendAndForget(event.getEventType().getEventBusAddress(), event); + } } diff --git a/src/main/java/com/redhat/labs/omp/service/EngagementService.java b/src/main/java/com/redhat/labs/omp/service/EngagementService.java index f68aecd7..8b535295 100644 --- a/src/main/java/com/redhat/labs/omp/service/EngagementService.java +++ b/src/main/java/com/redhat/labs/omp/service/EngagementService.java @@ -18,8 +18,13 @@ import com.redhat.labs.omp.model.Engagement; import com.redhat.labs.omp.model.FileAction; import com.redhat.labs.omp.model.Launch; +import com.redhat.labs.omp.model.event.BackendEvent; +import com.redhat.labs.omp.model.event.EventType; import com.redhat.labs.omp.repository.EngagementRepository; +import io.quarkus.vertx.ConsumeEvent; +import io.vertx.mutiny.core.eventbus.EventBus; + @ApplicationScoped public class EngagementService { @@ -39,7 +44,7 @@ public class EngagementService { EngagementRepository repository; @Inject - GitSyncService gitSyncService; + EventBus eventBus; /** * Creates a new {@link Engagement} resource in the data store and marks if for @@ -139,7 +144,7 @@ public List getAll() { * Used by the {@link GitSyncService} to delete all {@link Engagement} from the * data store before re-populating from Git. */ - private void deleteAll() { + void deleteAll() { long count = repository.deleteAll(); LOGGER.info("removed '" + count + "' engagements from the data store."); } @@ -149,7 +154,7 @@ private void deleteAll() { * * @param engagementList */ - private void insertEngagementListInRepository(List engagementList) { + void insertEngagementListInRepository(List engagementList) { repository.persist(engagementList); } @@ -158,7 +163,7 @@ private void insertEngagementListInRepository(List engagementList) { * * @param engagementList */ - public void updateEngagementListInRepository(List engagementList) { + void updateEngagementListInRepository(List engagementList) { repository.update(engagementList); } @@ -168,7 +173,7 @@ public void updateEngagementListInRepository(List engagementList) { * * @param engagementList */ - public void syncWithGitLab(List engagementList) { + void refreshFromEngagementList(List engagementList) { // remove all from database deleteAll(); @@ -183,7 +188,7 @@ public void syncWithGitLab(List engagementList) { * * @return */ - public List getModifiedEngagements() { + List getModifiedEngagements() { return repository.findByModified(); } @@ -209,10 +214,76 @@ public Engagement launch(Engagement engagement) { Engagement updated = update(engagement.getCustomerName(), engagement.getProjectName(), engagement); // sync change(s) to git - gitSyncService.processModifiedEngagements(); + sendEngagementsModifiedEvent(); return updated; } -} + /** + * Consumes a {@link BackendEvent}. If the force flag on the event is true or + * there are no {@link Engagement} currently in the database, the {@link List} + * of {@link Engagement}s in the event will be inserted into the database. + * Please note that the database will be purged before the insert happens. + * + * @param event + */ + @ConsumeEvent(EventType.Constants.DB_REFRESH_ADDRESS) + void consumeDbRefreshRequestedEvent(BackendEvent event) { + + if (!event.isForceUpdate() && getAll().size() > 0) { + LOGGER.debug("engagements already exist in db and force is not set. doing nothing for db refresh request."); + return; + } + + LOGGER.debug("purging existing engagements from db and inserting from event list {}", event.getEngagementList()); + // refresh the db + refreshFromEngagementList(event.getEngagementList()); + + } + + /** + * Consumes a {@link BackendEvent} and updates the database using the + * {@link List} of {@link Engagement}s that are contained in the event. + * + * @param event + */ + @ConsumeEvent(EventType.Constants.UPDATE_ENGAGEMENTS_IN_DB_REQUESTED_ADDRESS) + void consumeUpdateEngagementsInDbRequestedEvent(BackendEvent event) { + updateEngagementListInRepository(event.getEngagementList()); + } + + /** + * Consumes the {@link BackendEvent} and triggers the processing of any modified + * {@link Engagement}s. + * + * @param event + */ + @ConsumeEvent(EventType.Constants.PUSH_TO_GIT_REQUESTED_ADDRESS) + void consumePushToGitRequestedEvent(BackendEvent event) { + + LOGGER.debug("consuming process time elapsed event."); + + sendEngagementsModifiedEvent(); + } + + /** + * If any {@link Engagement}s in the database have been modified, it creates a + * {@link BackendEvent} and places it on the {@link EventBus} for processing. + */ + void sendEngagementsModifiedEvent() { + + List modifiedList = getModifiedEngagements(); + + if (modifiedList.size() == 0) { + LOGGER.debug("no modified engagements to process"); + return; + } + + LOGGER.debug("emitting db engagements modified event"); + BackendEvent event = BackendEvent.createUpdateEngagementsInGitRequestedEvent(modifiedList); + eventBus.sendAndForget(event.getEventType().getEventBusAddress(), event); + + } + +} \ No newline at end of file diff --git a/src/main/java/com/redhat/labs/omp/service/GitSyncService.java b/src/main/java/com/redhat/labs/omp/service/GitSyncService.java index acf00d90..37fd8dd7 100644 --- a/src/main/java/com/redhat/labs/omp/service/GitSyncService.java +++ b/src/main/java/com/redhat/labs/omp/service/GitSyncService.java @@ -13,56 +13,59 @@ import com.redhat.labs.omp.model.Engagement; import com.redhat.labs.omp.model.FileAction; -import com.redhat.labs.omp.repository.ActiveSyncRepository; +import com.redhat.labs.omp.model.event.BackendEvent; +import com.redhat.labs.omp.model.event.EventType; import com.redhat.labs.omp.rest.client.OMPGitLabAPIService; +import io.quarkus.vertx.ConsumeEvent; +import io.vertx.mutiny.core.eventbus.EventBus; + @ApplicationScoped public class GitSyncService { private static final Logger LOGGER = LoggerFactory.getLogger(GitSyncService.class); - @Inject - ActiveSyncRepository activeSyncRepository; - - @Inject - EngagementService engagementService; - @Inject @RestClient OMPGitLabAPIService gitApiClient; - public void refreshBackedFromGit() { - - LOGGER.info("refreshing backend data from Git..."); - - // get all engagements from git - List engagementList = gitApiClient.getEngagments(); + @Inject + EventBus eventBus; - // call sync - engagementService.syncWithGitLab(engagementList); + /** + * This method consumes a {@link BackendEvent}, enriches the event with + * {@link Engagement}s from Git and sends the event to the {@link EventBus} for + * processing. + * + * @param event + */ + @ConsumeEvent(EventType.Constants.DB_REFRESH_REQUESTED_ADDRESS) + void consumeDbRefreshRequestedEvent(BackendEvent event) { - LOGGER.info("refresh of backend data complete."); + LOGGER.debug("processing db refresh request event"); - } + // create refresh event + BackendEvent refreshDbEvent = BackendEvent.createDatabaseRefreshEvent(gitApiClient.getEngagments(), + event.isForceUpdate()); + // send event to bus for processing + eventBus.sendAndForget(refreshDbEvent.getEventType().getEventBusAddress(), refreshDbEvent); - /** - * Processes modified {@link Engagement} in the data store by calling the Git - * API to update Git. - */ - public void processModifiedEngagements() { - processModifiedEngagements(engagementService.getModifiedEngagements()); } /** * Processing the list of {@link Engagement} that have been modified. The Git * API will be called to process the engagement depending on the * {@link FileAction} specified. After the REST call completes, the - * {@link Engagement} will be reset in the data store. + * {@link Engagement} updated list will be added to a {@link BackendEvent} and + * added to the {@link EventBus} to trigger an updated in the database. * * @param engagementList * @param action */ - private void processModifiedEngagements(List engagementList) { + @ConsumeEvent(EventType.Constants.UPDATE_ENGAGEMENTS_IN_GIT_REQUESTED_ADDRESS) + void consumeUpdateEngagementsInGitRequestedEvent(BackendEvent event) { + + List engagementList = event.getEngagementList(); for (Engagement engagement : engagementList) { @@ -96,8 +99,9 @@ private void processModifiedEngagements(List engagementList) { } - // update engagements in db - engagementService.updateEngagementListInRepository(engagementList); + // send event to update in db + BackendEvent updateInDbEvent = BackendEvent.createUpdateEngagementsInDbRequestedEvent(engagementList); + eventBus.sendAndForget(updateInDbEvent.getEventType().getEventBusAddress(), updateInDbEvent); } @@ -119,4 +123,4 @@ private void updateIdFromResponse(Engagement engagement, Response response) { } -} +} \ No newline at end of file