fly-ruby 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/test.yml +27 -2
- data/CHANGELOG.md +11 -0
- data/README.md +1 -1
- data/lib/fly-ruby/configuration.rb +4 -0
- data/lib/fly-ruby/railtie.rb +25 -17
- data/lib/fly-ruby/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 79850f3de9e2a7e78ff9d46ba7728c605fae5651786d3a425dec0d12056d75b6
|
4
|
+
data.tar.gz: 5349dfbb16a8a257a909c68410f2041bf5202bbeed5ae88e496a9efa2e1c9478
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e43ad67e8d81f5d817ad274bb0f2e2a97d276fc1a9f0bb78549ea6cc06b5c2e5a90fd2ddd03f7529f592cf037bed68ed9f172945425829b7952acbdd4c20df58
|
7
|
+
data.tar.gz: d91c4fd6e1a5d3619165c9a44076840ac331c8533f0cb94f34001022d987bca1f7b654de34668cc6029305341f39bc7caa22b31ad76968802261fd884816b878
|
data/.github/workflows/test.yml
CHANGED
@@ -7,12 +7,35 @@ jobs:
|
|
7
7
|
strategy:
|
8
8
|
matrix:
|
9
9
|
include:
|
10
|
-
- { os: ubuntu-latest, ruby_version: 2.4 }
|
11
10
|
- { os: ubuntu-latest, ruby_version: 2.5 }
|
12
11
|
- { os: ubuntu-latest, ruby_version: 2.6 }
|
13
12
|
- { os: ubuntu-latest, ruby_version: 2.7 }
|
14
13
|
- { os: ubuntu-latest, ruby_version: '3.0' }
|
15
|
-
|
14
|
+
services:
|
15
|
+
# label used to access the service container
|
16
|
+
postgres:
|
17
|
+
# Docker Hub image
|
18
|
+
image: postgres:latest
|
19
|
+
# service environment variables
|
20
|
+
# `POSTGRES_HOST` is `postgres`
|
21
|
+
env:
|
22
|
+
# optional (defaults to `postgres`)
|
23
|
+
POSTGRES_DB: fly_ruby_test
|
24
|
+
# required
|
25
|
+
POSTGRES_PASSWORD: postgres_password
|
26
|
+
# optional (defaults to `5432`)
|
27
|
+
POSTGRES_PORT: 5432
|
28
|
+
# optional (defaults to `postgres`)
|
29
|
+
POSTGRES_USER: postgres_user
|
30
|
+
ports:
|
31
|
+
# maps tcp port 5432 on service container to the host
|
32
|
+
- 5432:5432
|
33
|
+
# set health checks to wait until postgres has started
|
34
|
+
options: >-
|
35
|
+
--health-cmd pg_isready
|
36
|
+
--health-interval 10s
|
37
|
+
--health-timeout 5s
|
38
|
+
--health-retries 5
|
16
39
|
steps:
|
17
40
|
- name: Setup Ruby, JRuby and TruffleRuby
|
18
41
|
uses: ruby/setup-ruby@v1.75.0
|
@@ -22,6 +45,8 @@ jobs:
|
|
22
45
|
- name: Checkout code
|
23
46
|
uses: actions/checkout@v2
|
24
47
|
- name: Run tests
|
48
|
+
env:
|
49
|
+
DATABASE_USER: postgres_user
|
25
50
|
run: |
|
26
51
|
bundle install --jobs 4 --retry 3
|
27
52
|
rake
|
data/CHANGELOG.md
ADDED
data/README.md
CHANGED
@@ -6,7 +6,7 @@ This gem contains helper code and Rack middleware for deploying Ruby web apps on
|
|
6
6
|
|
7
7
|
## Speed up apps using region-local database replicas
|
8
8
|
|
9
|
-
Fly's cross-region private networking makes it easy to run database replicas [alongside your app instances in multiple regions](https://fly.io/docs/getting-started/multi-region-databases/). These replicas can be used for faster reads
|
9
|
+
Fly's [cross-region private networking](https://fly.io/docs/reference/privatenetwork/) makes it easy to run database replicas [alongside your app instances in multiple regions](https://fly.io/docs/getting-started/multi-region-databases/). These replicas can be used for faster reads and application performance.
|
10
10
|
|
11
11
|
Writes, however, will be slow if performed across regions. Fly allows web apps to specify that a request be *replayed*, at the routing layer, in another region.
|
12
12
|
|
@@ -56,6 +56,10 @@ module Fly
|
|
56
56
|
database_url && primary_region && current_region && web?
|
57
57
|
end
|
58
58
|
|
59
|
+
def in_secondary_region?
|
60
|
+
primary_region && primary_region != current_region
|
61
|
+
end
|
62
|
+
|
59
63
|
# Is the current process a Rails console?
|
60
64
|
def console?
|
61
65
|
defined?(::Rails::Console) && $stdout.isatty && $stdin.isatty
|
data/lib/fly-ruby/railtie.rb
CHANGED
@@ -1,27 +1,35 @@
|
|
1
1
|
require_relative '../fly-ruby'
|
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.merge(Fly.configuration.regional_database_config))
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
# Set useful headers for debugging
|
17
|
+
def set_debug_response_headers
|
18
|
+
return unless defined?(ApplicationController)
|
19
|
+
|
20
|
+
ApplicationController.send(:after_action) do
|
21
|
+
response.headers['Fly-Region'] = ENV['FLY_REGION']
|
22
|
+
response.headers['Fly-Database-Host'] = Fly.configuration.regional_database_config["host"]
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
4
26
|
initializer("fly.regional_database") do |app|
|
27
|
+
set_debug_response_headers
|
5
28
|
if Fly.configuration.eligible_for_activation?
|
6
|
-
# Run the middleware high in the stack, but after static file delivery
|
7
29
|
app.config.middleware.insert_after ActionDispatch::Executor, Fly::RegionalDatabase
|
8
30
|
|
9
|
-
|
10
|
-
|
11
|
-
# we should reconnect to the region-local database. This may need some additional
|
12
|
-
# hooks for forking servers to work correctly.
|
13
|
-
if defined?(ActiveRecord) && ActiveRecord::Base.connected?
|
14
|
-
config = ActiveRecord::Base.connection_db_config.configuration_hash
|
15
|
-
ActiveRecord::Base.establish_connection(config.merge(Fly.configuration.regional_database_config))
|
16
|
-
end
|
17
|
-
|
18
|
-
# Set useful headers for debugging
|
19
|
-
::ApplicationController.send(:after_action) do
|
20
|
-
response.headers['Fly-Region'] = ENV['FLY_REGION']
|
21
|
-
response.headers['Fly-Database-Host'] = Fly.configuration.regional_database_config["host"]
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
31
|
+
# Run the middleware high in the stack, but after static file delivery
|
32
|
+
hijack_database_connection if Fly.configuration.in_secondary_region?
|
25
33
|
elsif Fly.configuration.web?
|
26
34
|
puts "Warning: DATABASE_URL, PRIMARY_REGION and FLY_REGION must be set to activate the fly-ruby middleware. Middleware not loaded."
|
27
35
|
end
|
data/lib/fly-ruby/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fly-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joshua Sierles
|
@@ -33,6 +33,7 @@ extra_rdoc_files: []
|
|
33
33
|
files:
|
34
34
|
- ".github/workflows/test.yml"
|
35
35
|
- ".gitignore"
|
36
|
+
- CHANGELOG.md
|
36
37
|
- Gemfile
|
37
38
|
- LICENSE
|
38
39
|
- README.md
|