Skip to content

Commit

Permalink
Don't wrap into the around_check when #handle_result
Browse files Browse the repository at this point in the history
This caused us a bug on Drivy because we're running checker jobs through
a slave database. When sending an email, we're also writing in the
database which was causing an error.
  • Loading branch information
nicoolas25 committed Apr 3, 2018
1 parent 85d7891 commit 2e7db3e
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -6,3 +6,4 @@
/spec/reports/
/tmp/
.*.swp
*.gem
2 changes: 1 addition & 1 deletion Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
checker_jobs (0.1.0.pre)
checker_jobs (0.1.2.pre)

GEM
remote: https://rubygems.org/
Expand Down
4 changes: 2 additions & 2 deletions examples/user_checker.rb
Expand Up @@ -24,9 +24,9 @@
config.emails_backend = :action_mailer
config.emails_options = { from: "oss@drivy.com", reply_to: "no-reply@drivy.com" }

config.around_check = ->(check) {
config.around_check = -> {
puts "Starting check #{check.name}..."
check.perform
yield
puts "Done #{check.name}!"
}
end
Expand Down
6 changes: 4 additions & 2 deletions lib/checker_jobs/checks/base.rb
@@ -1,8 +1,10 @@
CheckerJobs::Checks::Base = Struct.new(:klass, :name, :options, :block) do
def perform
klass.new.instance_exec(&block).tap do |result|
handle_result(result)
result = CheckerJobs.configuration.around_check.call do
klass.new.instance_exec(&block)
end

result.tap { |res| handle_result(res) }
end

private
Expand Down
2 changes: 1 addition & 1 deletion lib/checker_jobs/configuration.rb
Expand Up @@ -17,7 +17,7 @@ def self.default
config.emails_options = {}
config.emails_formatter_class = CheckerJobs::EmailsBackends::DefaultFormatter
config.time_between_checks = DEFAULT_TIME_BETWEEN_CHECKS
config.around_check = ->(check) { check.perform }
config.around_check = ->(&block) { block.call }
end
end

Expand Down
3 changes: 1 addition & 2 deletions lib/checker_jobs/dsl.rb
Expand Up @@ -67,7 +67,6 @@ def add_check(klass, name, options, block)
end

def perform_check(check_name)
check = checks.fetch(check_name.to_s)
CheckerJobs.configuration.around_check.call(check)
checks.fetch(check_name.to_s).perform
end
end
2 changes: 1 addition & 1 deletion lib/checker_jobs/version.rb
@@ -1,3 +1,3 @@
module CheckerJobs
VERSION = "0.1.1.pre".freeze
VERSION = "0.1.2.pre".freeze
end
25 changes: 24 additions & 1 deletion spec/checker_jobs/checks/base_spec.rb
Expand Up @@ -8,7 +8,6 @@
end
end


describe "#perform" do
subject(:perform) { instance.perform }

Expand All @@ -17,5 +16,29 @@
it "executes the block in the context of a new klass's instance" do
is_expected.to eq 13 # "oss@drivy.com".size
end

context "when there is an around_check" do
before do
allow(CheckerJobs.configuration).
to receive(:around_check).
and_return(around_check)
end

let(:around_check) do
Proc.new do |&_block|
0 # Don't call the block
end
end

it "delegates the execution of the block to the around_check" do
is_expected.to be 0
end

it "keeps handling the result outside the around_check delegation" do
allow(instance).to receive(:handle_result).and_call_original
perform
expect(instance).to have_received(:handle_result).with(0)
end
end
end
end
4 changes: 2 additions & 2 deletions spec/checker_jobs/configuration_spec.rb
Expand Up @@ -36,8 +36,8 @@

let(:check) { instance_spy(CheckerJobs::Checks::Base) }

it "calls #perform on its argument when called" do
around_check.call(check)
it "calls #call its block argument when called" do
around_check.call { check.perform }
expect(check).to have_received(:perform)
end
end
Expand Down

0 comments on commit 2e7db3e

Please sign in to comment.