Skip to content

Commit

Permalink
Merge pull request #3304 from ingef/fix/form_scan_fail_independently
Browse files Browse the repository at this point in the history
Fix/form scan fail independently
  • Loading branch information
thoniTUB authored Feb 22, 2024
2 parents b5e6216 + 6ec79a2 commit cfca127
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.bakdata.conquery.models.config.auth.AuthenticationClientFilterProvider;
import com.bakdata.conquery.models.config.auth.MultiInstancePlugin;
import com.bakdata.conquery.models.datasets.Dataset;
import com.bakdata.conquery.models.forms.frontendconfiguration.FormConfigProvider;
import com.bakdata.conquery.models.forms.frontendconfiguration.FormFrontendConfigInformation;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -97,7 +98,8 @@ public void initialize(ManagerNode managerNode) {
managerNode.getEnvironment().healthChecks().register(getId(), externalApi.createHealthCheck());

// Register form configuration provider
managerNode.getFormScanner().registerFrontendFormConfigProvider(this::registerFormConfigs);
log.info("Registering frontend form config provider for '{}'", getId());
managerNode.getFormScanner().registerFrontendFormConfigProvider(new FormConfigProvider(getId(), this::registerFormConfigs));
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.bakdata.conquery.models.forms.frontendconfiguration;

import java.util.function.Consumer;

import com.google.common.collect.ImmutableCollection;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

/**
* Helper class to hold the identifier and the provider method for a form config provider
*/
@RequiredArgsConstructor
public class FormConfigProvider {
@Getter
private final String providerName;
private final Consumer<ImmutableCollection.Builder<FormFrontendConfigInformation>> provider;

public void addFormConfigs(ImmutableCollection.Builder<FormFrontendConfigInformation> collect) {
provider.accept(collect);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;

import javax.annotation.Nullable;

Expand All @@ -21,11 +21,9 @@
import com.bakdata.conquery.models.config.FrontendConfig;
import com.bakdata.conquery.models.config.ManualConfig;
import com.bakdata.conquery.resources.admin.rest.AdminProcessor;
import com.bakdata.conquery.util.QueryUtils;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;
import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder;
Expand All @@ -45,16 +43,16 @@ public class FormScanner extends Task {
* task accounts the change.
*/
private final ConqueryConfig config;
private Consumer<ImmutableCollection.Builder<FormFrontendConfigInformation>> providerChain = QueryUtils.getNoOpEntryPoint();
private List<FormConfigProvider> formConfigProviders = new ArrayList<>();

public FormScanner(ConqueryConfig config) {
super("form-scanner");
this.config = config;
registerFrontendFormConfigProvider(ResourceFormConfigProvider::accept);
registerFrontendFormConfigProvider(new FormConfigProvider("internal", ResourceFormConfigProvider::accept));
}

public synchronized void registerFrontendFormConfigProvider(Consumer<ImmutableCollection.Builder<FormFrontendConfigInformation>> provider) {
providerChain = providerChain.andThen(provider);
public synchronized void registerFrontendFormConfigProvider(FormConfigProvider provider) {
formConfigProviders.add(provider);
}


Expand All @@ -70,6 +68,18 @@ public static Set<FormType> getAllFormTypes() {
@Override
public void execute(Map<String, List<String>> parameters, PrintWriter output) throws Exception {
FRONTEND_FORM_CONFIGS = generateFEFormConfigMap();

if (output == null) {
// Not called from a request
return;
}

// If called from a request, respond with findings
output.write("Registered forms:\n");
for (String formId : FRONTEND_FORM_CONFIGS.keySet()) {
output.write(String.format("\t%s\n", formId));
}
output.flush();
}

private Map<String, FormType> generateFEFormConfigMap() {
Expand Down Expand Up @@ -170,19 +180,26 @@ private static Map<String, Class<? extends Form>> findBackendMappingClasses() {

/**
* Frontend form configurations can be provided from different sources.
* Each source must register a provider with {@link FormScanner#registerFrontendFormConfigProvider(Consumer)} beforehand.
* Each source must register a provider with {@link FormScanner#registerFrontendFormConfigProvider(FormConfigProvider)} beforehand.
*/
@SneakyThrows
private List<FormFrontendConfigInformation> findFrontendFormConfigs() {

final ImmutableList.Builder<FormFrontendConfigInformation> frontendConfigs = ImmutableList.builder();

try {
providerChain.accept(frontendConfigs);
}
catch (Exception e) {
log.error("Unable to collect all frontend form configurations.", e);
log.trace("Begin collecting form frontend configurations");

for (FormConfigProvider formConfigProvider : formConfigProviders) {

try {
formConfigProvider.addFormConfigs(frontendConfigs);
}
catch (Exception e) {
log.error("Unable to collect frontend form configurations from {}.", formConfigProvider.getProviderName(), e);
}
}

log.trace("Finished collecting form frontend configurations");
return frontendConfigs.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
* In order to be found, the configuration file name must end with {@code *.frontend_conf.json}.
*/
@Slf4j
public class ResourceFormConfigProvider{
public class ResourceFormConfigProvider {

public static void accept(ImmutableCollection.Builder<FormFrontendConfigInformation> formConfigInfos) {
ResourceList frontendConfigs = CPSTypeIdResolver.SCAN_RESULT
.getResourcesMatchingPattern(Pattern.compile(".*\\.frontend_conf\\.json"));
.getResourcesMatchingPattern(Pattern.compile(".*\\.frontend_conf\\.json"));

for (Resource config : frontendConfigs) {
try (config){
try(InputStream in = config.open()) {
try (config) {
try (InputStream in = config.open()) {
JsonNode configTree = Jackson.MAPPER.reader().readTree(in);
if (!configTree.isObject()) {
log.warn("Expected '{}' to be an JSON object but was '{}'. Skipping registration.", config.getPath(), configTree.getNodeType());
Expand Down

0 comments on commit cfca127

Please sign in to comment.