Skip to content

A platform for community discussion. Free, open, simple.

DEV: Allow lock step to resolve keys from service context (#37952)

Loïc Guitaut9290012adf8e
diff --git a/docs/developer-guides/docs/03-code-internals/19-service-objects.md b/docs/developer-guides/docs/03-code-internals/19-service-objects.md
index f4eecddc130..d4e71df1ae8 100644
--- a/docs/developer-guides/docs/03-code-internals/19-service-objects.md
+++ b/docs/developer-guides/docs/03-code-internals/19-service-objects.md
@@ -397,7 +397,9 @@ This step wraps other steps. If any of the wrapped steps raises an exception, th
This step wraps other steps. If the lock can't be acquired for any reason, the execution flow will be halted.
-The `keys` provided are matched against the `params` object of the service. The corresponding values are used to provide the underlying lock with a unique name. For example, if the provided key is `user_id` and its value is `1`, then the resulting lock name will be `user_id:1`, so if the same service is called with `2` as the `user_id` value, it will not wait on the first lock.
+The `keys` provided are first matched against the `params` object of the service and, if not found, against the service context. This allows locking on values derived from earlier steps, such as a model fetched from the database. When a resolved value responds to `id` (e.g. an `ActiveRecord` model), its `id` is used automatically to build the lock name.
+
+For example, if the provided key is `user_id` and its value is `1`, then the resulting lock name will be `user_id:1`, so if the same service is called with `2` as the `user_id` value, it will not wait on the first lock. Similarly, `lock(:topic)` after a `model :topic` step will use the topic's `id` in the lock name.
### `only_if(condition_name, &block)`
diff --git a/lib/service/base.rb b/lib/service/base.rb
index 724c2f87bf7..ff273cae98c 100644
--- a/lib/service/base.rb
+++ b/lib/service/base.rb
@@ -314,7 +314,7 @@ module Service
end
if success != :success
- context[result_key].fail(lock_not_aquired: true)
+ context[result_key].fail(lock_not_acquired: true)
context.fail!
end
end
@@ -324,7 +324,10 @@ module Service
def lock_name
[
context.__service_class__.to_s.underscore,
- *keys.flat_map { |key| [key, context[:params].public_send(key)] },
+ *keys.flat_map do |key|
+ value = context[:params].public_send(key) || context[key]
+ [key, value.try(:id) || value]
+ end,
].join(":")
end
end
diff --git a/spec/lib/service/runner_spec.rb b/spec/lib/service/runner_spec.rb
index 4aa0c9d4103..32fcaeb0426 100644
--- a/spec/lib/service/runner_spec.rb
+++ b/spec/lib/service/runner_spec.rb
@@ -181,6 +181,22 @@ RSpec.describe Service::Runner do
end
end
+ class LockWithModelService
+ include Service::Base
+
+ model :topic
+ lock(:topic) { step :locked_step }
+
+ private
+
+ def fetch_topic
+ OpenStruct.new(id: 123)
+ end
+
+ def locked_step
+ end
+ end
+
describe ".call" do
subject(:runner) { described_class.call(service, dependencies, &actions_block) }
@@ -516,6 +532,40 @@ RSpec.describe Service::Runner do
expect(runner).to eq :success
end
end
+
+ context "when the lock key resolves from context" do
+ let(:service) { LockWithModelService }
+ let(:dependencies) { { params: {} } }
+ let(:actions) { <<-BLOCK }
+ proc do
+ on_success { :success }
+ on_lock_not_acquired(:topic) { :lock_not_acquired }
+ end
+ BLOCK
+
+ context "when the lock is acquired" do
+ before { allow(DistributedMutex).to receive(:synchronize).and_call_original }
+
+ it "runs the success block" do
+ expect(runner).to eq :success
+ end
+
+ it "uses the model's id in the lock name" do
+ runner
+ expect(DistributedMutex).to have_received(:synchronize).with(
+ "lock_with_model_service:topic:123",
+ )
+ end
+ end
+
+ context "when the lock is not acquired" do
+ before { allow(DistributedMutex).to receive(:synchronize) }
+
+ it "runs the lock_not_acquired block" do
+ expect(runner).to eq :lock_not_acquired
+ end
+ end
+ end
end
context "when using several actions together" do