Skip to content

Add :run callback support to interactions#592

Open
scottgratton wants to merge 1 commit intoAaronLasseigne:mainfrom
scottgratton:add-callbacks-for-run
Open

Add :run callback support to interactions#592
scottgratton wants to merge 1 commit intoAaronLasseigne:mainfrom
scottgratton:add-callbacks-for-run

Conversation

@scottgratton
Copy link
Copy Markdown

@scottgratton scottgratton commented Jan 27, 2026

#593

Problem:
In some services, I'd like to wrap the full operation in a mutex lock inside of the service itself.

In my case I am using RedLock to prevent race conditions. When I used it to wrap the contents of my execute block, some values were actually being cached prior during the ActiveInteraction validations - at the same time, another thread was still running its execute block with the mutex lock. The first thread releases the lock and the second thread begins its execute block, acquiring a lock for itself, but having already cached some values that are then out of date. The currently available set_callback functionality allows for callbacks around filter, validate, and execute, but you cannot wrap all of those in one action. I would prefer to use the block implementation from Redlock rather than worrying about starting a lock and then ensuring it is released in all scenarios.

Ideally, I'd like to do something like this:

set_callback :run, :around, lambda { |_interaction, block|
    lock_manager = Redlock::Client.new(MY_REDIS_CONFIG)
    lock_manager.lock!("resource_key", 2000) do
      block.call
    end
  }

Approach:
Here is a breakdown of what was added:

  • Added define_callbacks :run to Runnable module
  • Wrapped #run method with run_callbacks to allow :before, :after, and :around callbacks
  • Added tests for :run callback with all three types
  • Updated README with documentation and example

I got this running locally and have ran rake locally to confirm that all tests and linting pass.


I'd be grateful if maintainers would consider this addition as a small, but helpful addition when wanting to wrap the entire execution from within the ActiveInteraction service without having to do something like overwrite the run! method from within. Thanks for your consideration!

@scottgratton scottgratton force-pushed the add-callbacks-for-run branch 3 times, most recently from c7d9f89 to 775d130 Compare January 29, 2026 18:20
- Added define_callbacks :run to Runnable module
- Wrapped #run method with run_callbacks to allow :before, :after, and :around callbacks
- Added tests for :run callback with all three types
- Updated README with documentation and example
@scottgratton scottgratton force-pushed the add-callbacks-for-run branch from 775d130 to bff81bc Compare January 29, 2026 18:25
@mkrul
Copy link
Copy Markdown

mkrul commented Jan 29, 2026

54TLbcUcnRm3sWQK3HKkuAx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants