dynamic-configuration


License

License

Categories

Categories

Configuration Application Layer Libs config
GroupId

GroupId

ai.faculty
ArtifactId

ArtifactId

dynamic-configuration_2.11
Last Version

Last Version

0.3.3
Release Date

Release Date

Type

Type

jar
Description

Description

dynamic-configuration
dynamic-configuration
Project URL

Project URL

https://github.com/facultyai/dynamic-configuration
Project Organization

Project Organization

ai.faculty
Source Code Management

Source Code Management

https://github.com/facultyai/dynamic-configuration

Download dynamic-configuration_2.11

How to add to project

<!-- https://jarcasting.com/artifacts/ai.faculty/dynamic-configuration_2.11/ -->
<dependency>
    <groupId>ai.faculty</groupId>
    <artifactId>dynamic-configuration_2.11</artifactId>
    <version>0.3.3</version>
</dependency>
// https://jarcasting.com/artifacts/ai.faculty/dynamic-configuration_2.11/
implementation 'ai.faculty:dynamic-configuration_2.11:0.3.3'
// https://jarcasting.com/artifacts/ai.faculty/dynamic-configuration_2.11/
implementation ("ai.faculty:dynamic-configuration_2.11:0.3.3")
'ai.faculty:dynamic-configuration_2.11:jar:0.3.3'
<dependency org="ai.faculty" name="dynamic-configuration_2.11" rev="0.3.3">
  <artifact name="dynamic-configuration_2.11" type="jar" />
</dependency>
@Grapes(
@Grab(group='ai.faculty', module='dynamic-configuration_2.11', version='0.3.3')
)
libraryDependencies += "ai.faculty" % "dynamic-configuration_2.11" % "0.3.3"
[ai.faculty/dynamic-configuration_2.11 "0.3.3"]

Dependencies

compile (4)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.11.12
com.typesafe.akka : akka-actor_2.11 jar 2.5.16
com.typesafe.play : play-ahc-ws-standalone_2.11 jar 1.1.2
com.amazonaws : aws-java-sdk-s3 jar 1.11.408

test (3)

Group / Artifact Type Version
com.typesafe.akka : akka-testkit_2.11 jar 2.5.14
org.mockito : mockito-core jar 2.22.0
org.scalatest : scalatest_2.11 jar 3.0.5

Project Modules

There are no modules declared in this project.

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 the local file system, on Amazon S3, or on a web server that speaks HTTP. 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": "faculty-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 DynamicConfiguration.fromS3, 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].

DynamicConfiguration.fromS3 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 scala.concurrent.duration._
import scala.util.Try

import com.amazonaws.regions.Regions
import com.amazonaws.services.s3.AmazonS3ClientBuilder
import ai.faculty.configuration.{DynamicConfiguration, RefreshOptions}
import org.json4s._

final 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 =
    DynamicConfiguration.fromS3[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("Configuration not ready")
    }
}

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

ai.faculty

Faculty

Artificial Intelligence made real

Versions

Version
0.3.3
0.3.2