-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from samply/feature/exporter-job
Feature/exporter job
- Loading branch information
Showing
20 changed files
with
372 additions
and
148 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
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,88 @@ | ||
package de.samply.exporter; | ||
|
||
import de.samply.app.ProjectManagerConst; | ||
import de.samply.db.model.ProjectBridgehead; | ||
import de.samply.db.repository.ProjectBridgeheadRepository; | ||
import de.samply.project.state.ProjectState; | ||
import de.samply.query.QueryState; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Component; | ||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Mono; | ||
|
||
import java.time.Instant; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.function.Consumer; | ||
import java.util.function.Function; | ||
|
||
@Component | ||
public class ExporterJob { | ||
|
||
private final boolean enabled; | ||
private final ExporterService exporterService; | ||
private final ProjectBridgeheadRepository projectBridgeheadRepository; | ||
private final Set<ProjectState> activeStates = Set.of(ProjectState.DEVELOP, ProjectState.PILOT, ProjectState.FINAL); | ||
|
||
public ExporterJob( | ||
@Value(ProjectManagerConst.ENABLE_EXPORTER_SV) boolean enabled, | ||
ExporterService exporterService, | ||
ProjectBridgeheadRepository projectBridgeheadRepository) { | ||
this.enabled = enabled; | ||
this.exporterService = exporterService; | ||
this.projectBridgeheadRepository = projectBridgeheadRepository; | ||
} | ||
|
||
@Scheduled(cron = ProjectManagerConst.EXPORTER_CRON_EXPRESSION_SV) | ||
public void checkExports() { | ||
if (enabled) { | ||
Mono.when( | ||
checkQueriesToSend(), | ||
checkQueriesToSendAndExecute(), | ||
checkQueriesAlreadySent(), | ||
checkQueriesAlreadySentToBeExecuted()).block(); | ||
|
||
} | ||
} | ||
|
||
private Mono<Void> checkQueriesToSend() { | ||
return checkQueries(QueryState.TO_BE_SENT, QueryState.SENDING, exporterService::sendQueryToBridgehead); | ||
} | ||
|
||
private Mono<Void> checkQueriesToSendAndExecute() { | ||
return checkQueries(QueryState.TO_BE_SENT_AND_EXECUTED, QueryState.SENDING_AND_EXECUTING, exporterService::sendQueryToBridgeheadAndExecute); | ||
} | ||
|
||
private Mono<Void> checkQueriesAlreadySent() { | ||
return checkQueries(QueryState.SENDING, QueryState.FINISHED, exporterService::checkIfQueryIsAlreadySent); | ||
} | ||
|
||
private Mono<Void> checkQueriesAlreadySentToBeExecuted() { | ||
return checkQueries(QueryState.SENDING_AND_EXECUTING, QueryState.FINISHED, exporterService::checkIfQueryIsAlreadySent, Optional.of( | ||
exporterServiceResult -> | ||
exporterService.fetchExporterExecutionIdFromExporterResponse(exporterServiceResult.result()).ifPresent(exportExecutionId -> | ||
exporterServiceResult.projectBridgehead().setExporterExecutionId(exportExecutionId)))); | ||
} | ||
|
||
private Mono<Void> checkQueries(QueryState initialQueryState, QueryState finalQueryState, | ||
Function<ProjectBridgehead, Mono<ExporterServiceResult>> exporterServiceFunction) { | ||
return checkQueries(initialQueryState, finalQueryState, exporterServiceFunction, Optional.empty()); | ||
} | ||
|
||
private Mono<Void> checkQueries(QueryState initialQueryState, QueryState finalQueryState, | ||
Function<ProjectBridgehead, Mono<ExporterServiceResult>> exporterServiceFunction, Optional<Consumer<ExporterServiceResult>> exporterServiceResultConsumer) { | ||
return Flux.fromIterable(projectBridgeheadRepository.getByQueryStateAndProjectState(initialQueryState, activeStates)) | ||
.flatMap(exporterServiceFunction) | ||
.flatMap(exporterServiceResult -> { | ||
exporterServiceResultConsumer.ifPresent(consumer -> consumer.accept(exporterServiceResult)); | ||
ProjectBridgehead projectBridgehead = exporterServiceResult.projectBridgehead(); | ||
projectBridgehead.setQueryState(finalQueryState); | ||
projectBridgehead.setExporterResponse(exporterServiceResult.result()); | ||
projectBridgehead.setModifiedAt(Instant.now()); | ||
projectBridgeheadRepository.save(projectBridgehead); | ||
return Mono.empty(); | ||
}).then(); | ||
} | ||
|
||
} |
Oops, something went wrong.