diff --git a/src/main/java/com/devcycle/sdk/server/local/DVCLocalClient.java b/src/main/java/com/devcycle/sdk/server/local/DVCLocalClient.java index 9aa7ada7..48ff8977 100755 --- a/src/main/java/com/devcycle/sdk/server/local/DVCLocalClient.java +++ b/src/main/java/com/devcycle/sdk/server/local/DVCLocalClient.java @@ -14,6 +14,8 @@ public final class DVCLocalClient { private final DVCOptions dvcOptions; private static LocalBucketing localBucketing = new LocalBucketing(); + + private EnvironmentConfigManager configManager; private final String serverKey; @@ -24,8 +26,8 @@ public DVCLocalClient(String serverKey) { this(serverKey, DVCOptions.builder().build()); } - public DVCLocalClient(String serverKey, DVCOptions dvcOptions ) { - new EnvironmentConfigManager(serverKey, dvcOptions); + public DVCLocalClient(String serverKey, DVCOptions dvcOptions) { + configManager = new EnvironmentConfigManager(serverKey, localBucketing, dvcOptions); this.serverKey = serverKey; this.dvcOptions = dvcOptions; OBJECT_MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL); @@ -65,7 +67,11 @@ public Variable variable(User user, String key, T defaultValue) { if (defaultValue == null) { throw new IllegalArgumentException("defaultValue cannot be null"); } - + + if (!configManager.isConfigInitialized()) { + System.out.println("Variable called before DVCClient has initialized, returning default value"); + } + try { String userString = OBJECT_MAPPER.writeValueAsString(user); @@ -88,6 +94,13 @@ public Variable variable(User user, String key, T defaultValue) { .reasonUsingDefaultValue("Variable not found") .build(); + // TODO queue events + // eventQueue.queueAggregateEvent( + // user, + // new Event(type: EventTypes.variableDefaulted, target: key), + // null + // ); + return variable; } diff --git a/src/main/java/com/devcycle/sdk/server/local/EnvironmentConfigManager.java b/src/main/java/com/devcycle/sdk/server/local/EnvironmentConfigManager.java index e484b0c3..03c2081c 100644 --- a/src/main/java/com/devcycle/sdk/server/local/EnvironmentConfigManager.java +++ b/src/main/java/com/devcycle/sdk/server/local/EnvironmentConfigManager.java @@ -30,7 +30,9 @@ public final class EnvironmentConfigManager { private String environmentKey; private int pollingIntervalMS; - public EnvironmentConfigManager(String environmentKey, DVCOptions options) { + private LocalBucketing localBucketing; + + public EnvironmentConfigManager(String environmentKey, LocalBucketing localBucketing, DVCOptions options) { this.environmentKey = environmentKey; configApiClient = new DVCApiClient(environmentKey, options).initialize(); @@ -38,6 +40,8 @@ public EnvironmentConfigManager(String environmentKey, DVCOptions options) { int configPollingIntervalMs = options.getConfigPollingIntervalMs(); pollingIntervalMS = configPollingIntervalMs >= MIN_INTERVALS_MS ? configPollingIntervalMs : DEFAULT_POLL_INTERVAL_MS; + + this.localBucketing = localBucketing; setupScheduler(); } @@ -47,7 +51,7 @@ private void setupScheduler() { public void run() { try { getConfig(); - } catch (DVCException e) { + } catch (DVCException | JsonProcessingException e) { e.printStackTrace(); } } @@ -56,14 +60,18 @@ public void run() { scheduler.scheduleAtFixedRate(getConfigRunnable, 0, this.pollingIntervalMS, TimeUnit.MILLISECONDS); } - private ProjectConfig getConfig() throws DVCException { + public boolean isConfigInitialized() { + return config != null; + } + + private ProjectConfig getConfig() throws DVCException, JsonProcessingException { Call config = this.configApiClient.getConfig(this.environmentKey, this.configETag); this.config = getConfigResponse(config); return this.config; } - private ProjectConfig getConfigResponse(Call call) throws DVCException { + private ProjectConfig getConfigResponse(Call call) throws DVCException, JsonProcessingException { ErrorResponse errorResponse = ErrorResponse.builder().build(); Response response; @@ -78,6 +86,18 @@ private ProjectConfig getConfigResponse(Call call) throws DVCExce errorResponse.setMessage("Unknown error"); if (response.isSuccessful()) { + ProjectConfig config = response.body(); + try { + ObjectMapper mapper = new ObjectMapper(); + localBucketing.storeConfig(environmentKey, mapper.writeValueAsString(config)); + } catch (JsonProcessingException e) { + if (this.config != null) { + System.out.printf("Unable to parse config with etag: %s. Using cache, etag %s%n", currentETag, this.configETag); + return this.config; + } else { + throw e; + } + } String currentETag = response.headers().get("ETag"); this.configETag = currentETag; return response.body();