Skip to content

[SP-8460] Introduce within executor#38

Merged
petergebala merged 6 commits into
masterfrom
SP-8460-around-executor
Apr 14, 2026
Merged

[SP-8460] Introduce within executor#38
petergebala merged 6 commits into
masterfrom
SP-8460-around-executor

Conversation

@msx2

@msx2 msx2 commented Apr 13, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds a new within executor to the Opera DSL, allowing one or more steps to be wrapped with a custom method defined on the operation class.

Removes benchmark executor - no longer needed.

What was added

within DSL method — wraps a block of steps with a named instance method. The method must yield to execute the nested steps; if it does not yield, the nested steps are skipped silently. Standard break conditions (finish!, errors) propagate correctly inside the block.

class MyOperaClass < Opera::Operation::Base
  within :read_from_replica do
    step :calculations_method_1
    step :calculations_method_2
  end
  step :some_other_step
  step :another_method
  step :output

  def calculations_method_1
    # logic executed on replica
  end

  def calculations_method_2
    # logic executed on replica
  end

  def some_other_step
    # logic here from main db
  end

  def another_method
    read_from_replica do
      # calculations reading from replica
    end
  end

  def output
    result.output = { success: true }
  end

  private

  def read_from_replica(&block)
    ActiveRecord::Base.connected_to(role: :reading, &block)
  end
end

The named method can also be called inline within any other step method, without the DSL block, for ad-hoc usage.

@msx2 msx2 added the enhancement New feature or request label Apr 13, 2026
@msx2 msx2 requested review from cintrzyk, kikorb and petergebala April 13, 2026 12:45
@kikorb

kikorb commented Apr 13, 2026

Copy link
Copy Markdown
Member

@msx2 love the idea, have you thought about

  1. using super like we do in benchmark for example so the steps are evaluated by the executor as steps
  2. reuse your newly created class and have benchmark inherit from it for example. maybe even transaction but that might be a little bit more complex

Let me know

@cintrzyk cintrzyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice concept! I think around might be a bit misleading here, as it suggests a callback-style wrapper rather than executing steps within a specific context. Maybe something like within would better reflect the intent?

Does it work with other DSL methods? for example:

around with_something do 
  validate :schema
  operation :notify
end

if yes, then it would be great to add an example in Readme and specs 🤔

@msx2

msx2 commented Apr 14, 2026

Copy link
Copy Markdown
Contributor Author

@kikorb

  1. using super like we do in benchmark for example so the steps are evaluated by the executor as steps

It used the same logic as the super does, but explicitly. Everything was evaluated properly. But I changed this for consistency in 869ad3c

  1. reuse your newly created class and have benchmark inherit from it for example. maybe even transaction but that might be a little bit more complex

Originally I thought about it for a moment, but I think it will bring more complexity than is necessary for what we want to accomplish here, so I'd leave it as is :)


@cintrzyk

Does it work with other DSL methods? for example:

around with_something do 
  validate :schema
  operation :notify
end

if yes, then it would be great to add an example in Readme and specs 🤔

It works with all DSL methods. Added more examples in 78398bd with mixed step/operation cases. Added there also a case where around is used inside a transaction.

I think around might be a bit misleading here, as it suggests a callback-style wrapper rather than executing steps within a specific context. Maybe something like within would better reflect the intent?

I hear you... Renamed to within in 4036365

@msx2 msx2 changed the title [SP-8460] Introduce around executor [SP-8460] Introduce within executor Apr 14, 2026

@kikorb kikorb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove Benchmark @msx2 it is no longer needed, redundant. This approach is superior and we could remove complexity.

Maybe if you don't feel like it mark it as deprecated so we drop it later

@msx2

msx2 commented Apr 14, 2026

Copy link
Copy Markdown
Contributor Author

@kikorb Removed benchmark in 7a05419

@petergebala petergebala merged commit 9eafd8c into master Apr 14, 2026
4 checks passed
@msx2 msx2 deleted the SP-8460-around-executor branch April 14, 2026 10:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants