Skip to content

Commit

Permalink
Fix how Rake tasks are detected
Browse files Browse the repository at this point in the history
`Rake.application` might be callable, but if it's empty, we're not in a Rake task.s
  • Loading branch information
adamlogic committed Aug 9, 2023
1 parent 0cc3b57 commit dcdd3c0
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 8 deletions.
2 changes: 1 addition & 1 deletion judoscale-rails/lib/judoscale/rails/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def in_rails_console?
end

def in_rake_task?
defined?(::Rake) && Rake.respond_to?(:application)
defined?(::Rake) && Rake.respond_to?(:application) && Rake.application.top_level_tasks.any?
end

initializer "Judoscale.logger" do |app|
Expand Down
2 changes: 1 addition & 1 deletion judoscale-ruby/lib/judoscale/metrics_collector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Judoscale
class MetricsCollector
def self.collect?(config)
in_rake_task = defined?(::Rake) && Rake.respond_to?(:application)
in_rake_task = defined?(::Rake) && Rake.respond_to?(:application) && Rake.application.top_level_tasks.any?

!in_rake_task || in_whitelisted_rake_tasks?(config.allow_rake_tasks)
end
Expand Down
6 changes: 5 additions & 1 deletion judoscale-ruby/test/job_metrics_collector_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ module Judoscale
Object.stub_const :Rake, nil do
_(WebMetricsCollector.collect?(Config.instance)).must_equal true
end

Object.stub_const :Rake, RakeMock.new([]) do
_(WebMetricsCollector.collect?(Config.instance)).must_equal true
end
end

it "returns false when running in a rake task" do
Object.stub_const :Rake, RakeMock.new do
Object.stub_const :Rake, RakeMock.new(["foo"]) do
_(WebMetricsCollector.collect?(Config.instance)).must_equal false
end
end
Expand Down
2 changes: 1 addition & 1 deletion judoscale-ruby/test/rake_mock.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class RakeMock
def initialize(top_level_tasks = [])
def initialize(top_level_tasks)
@top_level_tasks = top_level_tasks
end

Expand Down
6 changes: 5 additions & 1 deletion judoscale-ruby/test/web_metrics_collector_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ module Judoscale
Object.stub_const :Rake, nil do
_(WebMetricsCollector.collect?(Config.instance)).must_equal true
end

Object.stub_const :Rake, RakeMock.new([]) do
_(WebMetricsCollector.collect?(Config.instance)).must_equal true
end
end

it "returns false when running in a rake task" do
Object.stub_const :Rake, RakeMock.new do
Object.stub_const :Rake, RakeMock.new(["foo"]) do
_(WebMetricsCollector.collect?(Config.instance)).must_equal false
end
end
Expand Down
7 changes: 4 additions & 3 deletions sample-apps/rails-sample/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
PATH
remote: ../../judoscale-rails
specs:
judoscale-rails (1.3.0)
judoscale-ruby (= 1.3.0)
judoscale-rails (1.5.0)
judoscale-ruby (= 1.5.0)
railties

PATH
remote: ../../judoscale-ruby
specs:
judoscale-ruby (1.3.0)
judoscale-ruby (1.5.0)

GEM
remote: https://rubygems.org/
Expand Down Expand Up @@ -76,6 +76,7 @@ GEM
PLATFORMS
arm64-darwin-20
arm64-darwin-21
arm64-darwin-22
x86_64-darwin-21
x86_64-linux

Expand Down

0 comments on commit dcdd3c0

Please sign in to comment.