Skip to content

Commit

Permalink
Use constructor injection for singleton (airbytehq#18163)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdpgrailsdev authored and jhammarstedt committed Oct 31, 2022
1 parent 72b2c43 commit b970a27
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@

package io.airbyte.cron.config;

import io.airbyte.config.Configs.DeploymentMode;
import io.airbyte.config.persistence.split_secrets.JsonSecretsProcessor;
import io.micronaut.context.annotation.Factory;
import io.micronaut.context.annotation.Value;
import io.micronaut.core.util.StringUtils;
import jakarta.inject.Singleton;
import java.util.Locale;
import java.util.function.Function;
import lombok.extern.slf4j.Slf4j;

/**
Expand All @@ -16,11 +21,20 @@
@Slf4j
public class ApplicationBeanFactory {

@Singleton
public DeploymentMode deploymentMode(@Value("${airbyte.deployment-mode}") final String deploymentMode) {
return convertToEnum(deploymentMode, DeploymentMode::valueOf, DeploymentMode.OSS);
}

@Singleton
public JsonSecretsProcessor jsonSecretsProcessor() {
return JsonSecretsProcessor.builder()
.copySecrets(false)
.build();
}

private <T> T convertToEnum(final String value, final Function<String, T> creatorFunction, final T defaultValue) {
return StringUtils.isNotEmpty(value) ? creatorFunction.apply(value.toUpperCase(Locale.ROOT)) : defaultValue;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@

import datadog.trace.api.Trace;
import io.airbyte.config.Configs.DeploymentMode;
import io.airbyte.config.EnvConfigs;
import io.airbyte.config.init.ApplyDefinitionsHelper;
import io.airbyte.config.init.RemoteDefinitionsProvider;
import io.airbyte.config.persistence.ConfigRepository;
import io.micronaut.context.annotation.Value;
import io.micronaut.scheduling.annotation.Scheduled;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import java.net.URI;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -29,29 +27,33 @@
@Slf4j
public class DefinitionsUpdater {

@Inject
private ConfigRepository configRepository;
private final ConfigRepository configRepository;

@Value("${airbyte.cron.update-definitions.enabled}")
private boolean shouldUpdateDefinitions;
private final boolean shouldUpdateDefinitions;

private final URI remoteCatalogUrl;
private final DeploymentMode deploymentMode;

public DefinitionsUpdater() {
public DefinitionsUpdater(final ConfigRepository configRepository,
final DeploymentMode deploymentMode,
@Value("${airbyte.remote-connector-catalog-url}") final String remoteCatalogUrl,
@Value("${airbyte.cron.update-definitions.enabled}") final boolean shouldUpdateDefinitions) {
log.info("Creating connector definitions updater");

final EnvConfigs envConfigs = new EnvConfigs();
remoteCatalogUrl = envConfigs.getRemoteConnectorCatalogUrl().orElse(null);
deploymentMode = envConfigs.getDeploymentMode();
this.configRepository = configRepository;
this.deploymentMode = deploymentMode;
this.remoteCatalogUrl = remoteCatalogUrl != null ? URI.create(remoteCatalogUrl) : null;
this.shouldUpdateDefinitions = shouldUpdateDefinitions;
}

@Trace(operationName = SCHEDULED_TRACE_OPERATION_NAME)
@Scheduled(fixedRate = "30s",
initialDelay = "1m")
void updateDefinitions() {
if (!shouldUpdateDefinitions)
if (!shouldUpdateDefinitions) {
log.info("Connector definitions update disabled.");
return;
}

if (remoteCatalogUrl == null) {
log.warn("Tried to update definitions, but the remote catalog url is not set");
Expand Down
1 change: 1 addition & 0 deletions airbyte-cron/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ airbyte:
local:
docker-mount: ${LOCAL_DOCKER_MOUNT:}
root: ${LOCAL_ROOT}
remote-connector-catalog-url: ${REMOTE_CONNECTOR_CATALOG_URL:}
role: ${AIRBYTE_ROLE:}
temporal:
worker:
Expand Down

0 comments on commit b970a27

Please sign in to comment.