Skip to content

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

DEV: Fix isolation bugs in service framework's `each` step (#40311)

Loïc Guitautf92a03593623
diff --git a/lib/service/base/context.rb b/lib/service/base/context.rb
index 71700514fbd..0264b99e0f7 100644
--- a/lib/service/base/context.rb
+++ b/lib/service/base/context.rb
@@ -23,15 +23,16 @@ module Service
end
def to_h
- store.deep_dup
+ store.dup
end
def with_isolation(persist_keys: [])
- @isolated_store = to_h
+ isolated_stores << to_h
yield
ensure
- @store.merge!(@isolated_store.slice(*persist_keys, *step_result_keys))
- @isolated_store = nil
+ isolated_stores.pop.then do |isolated_store|
+ store.merge!(isolated_store.slice(*persist_keys, *step_result_keys(isolated_store)))
+ end
end
# @return [Boolean] returns +true+ if the context is set as successful (default)
@@ -74,11 +75,15 @@ module Service
private
def store
- @isolated_store || @store
+ isolated_stores.last || @store
end
- def step_result_keys
- store.keys.select { it.start_with?("result.") }
+ def isolated_stores
+ @isolated_stores ||= []
+ end
+
+ def step_result_keys(source = store)
+ source.keys.select { it.start_with?("result.") }
end
def method_missing(method_name, *args, &block)
diff --git a/lib/service/base/each_step.rb b/lib/service/base/each_step.rb
index d6141a3fea9..708442d4ef4 100644
--- a/lib/service/base/each_step.rb
+++ b/lib/service/base/each_step.rb
@@ -16,18 +16,18 @@ module Service
end
def run_step
- context.with_isolation(persist_keys: [item_name, :index, *initializers.keys]) do
- context.merge!(initializers.transform_values { instance.instance_exec(&it) })
- Array
- .wrap(context[name])
- .tap do |collection|
- context[result_key].merge!(total: collection.size, skipped?: collection.empty?)
- end
- .each_with_index do |item, index|
+ context.merge!(initializers.transform_values { instance.instance_exec(&it) })
+ Array
+ .wrap(context[name])
+ .tap do |collection|
+ context[result_key].merge!(total: collection.size, skipped?: collection.empty?)
+ end
+ .each_with_index do |item, index|
+ context.with_isolation(persist_keys: [item_name, :index, *initializers.keys]) do
context.merge!(item_name => item, :index => index)
steps.each { |step| step.call(instance, context) }
end
- end
+ end
end
private
diff --git a/spec/lib/service/base/each_step_spec.rb b/spec/lib/service/base/each_step_spec.rb
index dbc07822c25..beb7c952000 100644
--- a/spec/lib/service/base/each_step_spec.rb
+++ b/spec/lib/service/base/each_step_spec.rb
@@ -132,6 +132,80 @@ RSpec.describe Service::Base::EachStep do
end
end
+ context "with isolation between iterations" do
+ let(:service) do
+ Class.new do
+ include Service::Base
+
+ step :setup
+
+ each :users, persist: { observed_values: -> { [] } } do
+ step :track
+ end
+
+ private
+
+ def setup
+ context[:users] = %i[alice bob]
+ end
+
+ def track(user:, observed_values:)
+ observed_values << context[:transient]
+ context[:transient] = user
+ end
+ end
+ end
+
+ it "does not leak non-persisted variables between iterations" do
+ expect(result).to have_attributes(observed_values: [nil, nil])
+ end
+ end
+
+ context "when the outer context contains an ActiveRecord model" do
+ fab!(:user)
+
+ let(:service) do
+ Class.new do
+ include Service::Base
+
+ model :user
+ step :setup_usernames
+
+ each :usernames,
+ persist: {
+ seen_user_ids: -> { [] },
+ user_persisted_states: -> { [] },
+ } do
+ step :track_user
+ end
+
+ private
+
+ def fetch_user(user_id:)
+ User.find(user_id)
+ end
+
+ def setup_usernames
+ context[:usernames] = %i[alice bob]
+ end
+
+ def track_user(user:, seen_user_ids:, user_persisted_states:)
+ seen_user_ids << user.id
+ user_persisted_states << user.persisted?
+ end
+ end
+ end
+
+ let(:dependencies) { { user_id: user.id } }
+
+ it "keeps the original model available inside each iterations" do
+ expect(result).to have_attributes(
+ seen_user_ids: [user.id, user.id],
+ user_persisted_states: [true, true],
+ )
+ end
+ end
+
context "with persist option" do
context "with a lambda initializer" do
let(:service) do
@@ -196,5 +270,45 @@ RSpec.describe Service::Base::EachStep do
end
end
end
+
+ context "when nesting each steps" do
+ let(:service) do
+ Class.new do
+ include Service::Base
+
+ model :groups
+
+ each :groups, persist: { processed: -> { [] } } do
+ each :group, as: :item, persist: { processed: -> { context[:processed] } } do
+ step :collect_item
+ end
+ end
+
+ private
+
+ def fetch_groups
+ context[:input_groups]
+ end
+
+ def collect_item(group:, item:, processed:)
+ processed << [group, item]
+ end
+ end
+ end
+ let(:dependencies) { { input_groups: [%i[alice bob], %i[charlie dave]] } }
+
+ it { is_expected.to run_successfully }
+
+ it "accumulates results across both levels of iteration" do
+ expect(result[:processed]).to eq(
+ [
+ [%i[alice bob], :alice],
+ [%i[alice bob], :bob],
+ [%i[charlie dave], :charlie],
+ [%i[charlie dave], :dave],
+ ],
+ )
+ end
+ end
end
end