Skip to content

Commit

Permalink
switched to bus and added db populate (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
dwasinge authored May 8, 2020
1 parent 5578748 commit 889b4c7
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 43 deletions.
42 changes: 42 additions & 0 deletions src/main/java/com/redhat/labs/omp/model/event/BackendEvent.java
Original file line number Diff line number Diff line change
@@ -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<Engagement> 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<Engagement> 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<Engagement> engagementList) {
return BackendEvent.builder().eventType(EventType.UPDATE_ENGAGEMENTS_IN_DB_REQUESTED)
.engagementList(engagementList).build();
}

public static BackendEvent createUpdateEngagementsInGitRequestedEvent(List<Engagement> engagementList) {
return BackendEvent.builder().eventType(EventType.UPDATE_ENGAGEMENTS_IN_GIT_REQUESTED)
.engagementList(engagementList).build();
}

}
31 changes: 31 additions & 0 deletions src/main/java/com/redhat/labs/omp/model/event/EventType.java
Original file line number Diff line number Diff line change
@@ -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";

}

}
14 changes: 10 additions & 4 deletions src/main/java/com/redhat/labs/omp/resource/GitSyncResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -24,7 +26,7 @@
public class GitSyncResource {

@Inject
GitSyncService service;
EventBus eventBus;

@Inject
JsonWebToken jwt;
Expand All @@ -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();

}
Expand All @@ -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();

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -25,7 +27,7 @@ public class ActiveGitSyncService {
ActiveSyncRepository activeSyncRepository;

@Inject
GitSyncService gitSyncService;
EventBus eventBus;

private final UUID uuid = UUID.randomUUID();

Expand All @@ -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);
}

}

/**
Expand Down Expand Up @@ -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);

}

}
Expand Down
87 changes: 79 additions & 8 deletions src/main/java/com/redhat/labs/omp/service/EngagementService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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
Expand Down Expand Up @@ -139,7 +144,7 @@ public List<Engagement> 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.");
}
Expand All @@ -149,7 +154,7 @@ private void deleteAll() {
*
* @param engagementList
*/
private void insertEngagementListInRepository(List<Engagement> engagementList) {
void insertEngagementListInRepository(List<Engagement> engagementList) {
repository.persist(engagementList);
}

Expand All @@ -158,7 +163,7 @@ private void insertEngagementListInRepository(List<Engagement> engagementList) {
*
* @param engagementList
*/
public void updateEngagementListInRepository(List<Engagement> engagementList) {
void updateEngagementListInRepository(List<Engagement> engagementList) {
repository.update(engagementList);
}

Expand All @@ -168,7 +173,7 @@ public void updateEngagementListInRepository(List<Engagement> engagementList) {
*
* @param engagementList
*/
public void syncWithGitLab(List<Engagement> engagementList) {
void refreshFromEngagementList(List<Engagement> engagementList) {

// remove all from database
deleteAll();
Expand All @@ -183,7 +188,7 @@ public void syncWithGitLab(List<Engagement> engagementList) {
*
* @return
*/
public List<Engagement> getModifiedEngagements() {
List<Engagement> getModifiedEngagements() {
return repository.findByModified();
}

Expand All @@ -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<Engagement> 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);

}

}
Loading

0 comments on commit 889b4c7

Please sign in to comment.