A platform for community discussion. Free, open, simple.
DEV: Follow-up to the `lock` step for services
Loïc Guitautf057c71fc874
diff --git a/lib/service/base.rb b/lib/service/base.rbindex 7d12a460efa..0721e934adb 100644--- a/lib/service/base.rb+++ b/lib/service/base.rb@@ -265,10 +265,11 @@ module Serviceclass LockStep < Stepinclude StepsHelpers- attr_reader :steps+ attr_reader :steps, :keysdef initialize(*keys, &block)@keys = keys+ @name = keys.join(":")@steps = []instance_exec(&block)end@@ -290,10 +291,12 @@ module Serviceendend+ private+def lock_name[context.__service_class__.to_s.underscore,- *@keys.flat_map { |key| [key, context[:params].send(key)] },+ *keys.flat_map { |key| [key, context[:params].public_send(key)] },].join(":")endenddiff --git a/lib/service/runner.rb b/lib/service/runner.rbindex 18d73070fa2..d61254a64a9 100644--- a/lib/service/runner.rb+++ b/lib/service/runner.rb@@ -25,6 +25,8 @@# * +on_exceptions(*exceptions)+: will execute the provided block if any# exceptions were caught by the `try` block. One or more exception classes# can be provided to specifically handle those exceptions.+# * +on_lock_not_acquired(*keys)+: will execute the provided block if the lock+# using `keys` wasn’t acquired successfully.## All the specialized steps receive the failing step result object as an# argument to their block. `on_model_errors` receives the actual model so it’s@@ -105,6 +107,10 @@ class Service::Runnername: "default",property: :exception,},+ on_lock_not_acquired: {+ condition: ->(*keys) { failure_for?("result.lock.#{keys.join(":")}") },+ key: [],+ },}.with_indifferent_access.freeze# @!visibility privatediff --git a/lib/service/steps_inspector.rb b/lib/service/steps_inspector.rbindex 5ab0c804e33..0a062e952ca 100644--- a/lib/service/steps_inspector.rb+++ b/lib/service/steps_inspector.rb@@ -123,6 +123,17 @@ class Service::StepsInspectorendend+ # @!visibility private+ class Lock < Transaction+ def inspect+ "#{" " * nesting_level}[#{inspect_type}] #{name}#{runtime} #{emoji}".rstrip+ end++ def error+ "Lock '#{name}' was not acquired."+ end+ end+attr_reader :steps, :resultdef initialize(result)diff --git a/spec/lib/service/runner_spec.rb b/spec/lib/service/runner_spec.rbindex 44e82988f25..ca76f3225ad 100644--- a/spec/lib/service/runner_spec.rb+++ b/spec/lib/service/runner_spec.rb@@ -487,6 +487,31 @@ RSpec.describe Service::Runner doendend+ context "when using the on_lock_not_acquired action" do+ let(:service) { LockService }+ let(:dependencies) { { params: { post_id: 123, user_id: 456 } } }+ let(:actions) { <<-BLOCK }+ proc do+ on_success { :success }+ on_lock_not_acquired(:post_id, :user_id) { :lock_not_acquired }+ end+ BLOCK++ context "when the service fails" do+ before { allow(DistributedMutex).to receive(:synchronize) }++ it "runs the provided block" do+ expect(runner).to eq :lock_not_acquired+ end+ end++ context "when the service does not fail" do+ it "does not run the provided block" do+ expect(runner).to eq :success+ end+ end+ end+context "when using several actions together" dolet(:service) { FailureService }let(:actions) { <<-BLOCK }@@ -515,36 +540,5 @@ RSpec.describe Service::Runner doexpect(runner).to eq :successendend-- context "when aquiring a lock" do- let(:service) { LockService }- let(:dependencies) { { params: { post_id: 123, user_id: 456 } } }- let(:actions) { <<-BLOCK }- proc do- on_success { :success }- on_failure { :failure }- end- BLOCK-- it "runs successfully" do- expect(runner).to eq :success- end- end-- context "when failing to acquire a lock" do- let(:service) { LockService }- let(:dependencies) { { params: { post_id: 123, user_id: 456 } } }- let(:actions) { <<-BLOCK }- proc do- on_success { :success }- on_failure { :failure }- end- BLOCK-- it "fails the service" do- DistributedMutex.stubs(:synchronize).returns- expect(runner).to eq :failure- end- endendenddiff --git a/spec/lib/service/steps_inspector_spec.rb b/spec/lib/service/steps_inspector_spec.rbindex 1bc577562bd..6da91ac3ac2 100644--- a/spec/lib/service/steps_inspector_spec.rb+++ b/spec/lib/service/steps_inspector_spec.rb@@ -14,13 +14,16 @@ RSpec.describe Service::StepsInspector doparams doattribute :parameter+ attribute :other_param, :integervalidates :parameter, presence: trueend- transaction do- step :in_transaction_step_1- step :in_transaction_step_2+ lock(:parameter, :other_param) do+ transaction do+ step :in_transaction_step_1+ step :in_transaction_step_2+ endendtry { step :might_raise }@@ -51,16 +54,17 @@ RSpec.describe Service::StepsInspector docontext "when service runs without error" doit "outputs all the steps of the service" doexpect(output).to eq <<~OUTPUT.chomp- [ 1/10] [options] default ✅- [ 2/10] [model] model ✅- [ 3/10] [policy] policy ✅- [ 4/10] [params] default ✅- [ 5/10] [transaction]- [ 6/10] [step] in_transaction_step_1 ✅- [ 7/10] [step] in_transaction_step_2 ✅- [ 8/10] [try]- [ 9/10] [step] might_raise ✅- [10/10] [step] final_step ✅+ [ 1/11] [options] default ✅+ [ 2/11] [model] model ✅+ [ 3/11] [policy] policy ✅+ [ 4/11] [params] default ✅+ [ 5/11] [lock] parameter:other_param ✅+ [ 6/11] [transaction]+ [ 7/11] [step] in_transaction_step_1 ✅+ [ 8/11] [step] in_transaction_step_2 ✅+ [ 9/11] [try]+ [10/11] [step] might_raise ✅+ [11/11] [step] final_step ✅OUTPUTend@@ -80,10 +84,10 @@ RSpec.describe Service::StepsInspector doit "shows the failing step" doexpect(output).to eq <<~OUTPUT.chomp- [ 1/10] [options] default ✅- [ 2/10] [model] model ❌+ [ 1/11] [options] default ✅+ [ 2/11] [model] model ❌- (8 more steps not shown as the execution flow was stopped before reaching them)+ (9 more steps not shown as the execution flow was stopped before reaching them)OUTPUTendend@@ -99,11 +103,11 @@ RSpec.describe Service::StepsInspector doit "shows the failing step" doexpect(output).to eq <<~OUTPUT.chomp- [ 1/10] [options] default ✅- [ 2/10] [model] model ✅- [ 3/10] [policy] policy ❌+ [ 1/11] [options] default ✅+ [ 2/11] [model] model ✅+ [ 3/11] [policy] policy ❌- (7 more steps not shown as the execution flow was stopped before reaching them)+ (8 more steps not shown as the execution flow was stopped before reaching them)OUTPUTendend@@ -113,12 +117,12 @@ RSpec.describe Service::StepsInspector doit "shows the failing step" doexpect(output).to eq <<~OUTPUT.chomp- [ 1/10] [options] default ✅- [ 2/10] [model] model ✅- [ 3/10] [policy] policy ✅- [ 4/10] [params] default ❌+ [ 1/11] [options] default ✅+ [ 2/11] [model] model ✅+ [ 3/11] [policy] policy ✅+ [ 4/11] [params] default ❌- (6 more steps not shown as the execution flow was stopped before reaching them)+ (7 more steps not shown as the execution flow was stopped before reaching them)OUTPUTendend@@ -134,13 +138,14 @@ RSpec.describe Service::StepsInspector doit "shows the failing step" doexpect(output).to eq <<~OUTPUT.chomp- [ 1/10] [options] default ✅- [ 2/10] [model] model ✅- [ 3/10] [policy] policy ✅- [ 4/10] [params] default ✅- [ 5/10] [transaction]- [ 6/10] [step] in_transaction_step_1 ✅- [ 7/10] [step] in_transaction_step_2 ❌+ [ 1/11] [options] default ✅+ [ 2/11] [model] model ✅+ [ 3/11] [policy] policy ✅+ [ 4/11] [params] default ✅+ [ 5/11] [lock] parameter:other_param ✅+ [ 6/11] [transaction]+ [ 7/11] [step] in_transaction_step_1 ✅+ [ 8/11] [step] in_transaction_step_2 ❌(3 more steps not shown as the execution flow was stopped before reaching them)OUTPUT@@ -158,37 +163,55 @@ RSpec.describe Service::StepsInspector doit "shows the failing step" doexpect(output).to eq <<~OUTPUT.chomp- [ 1/10] [options] default ✅- [ 2/10] [model] model ✅- [ 3/10] [policy] policy ✅- [ 4/10] [params] default ✅- [ 5/10] [transaction]- [ 6/10] [step] in_transaction_step_1 ✅- [ 7/10] [step] in_transaction_step_2 ✅- [ 8/10] [try]- [ 9/10] [step] might_raise 💥+ [ 1/11] [options] default ✅+ [ 2/11] [model] model ✅+ [ 3/11] [policy] policy ✅+ [ 4/11] [params] default ✅+ [ 5/11] [lock] parameter:other_param ✅+ [ 6/11] [transaction]+ [ 7/11] [step] in_transaction_step_1 ✅+ [ 8/11] [step] in_transaction_step_2 ✅+ [ 9/11] [try]+ [10/11] [step] might_raise 💥(1 more steps not shown as the execution flow was stopped before reaching them)OUTPUTendend+ context "when the lock step is failing" do+ before { allow(DistributedMutex).to receive(:synchronize) }++ it "shows the failing step" do+ expect(output).to eq <<~OUTPUT.chomp+ [ 1/11] [options] default ✅+ [ 2/11] [model] model ✅+ [ 3/11] [policy] policy ✅+ [ 4/11] [params] default ✅+ [ 5/11] [lock] parameter:other_param ❌++ (6 more steps not shown as the execution flow was stopped before reaching them)+ OUTPUT+ end+ end+context "when running in specs" docontext "when a successful step is flagged as being an unexpected result" dobefore { result["result.policy.policy"]["spec.unexpected_result"] = true }it "adapts its output accordingly" doexpect(output).to eq <<~OUTPUT.chomp- [ 1/10] [options] default ✅- [ 2/10] [model] model ✅- [ 3/10] [policy] policy ✅ ⚠️ <= expected to return false but got true instead- [ 4/10] [params] default ✅- [ 5/10] [transaction]- [ 6/10] [step] in_transaction_step_1 ✅- [ 7/10] [step] in_transaction_step_2 ✅- [ 8/10] [try]- [ 9/10] [step] might_raise ✅- [10/10] [step] final_step ✅+ [ 1/11] [options] default ✅+ [ 2/11] [model] model ✅+ [ 3/11] [policy] policy ✅ ⚠️ <= expected to return false but got true instead+ [ 4/11] [params] default ✅+ [ 5/11] [lock] parameter:other_param ✅+ [ 6/11] [transaction]+ [ 7/11] [step] in_transaction_step_1 ✅+ [ 8/11] [step] in_transaction_step_2 ✅+ [ 9/11] [try]+ [10/11] [step] might_raise ✅+ [11/11] [step] final_step ✅OUTPUTendend@@ -205,11 +228,11 @@ RSpec.describe Service::StepsInspector doit "adapts its output accordingly" doexpect(output).to eq <<~OUTPUT.chomp- [ 1/10] [options] default ✅- [ 2/10] [model] model ✅- [ 3/10] [policy] policy ❌ ⚠️ <= expected to return true but got false instead+ [ 1/11] [options] default ✅+ [ 2/11] [model] model ✅+ [ 3/11] [policy] policy ❌ ⚠️ <= expected to return true but got false instead- (7 more steps not shown as the execution flow was stopped before reaching them)+ (8 more steps not shown as the execution flow was stopped before reaching them)OUTPUTendend@@ -263,7 +286,7 @@ RSpec.describe Service::StepsInspector doendit "returns the provided paramaters" do- expect(error).to match(/{"parameter"=>nil}/)+ expect(error).to match(/{"parameter"=>nil, "other_param"=>nil}/)endend@@ -312,6 +335,14 @@ RSpec.describe Service::StepsInspector doexpect(error).to match(/BOOM \([^(]*RuntimeError[^)]*\)/)endend++ context "when the lock step is failing" do+ before { allow(DistributedMutex).to receive(:synchronize) }++ it "returns an error" do+ expect(error).to eq("Lock 'parameter:other_param' was not acquired.")+ end+ endenddescribe "#inspect" do@@ -321,18 +352,18 @@ RSpec.describe Service::StepsInspector doexpect(inspector.inspect.gsub(%r{ \(\d+\.\d+ ms\)}, "")).to eq(<<~OUTPUT)Inspecting DummyService result object:- [ 1/10] [options] default ✅- [ 2/10] [model] model ✅- [ 3/10] [policy] policy ✅- [ 4/10] [params] default ❌+ [ 1/11] [options] default ✅+ [ 2/11] [model] model ✅+ [ 3/11] [policy] policy ✅+ [ 4/11] [params] default ❌- (6 more steps not shown as the execution flow was stopped before reaching them)+ (7 more steps not shown as the execution flow was stopped before reaching them)Why it failed:#<ActiveModel::Errors [#<ActiveModel::Error attribute=parameter, type=blank, options={}>]>- Provided parameters: {"parameter"=>nil}+ Provided parameters: {"parameter"=>nil, "other_param"=>nil}OUTPUTendend