Skip to content

Instantly share code, notes, and snippets.

@leonelgalan
Created November 11, 2016 20:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leonelgalan/9064695727b1b06c6eb28b0834a96fb0 to your computer and use it in GitHub Desktop.
Save leonelgalan/9064695727b1b06c6eb28b0834a96fb0 to your computer and use it in GitHub Desktop.
Commit shown on New Relic charts
From 80cb3085f74e18ca983495349320661084780383 Mon Sep 17 00:00:00 2001
From: Leonel Galan <leonelgalan@gmail.com>
Date: Fri, 11 Nov 2016 14:56:14 -0500
Subject: [PATCH] Configure octopus
---
Gemfile | 1 +
Gemfile.lock | 4 ++++
config/initializers/octopus.rb | 42 ++++++++++++++++++++++++++++++++++++++++++
3 files changed, 47 insertions(+)
create mode 100644 config/initializers/octopus.rb
diff --git a/Gemfile b/Gemfile
index 9590bce..f0e51af 100644
--- a/Gemfile
+++ b/Gemfile
@@ -1,6 +1,7 @@
source 'https://rubygems.org'
ruby '2.2.4'
+gem 'ar-octopus'
gem 'rubyzip'
gem 'descriptive_statistics'
gem 'colorize'
diff --git a/Gemfile.lock b/Gemfile.lock
index 0be24c3..6b83aba 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -74,6 +74,9 @@ GEM
annotate (2.7.1)
activerecord (>= 3.2, < 6.0)
rake (>= 10.4, < 12.0)
+ ar-octopus (0.8.6)
+ activerecord (>= 3.2.0)
+ activesupport (>= 3.2.0)
arbre (1.1.1)
activesupport (>= 3.0.0)
arel (6.0.3)
@@ -550,6 +553,7 @@ DEPENDENCIES
activeadmin!
acts_as_list
annotate
+ ar-octopus
audited-activerecord (~> 4.0)
aws-sdk
aws-sdk-v1
diff --git a/config/initializers/octopus.rb b/config/initializers/octopus.rb
new file mode 100644
index 0000000..f42d765
--- /dev/null
+++ b/config/initializers/octopus.rb
@@ -0,0 +1,42 @@
+if Rails.env.production?
+ # Loosely based on https://gist.github.com/eprothro/5374472 and
+ # https://github.com/thiagopradi/octopus/wiki/Dynamic-shard-configuration
+ master_url = ENV['DATABASE_URL']
+ slave_keys = ENV.keys.select{ |key| key =~ /HEROKU_POSTGRESQL_.*_URL/ }
+ slave_keys.delete_if { |key| ENV[key] == master_url }
+
+ shards = slave_keys.each_with_object({}) do |key, memo|
+ uri = URI.parse(ENV[key])
+ color = key.match(/HEROKU_POSTGRESQL_(.*)_URL/)[1].downcase
+ adapter = uri.scheme
+ adapter = 'postgresql' if adapter == 'postgres'
+
+ memo[color.to_sym] = {
+ adapter: adapter,
+ database: (uri.path || '').split('/')[1],
+ username: uri.user,
+ password: uri.password,
+ host: uri.host,
+ port: uri.port,
+ params: CGI.parse(uri.query || '')
+ }
+ memo
+ end
+
+ whitelist = ENV.fetch('SLAVE_ENABLED_FOLLOWERS', '').downcase.split(',').map(&:strip)
+ blacklist = ENV.fetch('SLAVE_DISABLED_FOLLOWERS', '').downcase.split(',').map(&:strip)
+ shards.delete_if do |color, _value|
+ color_as_s = color.to_s
+ !whitelist.empty? && !whitelist.include?(color_as_s) || blacklist.include?(color_as_s)
+ end
+
+ Octopus.setup do |config|
+ config.environments = [:production]
+ config.shards = shards
+ end
+else
+ # Octopus.setup do |config|
+ # config.environments = [:development, :staging]
+ # config.shards = {}
+ # end
+end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment