Skip to content

Cannot wrap full service execution in a callback from within #593

@scottgratton

Description

@scottgratton

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
  }

I've already put up a PR that I think would be the solution for what I'm looking for here. I am happy to receive any feedback:
https://github.com/AaronLasseigne/active_interaction/pull/592/files

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions