Git Product home page Git Product logo

dynamic-configuration's Introduction

Dynamic configuration tools

Build Status

This repository provides tools for setting up configuration that refreshes at particular intervals. It assumes that the current configuration lives in a file on Amazon S3. It tries to refresh the configuration at regular intervals.

Assume that, for instance, you want to create a class to frozzle some widgets. This class needs access to the current model of the widget to be frozzled. To avoid hard-coding the model in your code, you decide to keep a reference to the model in a file on S3. You want to be able to update that file and have your widget frozzler automatically pick up the changes.

Let's assume that your configuration is formatted as JSON:

{
  "widget-model": "ASI-1292"
}

To load and automatically refresh the configuration from S3, create a case class that represents your configuration (e.g. FrozzlerConfiguration in the example below). Then, call DynamicConfigurationFromS3, passing in the bucket and key at which your configuration file is located and a method for converting from the string content of your configuration to a Try[FrozzlerConfiguration].

DynamicConfigurationFromS3 will return a DynamicConfiguration object with a currentConfiguration method. This returns an option with either the current configuration, or None if the configuration is not loaded yet.

import com.asidatascience.configuration.{DynamicConfigurationFromS3, RefreshOptions}

import scala.util.Try
import org.json4s._

import scala.concurrent.duration._

import com.amazonaws.regions.Regions
import com.amazonaws.services.s3.AmazonS3ClientBuilder

case class FrozzlerConfiguration(model: String)

class WidgetFrozzler(
  configurationS3Bucket: String,
  configurationS3Key: String
) {

  implicit val actorSystem = ActorSystem()

  private def parseConfiguration(content: String) = {
    // parse the contents of the configuration file
    val contentAsJson = JsonMethods.parse(content)
    val JString(model) = (contentAsJson \ "widget-model")
    FrozzlerConfiguration(model)
  }

  val refreshOptions = RefreshOptions(
    initialDelay = 0.millis,
    updateInterval = 5.seconds
  )

  val s3Client = AmazonS3ClientBuilder
    .standard().withRegion(Regions.EU_WEST_1).build

  lazy val configurationService = DynamicConfigurationFromS3[FrozzlerConfiguration](
    s3Client,
    configurationS3Bucket,
    configurationS3Key,
    refreshOptions
  ){ contents => Try { parseConfiguration(contents) } }

  def frozzleWidgets = {
    configurationService.currentConfiguration match {
      case Some(configuration) =>
        val currentModel = configuration.model
        println(s"Creating widget with model $currentModel")
      case None =>
        println(s"Configuration not ready")
    }
  }
}

This is turned into a fully functional example in the /examples/simple directory.

dynamic-configuration's People

Contributors

acroz avatar pbugnion avatar srstevenson avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.