routes_lazy_routes 0.1.0 → 0.4.0

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: 776e64fd7ac17ff3e62acfb3cafd7e908ed32de16d798ecda0f9e494abdca344
4
- data.tar.gz: 5c8811a1b1411849d27e2988f9a4d12db36a9fbbfd5c6676d383903678c85f44
3
+ metadata.gz: a8065fe0f170a4c5d2e2c2f9373ca5c3720769a077ed97ae13551968109d5169
4
+ data.tar.gz: 78b733748df2a374f040e638eb3e4dd49992c34dfe90571a3b22e27a210472c8
5
5
  SHA512:
6
- metadata.gz: 444b44d3d13ba5eff7a1c1b2960eba368295223506e460b48156eb7a5046e94a208adefabc855d48f560de5cef5151b0ea6d58fa027378b3ba9096a6bcaf5c12
7
- data.tar.gz: a3ae5366ea23776ac6aeabfcaf7b3116adfb47aa7c7ec2becf97a22b62ed856001fee1d2d2d1d8dcb591f584b4aa6553d641da1ecd7f0fdb5887dd27d4bccf9b
6
+ metadata.gz: 846e473fd9665a503acc56c83075952b759aa24f88711e057594b03854a56b1443f88553f3ed3323e25e1a6fd12282cc87eed768c5090cfec72400ad8699a92d
7
+ data.tar.gz: 8da7e3f095411d40337f3d3da1aae0e4350e2e1da3c83639b7371382104ed34225960cf595a07ce0096e7c8624e1cf1dc89fa887b29c86cb824e91366dbc4d0b
@@ -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: 3.0.0
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
@@ -5,6 +5,8 @@ source "https://rubygems.org"
5
5
  # Specify your gem's dependencies in routes_lazy_routes.gemspec
6
6
  gemspec
7
7
 
8
+ gem "rails", ENV["RAILS_VERSION"] if ENV["RAILS_VERSION"]
9
+
8
10
  gem "rake", "~> 13.0"
9
11
 
10
12
  gem "minitest", "~> 5.0"
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.
@@ -17,5 +17,12 @@ module RoutesLazyRoutes
17
17
  end
18
18
  end
19
19
  end
20
+
21
+ # The load runner
22
+ def eager_load!
23
+ if RoutesLazyRoutes::RoutesReloaderWrapper === (reloader = Rails.application.routes_reloader)
24
+ reloader.reload!
25
+ end
26
+ end
20
27
  end
21
28
  end
@@ -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, original_routes_reloader)
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
- @mutex.synchronize do
15
- @app.instance_variable_set :@routes_reloader, @original_routes_reloader
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
- initializer 'routes_lazy_routes', before: :add_routing_paths do
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
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ namespace :routes_lazy_routes do
4
+ task :eager_load do
5
+ RoutesLazyRoutes.eager_load!
6
+ end
7
+ end
8
+
9
+ Rake::Task['routes'].enhance ['routes_lazy_routes:eager_load']
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RoutesLazyRoutes
4
- VERSION = "0.1.0"
4
+ VERSION = '0.4.0'
5
5
  end
@@ -25,6 +25,7 @@ Gem::Specification.new do |spec|
25
25
  end
26
26
  spec.require_paths = ["lib"]
27
27
 
28
- spec.add_dependency 'rails'
28
+ spec.add_dependency 'railties'
29
+ spec.add_dependency 'actionpack'
29
30
  spec.add_development_dependency 'byebug'
30
31
  end
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.1.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: 2020-12-06 00:00:00.000000000 Z
11
+ date: 2021-03-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rails
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.2.0.rc.2
101
+ rubygems_version: 3.3.0.dev
85
102
  signing_key:
86
103
  specification_version: 4
87
104
  summary: A Rails routes lazy loader