Skip to content

Commit

Permalink
Update Instance.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
hsinha610 authored Jul 30, 2024
1 parent c216636 commit 509ce2d
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions src/main/kotlin/com/featurevisor/sdk/Instance.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package com.featurevisor.sdk
import com.featurevisor.sdk.FeaturevisorError.MissingDatafileOptions
import com.featurevisor.types.*
import com.featurevisor.types.EventName.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
Expand All @@ -16,7 +18,7 @@ typealias InterceptContext = (Context) -> Context
typealias DatafileFetchHandler = (datafileUrl: String) -> Result<DatafileContent>

var emptyDatafile = DatafileContent(
schemaVersion = "1",
schemaVersion = "1",
revision = "unknown",
attributes = emptyList(),
segments = emptyList(),
Expand Down Expand Up @@ -56,6 +58,8 @@ class FeaturevisorInstance private constructor(options: InstanceOptions) {
internal var configureBucketKey = options.configureBucketKey
internal var configureBucketValue = options.configureBucketValue
internal var refreshJob: Job? = null
private var fetchJob: Job? = null
internal val coroutineScope = CoroutineScope(Dispatchers.IO)

init {
with(options) {
Expand Down Expand Up @@ -99,17 +103,26 @@ class FeaturevisorInstance private constructor(options: InstanceOptions) {
}

datafileUrl != null -> {
datafileReader = DatafileReader(options.datafile?: emptyDatafile)
fetchDatafileContent(datafileUrl, handleDatafileFetch) { result ->
if (result.isSuccess) {
datafileReader = DatafileReader(result.getOrThrow())
datafileReader = DatafileReader(options.datafile ?: emptyDatafile)
fetchJob = fetchDatafileContentJob(
url = datafileUrl,
logger = logger,
handleDatafileFetch = handleDatafileFetch,
retryCount = retryCount.coerceAtLeast(1),
retryInterval = retryInterval.coerceAtLeast(0),
coroutineScope = coroutineScope,
) { result ->
result.onSuccess { fetchResult ->
val datafileContent = fetchResult.datafileContent
datafileReader = DatafileReader(datafileContent)
statuses.ready = true
emitter.emit(READY, result.getOrThrow())
emitter.emit(READY, datafileContent, fetchResult.responseBodyString)
if (refreshInterval != null) startRefreshing()
} else {
logger?.error("Failed to fetch datafile: $result")
}.onFailure { error ->
logger?.error("Failed to fetch datafile: $error")
emitter.emit(ERROR)
}
cancelFetchRetry()
}
}

Expand All @@ -118,6 +131,12 @@ class FeaturevisorInstance private constructor(options: InstanceOptions) {
}
}

// Provide a mechanism to cancel the fetch job if retry count is more than one
fun cancelFetchRetry() {
fetchJob?.cancel()
fetchJob = null
}

fun setLogLevels(levels: List<Logger.LogLevel>) {
this.logger?.setLevels(levels)
}
Expand All @@ -126,14 +145,14 @@ class FeaturevisorInstance private constructor(options: InstanceOptions) {
val data = datafileJSON.toByteArray(Charsets.UTF_8)
try {
val datafileContent = Json.decodeFromString<DatafileContent>(String(data))
datafileReader = DatafileReader(datafileJson = datafileContent)
datafileReader = DatafileReader(datafileContent = datafileContent)
} catch (e: Exception) {
logger?.error("could not parse datafile", mapOf("error" to e))
}
}

fun setDatafile(datafileContent: DatafileContent) {
datafileReader = DatafileReader(datafileJson = datafileContent)
datafileReader = DatafileReader(datafileContent = datafileContent)
}

fun setStickyFeatures(stickyFeatures: StickyFeatures?) {
Expand Down

0 comments on commit 509ce2d

Please sign in to comment.