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 +4 -4
- data/.github/workflows/test.yml +0 -2
- data/Gemfile +1 -0
- data/lib/fly-ruby/configuration.rb +32 -13
- data/lib/fly-ruby/railtie.rb +5 -17
- data/lib/fly-ruby/version.rb +1 -1
- data/lib/fly-ruby.rb +5 -7
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eca3058695c9bca72614139b04fe716c66e51391fe0c97174d7585655b6360fc
|
4
|
+
data.tar.gz: 7cc2f305188afd9f78f7bc40a59fc4669bf531ec9d48bf15fc324e1c1a0f46cd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4c9786f36932df4f606b4944ba19ff658629490a69e86c10b8c4ea3c102235d8368780884f9e9cc436b079dcb683c34dd82007da59dfa6f70f7e6132fcff1f1c
|
7
|
+
data.tar.gz: db27454d933e0db41dd48c05b741eb3dc64a06bd0c688f9ccad08a879f616fa9d872e0a38923d4922d82e667a21c0ee19563cfac3ab47d4b9aa10ba3dc5b01bb
|
data/.github/workflows/test.yml
CHANGED
data/Gemfile
CHANGED
@@ -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
|
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 =
|
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
|
75
|
-
|
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
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
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 &&
|
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?
|
data/lib/fly-ruby/railtie.rb
CHANGED
@@ -1,30 +1,18 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
class Fly::Railtie < Rails::Railtie
|
4
|
-
|
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
|
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
|
data/lib/fly-ruby/version.rb
CHANGED
data/lib/fly-ruby.rb
CHANGED
@@ -22,16 +22,14 @@ module Fly
|
|
22
22
|
end
|
23
23
|
|
24
24
|
class Instance
|
25
|
-
|
25
|
+
attr_writer :configuration
|
26
26
|
|
27
|
-
def
|
28
|
-
|
27
|
+
def configuration
|
28
|
+
@configuration ||= Fly::Configuration.new
|
29
29
|
end
|
30
30
|
|
31
|
-
def configure
|
32
|
-
configuration
|
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
|
+
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-
|
11
|
+
date: 2022-08-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|