routes_lazy_routes 0.1.0 → 0.4.0
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/main.yml +16 -1
- data/Gemfile +2 -0
- data/README.md +13 -2
- data/lib/routes_lazy_routes.rb +7 -0
- data/lib/routes_lazy_routes/application.rb +25 -0
- data/lib/routes_lazy_routes/command/routes_command.rb +14 -0
- data/lib/routes_lazy_routes/lazy_routes_middleware.rb +3 -8
- data/lib/routes_lazy_routes/railtie.rb +19 -1
- data/lib/routes_lazy_routes/routes_reloader_wrapper.rb +11 -3
- data/lib/routes_lazy_routes/tasks/routes_lazy_routes.rake +9 -0
- data/lib/routes_lazy_routes/version.rb +1 -1
- data/routes_lazy_routes.gemspec +2 -1
- metadata +21 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a8065fe0f170a4c5d2e2c2f9373ca5c3720769a077ed97ae13551968109d5169
|
4
|
+
data.tar.gz: 78b733748df2a374f040e638eb3e4dd49992c34dfe90571a3b22e27a210472c8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 846e473fd9665a503acc56c83075952b759aa24f88711e057594b03854a56b1443f88553f3ed3323e25e1a6fd12282cc87eed768c5090cfec72400ad8699a92d
|
7
|
+
data.tar.gz: 8da7e3f095411d40337f3d3da1aae0e4350e2e1da3c83639b7371382104ed34225960cf595a07ce0096e7c8624e1cf1dc89fa887b29c86cb824e91366dbc4d0b
|
data/.github/workflows/main.yml
CHANGED
@@ -4,15 +4,30 @@ on: [push,pull_request]
|
|
4
4
|
|
5
5
|
jobs:
|
6
6
|
build:
|
7
|
+
strategy:
|
8
|
+
matrix:
|
9
|
+
include:
|
10
|
+
- ruby-version: 2.7.2
|
11
|
+
rails-version: '~> 5.2.4'
|
12
|
+
- ruby-version: 2.7.2
|
13
|
+
rails-version: '~> 6.0.3'
|
14
|
+
- ruby-version: 2.7.2
|
15
|
+
rails-version: '~> 6.1.0'
|
16
|
+
- ruby-version: 3.0.0
|
17
|
+
rails-version: '~> 6.0.3'
|
18
|
+
- ruby-version: 3.0.0
|
19
|
+
rails-version: '~> 6.1.0'
|
7
20
|
runs-on: ubuntu-latest
|
8
21
|
steps:
|
9
22
|
- uses: actions/checkout@v2
|
10
23
|
- name: Set up Ruby
|
11
24
|
uses: ruby/setup-ruby@v1
|
12
25
|
with:
|
13
|
-
ruby-version:
|
26
|
+
ruby-version: ${{ matrix.ruby-version }}
|
14
27
|
- name: Run the default task
|
15
28
|
run: |
|
16
29
|
gem install bundler -v 2.2.0.rc.2
|
17
30
|
bundle install
|
18
31
|
bundle exec rake
|
32
|
+
env:
|
33
|
+
RAILS_VERSION: ${{ matrix.rails-version }}
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
routes_lazy_routes is an evil Rails plugin that defers loading the whole bloody routes until the server gets the first request, so the app can spin up quickly. 🤘
|
4
4
|
|
5
|
-
This voodoo gem is designed especially for you who are maintaining a Rails app that contains hundreds of routes that forces to wait dozens of seconds per every `rails` command invocation.
|
5
|
+
This voodoo gem is designed especially for you who are maintaining a huge legacy Rails app that contains hundreds of routes that forces you to wait dozens of seconds per every `rails` command invocation.
|
6
6
|
|
7
7
|
|
8
8
|
## Installation
|
@@ -30,10 +30,21 @@ You have nothing to do with it. It should just work beneath the skin.
|
|
30
30
|
|
31
31
|
## Trade-off
|
32
32
|
|
33
|
-
The first visitor of the server should sacrifice their time for the Rails process to load the routes.
|
33
|
+
The first visitor of the server should sacrifice their time for the Rails process to load the routes. First strike is deadly.
|
34
34
|
If you're bundling this in the production server, it'd be a good idea to throw a jab to the server right after the deployment in order to warm up before accepting real client requests.
|
35
35
|
|
36
36
|
|
37
|
+
## Notes
|
38
|
+
|
39
|
+
- You can manually eager_load the routes by calling `RoutesLazyRoutes.eager_load!` (the "load runner").
|
40
|
+
|
41
|
+
- `Rails.application.eager_load!` automatically invokes `RoutesLazyRoutes.eager_load!` since that should be what we expect for `Rails.application.eager_load!`.
|
42
|
+
|
43
|
+
- Loading an integration test automatically kicks `RoutesLazyRoutes.eager_load!` since AD::Integration expects the routes to be loaded.
|
44
|
+
|
45
|
+
- And, as already explained, sending a request to the Rails server automatically runs `RoutesLazyRoutes.eager_load!` on the server.
|
46
|
+
|
47
|
+
|
37
48
|
## Contributing
|
38
49
|
|
39
50
|
Patches are welcome on GitHub at https://github.com/amatsuda/routes_lazy_routes.
|
data/lib/routes_lazy_routes.rb
CHANGED
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RoutesLazyRoutes
|
4
|
+
module Application
|
5
|
+
module LoadRunner
|
6
|
+
# We expect `Rails.application.eager_load!` to load all routes as well
|
7
|
+
def eager_load!
|
8
|
+
RoutesLazyRoutes.eager_load!
|
9
|
+
|
10
|
+
super
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
module TaskLoader
|
15
|
+
# A monkey-patch that loads our Rake task for enhancing `rake routes` after Rails loads all other tasks.
|
16
|
+
# Just declaring our own `rake_tasks` in the railtie cannot achieve this, since calling each railtie's `rake_tasks` is done before requiring "rails/tasks",
|
17
|
+
# so enhancing Rails' Rake task from a gem this way seems impossible.
|
18
|
+
def load_tasks(*)
|
19
|
+
super
|
20
|
+
|
21
|
+
load "#{__dir__}/tasks/routes_lazy_routes.rake"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RoutesLazyRoutes
|
4
|
+
module Command
|
5
|
+
module RoutesCommand
|
6
|
+
# A monkey-patch that eager_loads the routes right before running the `rails routes` command.
|
7
|
+
def require_environment!
|
8
|
+
super
|
9
|
+
|
10
|
+
RoutesLazyRoutes.eager_load!
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -2,20 +2,15 @@
|
|
2
2
|
|
3
3
|
module RoutesLazyRoutes
|
4
4
|
class LazyRoutesMiddleware
|
5
|
-
def initialize(app
|
5
|
+
def initialize(app)
|
6
6
|
@app = app
|
7
|
-
@original_routes_reloader = original_routes_reloader
|
8
|
-
@mutex = Mutex.new
|
9
7
|
@loaded = false
|
10
8
|
end
|
11
9
|
|
12
10
|
def call(env)
|
13
11
|
unless @loaded
|
14
|
-
|
15
|
-
|
16
|
-
@original_routes_reloader.execute
|
17
|
-
@loaded = true
|
18
|
-
end
|
12
|
+
RoutesLazyRoutes.eager_load!
|
13
|
+
@loaded = true
|
19
14
|
end
|
20
15
|
|
21
16
|
@app.call env
|
@@ -1,9 +1,27 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require_relative 'application'
|
4
|
+
|
3
5
|
module RoutesLazyRoutes
|
4
6
|
class Railtie < ::Rails::Railtie
|
5
|
-
|
7
|
+
# Extending the following modules have to be done very early, like before executing any initializer, so here it is
|
8
|
+
Rails::Application.prepend RoutesLazyRoutes::Application::TaskLoader
|
9
|
+
|
10
|
+
if defined? Rails::Command::RoutesCommand
|
11
|
+
require_relative 'command/routes_command'
|
12
|
+
Rails::Command::RoutesCommand.prepend RoutesLazyRoutes::Command::RoutesCommand
|
13
|
+
end
|
14
|
+
|
15
|
+
initializer :routes_lazy_routes, before: :add_routing_paths do
|
6
16
|
RoutesLazyRoutes.arise!
|
17
|
+
|
18
|
+
Rails.application.config.middleware.use LazyRoutesMiddleware
|
19
|
+
|
20
|
+
Rails.application.extend RoutesLazyRoutes::Application::LoadRunner
|
21
|
+
|
22
|
+
ActiveSupport.on_load :action_dispatch_integration_test, run_once: true do
|
23
|
+
RoutesLazyRoutes.eager_load!
|
24
|
+
end
|
7
25
|
end
|
8
26
|
end
|
9
27
|
end
|
@@ -2,16 +2,24 @@
|
|
2
2
|
|
3
3
|
module RoutesLazyRoutes
|
4
4
|
class RoutesReloaderWrapper
|
5
|
-
delegate :paths, :eager_load=, :updated?, :route_sets, to: :@original_routes_reloader
|
5
|
+
delegate :paths, :eager_load=, :updated?, :route_sets, :external_routes, to: :@original_routes_reloader
|
6
6
|
|
7
7
|
def initialize(original_routes_reloader)
|
8
8
|
@original_routes_reloader = original_routes_reloader
|
9
|
-
|
10
|
-
Rails.application.config.middleware.use LazyRoutesMiddleware, original_routes_reloader
|
9
|
+
@mutex = Mutex.new
|
11
10
|
end
|
12
11
|
|
13
12
|
def execute
|
14
13
|
# pretty vacant
|
15
14
|
end
|
15
|
+
|
16
|
+
def reload!
|
17
|
+
@mutex.synchronize do
|
18
|
+
if Rails.application.routes_reloader == self
|
19
|
+
Rails.application.instance_variable_set :@routes_reloader, @original_routes_reloader
|
20
|
+
@original_routes_reloader.execute
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
16
24
|
end
|
17
25
|
end
|
data/routes_lazy_routes.gemspec
CHANGED
metadata
CHANGED
@@ -1,17 +1,31 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: routes_lazy_routes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Akira Matsuda
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-03-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: railties
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: actionpack
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
16
30
|
requirements:
|
17
31
|
- - ">="
|
@@ -55,9 +69,12 @@ files:
|
|
55
69
|
- bin/console
|
56
70
|
- bin/setup
|
57
71
|
- lib/routes_lazy_routes.rb
|
72
|
+
- lib/routes_lazy_routes/application.rb
|
73
|
+
- lib/routes_lazy_routes/command/routes_command.rb
|
58
74
|
- lib/routes_lazy_routes/lazy_routes_middleware.rb
|
59
75
|
- lib/routes_lazy_routes/railtie.rb
|
60
76
|
- lib/routes_lazy_routes/routes_reloader_wrapper.rb
|
77
|
+
- lib/routes_lazy_routes/tasks/routes_lazy_routes.rake
|
61
78
|
- lib/routes_lazy_routes/version.rb
|
62
79
|
- routes_lazy_routes.gemspec
|
63
80
|
homepage: https://github.com/amatsuda/routes_lazy_routes
|
@@ -81,7 +98,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
81
98
|
- !ruby/object:Gem::Version
|
82
99
|
version: '0'
|
83
100
|
requirements: []
|
84
|
-
rubygems_version: 3.
|
101
|
+
rubygems_version: 3.3.0.dev
|
85
102
|
signing_key:
|
86
103
|
specification_version: 4
|
87
104
|
summary: A Rails routes lazy loader
|