fly-ruby 0.4.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ea1ca64bcd11ed8a60774f0d8ec255ffb49bc6b8e21a8bd32e7410068bec35c0
4
- data.tar.gz: fb55ad6c98cb631bf202aea206c53a9b2e9a7c738cd9835095dd647011cfc7f6
3
+ metadata.gz: eca3058695c9bca72614139b04fe716c66e51391fe0c97174d7585655b6360fc
4
+ data.tar.gz: 7cc2f305188afd9f78f7bc40a59fc4669bf531ec9d48bf15fc324e1c1a0f46cd
5
5
  SHA512:
6
- metadata.gz: 7c3df04f8bb9f97bfb87903b0fffc14ef2b320802e1183752c07f8b769b629f6c66af21ae0e4bb5a23a2608cfdfdda2f3b1c387e3edcd1f64b5d2474a79f7a6f
7
- data.tar.gz: ff4ca7c18d4b18ec237c8e1c59bce88dce849198934ed1d42c28ab2a081b577f8fa2cc38930d0478ec94a5bee580e2f12649b38d3ad46d176a774edaba60446c
6
+ metadata.gz: 4c9786f36932df4f606b4944ba19ff658629490a69e86c10b8c4ea3c102235d8368780884f9e9cc436b079dcb683c34dd82007da59dfa6f70f7e6132fcff1f1c
7
+ data.tar.gz: db27454d933e0db41dd48c05b741eb3dc64a06bd0c688f9ccad08a879f616fa9d872e0a38923d4922d82e667a21c0ee19563cfac3ab47d4b9aa10ba3dc5b01bb
@@ -7,8 +7,6 @@ jobs:
7
7
  strategy:
8
8
  matrix:
9
9
  include:
10
- - { os: ubuntu-latest, ruby_version: 2.5 }
11
- - { os: ubuntu-latest, ruby_version: 2.6 }
12
10
  - { os: ubuntu-latest, ruby_version: 2.7 }
13
11
  - { os: ubuntu-latest, ruby_version: '3.0' }
14
12
  services:
data/Gemfile CHANGED
@@ -6,6 +6,7 @@ gem 'rack-test'
6
6
  gem 'minitest'
7
7
  gem "rails"
8
8
  gem "pg"
9
+ gem "sqlite3"
9
10
  gem "climate_control"
10
11
  gem "minitest-around"
11
12
  gem "m"
@@ -62,26 +62,39 @@ module Fly
62
62
 
63
63
  def database_uri
64
64
  @database_uri ||= URI.parse(database_url)
65
- @database_uri
66
65
  end
67
66
 
68
- def regional_database_url
67
+ def database_app_name
68
+ database_uri.hostname.split(".")[-2] || database_uri.hostname
69
+ end
70
+
71
+ def database_domain
72
+ "#{database_app_name}.internal"
73
+ end
74
+
75
+ def primary_database_url
69
76
  uri = database_uri.dup
70
- uri.host = regional_database_host
77
+ uri.host = "#{primary_region}.#{database_domain}"
78
+ uri.port = secondary_database_port
71
79
  uri.to_s
72
80
  end
73
81
 
74
- def regional_database_host
75
- "#{current_region}.#{database_uri.hostname}"
82
+ def secondary_database_url
83
+ uri = database_uri.dup
84
+ uri.host = "top1.nearest.of.#{database_domain}"
85
+ uri.port = secondary_database_port
86
+ uri.to_s
76
87
  end
77
88
 
78
- # Rails-compatible database configuration
79
- def regional_database_config
80
- {
81
- :host => regional_database_host,
82
- :port => 5433,
83
- :adapter => "postgresql"
84
- }
89
+ def secondary_database_port
90
+ port = if in_secondary_region?
91
+ case database_uri.scheme
92
+ when "postgres"
93
+ 5433
94
+ end
95
+ end
96
+
97
+ port || database_uri.port
85
98
  end
86
99
 
87
100
  def redis_uri
@@ -100,7 +113,13 @@ module Fly
100
113
  end
101
114
 
102
115
  def eligible_for_activation?
103
- database_url && primary_region && current_region && web?
116
+ database_url && primary_region && current_region && !rake_task?
117
+ end
118
+
119
+ def hijack_database_connection!
120
+ # Don't reset the database URL for on-disk sqlite
121
+ return if database_uri.scheme.start_with?("sqlite") || database_uri.host !~ /(internal|localhost)/
122
+ ENV["DATABASE_URL"] = in_secondary_region? ? secondary_database_url : primary_database_url
104
123
  end
105
124
 
106
125
  def in_secondary_region?
@@ -1,30 +1,18 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Fly::Railtie < Rails::Railtie
4
- def hijack_database_connection
5
- ActiveSupport::Reloader.to_prepare do
6
- # If we already have a database connection when this initializer runs,
7
- # we should reconnect to the region-local database. This may need some additional
8
- # hooks for forking servers to work correctly.
9
- if defined?(ActiveRecord)
10
- config = ActiveRecord::Base.connection_db_config.configuration_hash
11
- ActiveRecord::Base.establish_connection(config.symbolize_keys.merge(Fly.configuration.regional_database_config))
12
- end
13
- end
14
- end
15
-
16
- initializer("fly.regional_database") do |app|
4
+ initializer("fly.regional_database", before: "active_record.initialize_database") do |app|
17
5
  # Insert the request middleware high in the stack, but after static file delivery
18
- app.config.middleware.insert_after ActionDispatch::Executor, Fly::Headers if Fly.configuration.web?
6
+ app.config.middleware.insert_after ActionDispatch::Executor, Fly::Headers
19
7
 
20
8
  if Fly.configuration.eligible_for_activation?
9
+
10
+ Fly.configuration.hijack_database_connection!
11
+
21
12
  app.config.middleware.insert_after Fly::Headers, Fly::RegionalDatabase::ReplayableRequestMiddleware
22
13
  # Insert the database exception handler at the bottom of the stack to take priority over other exception handlers
23
14
  app.config.middleware.use Fly::RegionalDatabase::DbExceptionHandlerMiddleware
24
15
 
25
- if Fly.configuration.in_secondary_region?
26
- hijack_database_connection
27
- end
28
16
  elsif Fly.configuration.web?
29
17
  puts "Warning: DATABASE_URL, PRIMARY_REGION and FLY_REGION must be set to activate the fly-ruby middleware. Middleware not loaded."
30
18
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Fly
4
- VERSION = "0.4.0"
4
+ VERSION = "0.5.1"
5
5
  end
data/lib/fly-ruby.rb CHANGED
@@ -22,16 +22,14 @@ module Fly
22
22
  end
23
23
 
24
24
  class Instance
25
- attr_accessor :configuration
25
+ attr_writer :configuration
26
26
 
27
- def initialize
28
- self.configuration = Fly::Configuration.new
27
+ def configuration
28
+ @configuration ||= Fly::Configuration.new
29
29
  end
30
30
 
31
- def configure
32
- configuration = Fly::Configuration.new
33
- yield(configuration) if block_given?
34
- self.configuration = configuration
31
+ def configure(&block)
32
+ configuration.tap(&block)
35
33
  end
36
34
  end
37
35
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fly-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joshua Sierles
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-08-19 00:00:00.000000000 Z
11
+ date: 2022-08-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack