All Downloads are FREE. Search and download functionalities are using the official Maven repository.

deployment.DeploymentRunner.kt Maven / Gradle / Ivy

There is a newer version: 0.5.1
Show newest version
package com.amplitude.deployment

import com.amplitude.Configuration
import com.amplitude.cohort.CohortLoader
import com.amplitude.util.getAllCohortIds
import com.amplitude.util.logger
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancelAndJoin
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

internal class DeploymentRunner(
    @Volatile var configuration: Configuration,
    private val deploymentKey: String,
    private val deploymentApi: DeploymentApi,
    private val deploymentStorage: DeploymentStorage,
    private val cohortLoader: CohortLoader
) {

    companion object {
        val log by logger()
    }

    private val supervisor = SupervisorJob()
    private val scope = CoroutineScope(supervisor)
    private val deploymentLoader = DeploymentLoader(deploymentApi, deploymentStorage, cohortLoader)

    suspend fun start() {
        log.trace("start: - deploymentKey=$deploymentKey")
        deploymentLoader.loadDeployment(deploymentKey)
        // Periodic flag config loader
        scope.launch {
            while (true) {
                delay(configuration.flagSyncIntervalMillis)
                deploymentLoader.loadDeployment(deploymentKey)
            }
        }
        // Periodic cohort refresher
        scope.launch {
            while (true) {
                delay(configuration.cohortSyncIntervalMillis)
                val cohortIds = deploymentStorage.getAllFlags(deploymentKey).values.getAllCohortIds()
                cohortLoader.loadCohorts(cohortIds)
            }
        }
    }

    suspend fun stop() {
        log.debug("stop: - deploymentKey=$deploymentKey")
        supervisor.cancelAndJoin()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy