class Fly::Configuration

Attributes

current_region[RW]

Set the region where this instance of the application is deployed

database_host_env_var[RW]
database_port_env_var[RW]
database_url[RW]
database_url_env_var[RW]

Environment variables related to the database connection. These get by this middleware in secondary regions, so they must be interpolated rather than defined directly in the configuration.

primary_region[RW]

Set the region where the primary database lives, i.e “ams”

redis_url[RW]
redis_url_env_var[RW]
replay_http_methods[RW]

Automatically replay these HTTP methods in the primary region

replay_threshold_in_seconds[RW]

How long, in seconds, should all requests from the same client be replayed in the primary region after a successful write replay

Public Class Methods

new() click to toggle source
# File lib/fly-ruby/configuration.rb, line 31
def initialize
  self.primary_region = ENV["PRIMARY_REGION"]
  self.current_region = ENV["FLY_REGION"]
  self.replay_http_methods = ["POST", "PUT", "PATCH", "DELETE"]
  self.database_url_env_var = "DATABASE_URL"
  self.redis_url_env_var = "REDIS_URL"
  self.database_host_env_var = "DATABASE_HOST"
  self.database_port_env_var = "DATABASE_PORT"
  self.replay_threshold_cookie = "fly-replay-threshold"
  self.replay_threshold_in_seconds = 5
  self.database_url = ENV[database_url_env_var]
  self.redis_url = ENV[redis_url_env_var]
end

Public Instance Methods

console?() click to toggle source

Is the current process a Rails console?

# File lib/fly-ruby/configuration.rb, line 93
def console?
  defined?(::Rails::Console) && $stdout.isatty && $stdin.isatty
end
database_uri() click to toggle source
# File lib/fly-ruby/configuration.rb, line 45
def database_uri
  @database_uri ||= URI.parse(database_url)
  @database_uri
end
eligible_for_activation?() click to toggle source
# File lib/fly-ruby/configuration.rb, line 84
def eligible_for_activation?
  database_url && primary_region && current_region && web?
end
in_secondary_region?() click to toggle source
# File lib/fly-ruby/configuration.rb, line 88
def in_secondary_region?
  primary_region && primary_region != current_region
end
rake_task?() click to toggle source

Is the current process a rake task?

# File lib/fly-ruby/configuration.rb, line 98
def rake_task?
  defined?(::Rake) && !Rake.application.top_level_tasks.empty?
end
redis_uri() click to toggle source
# File lib/fly-ruby/configuration.rb, line 69
def redis_uri
  @redis_uri ||= URI.parse(redis_url)
  @redis_uri
end
regional_database_config() click to toggle source

Rails-compatible database configuration

# File lib/fly-ruby/configuration.rb, line 61
def regional_database_config
  {
    "host" => regional_database_host,
    "port" => 5433,
    "adapter" => "postgresql"
  }
end
regional_database_host() click to toggle source
# File lib/fly-ruby/configuration.rb, line 56
def regional_database_host
  "#{current_region}.#{database_uri.hostname}"
end
regional_database_url() click to toggle source
# File lib/fly-ruby/configuration.rb, line 50
def regional_database_url
  uri = database_uri.dup
  uri.host = regional_database_host
  uri.to_s
end
regional_redis_host() click to toggle source
# File lib/fly-ruby/configuration.rb, line 74
def regional_redis_host
  "#{current_region}.#{redis_uri.hostname}"
end
regional_redis_url() click to toggle source
# File lib/fly-ruby/configuration.rb, line 78
def regional_redis_url
  uri = redis_uri.dup
  uri.host = regional_redis_host
  uri.to_s
end
web?() click to toggle source
# File lib/fly-ruby/configuration.rb, line 102
def web?
  !console? && !rake_task?
end