Joined November 2016
·

tommotaylor

Freelance Developer at Freelance
·
·

For anyone interested here is my implementation for methods that need arguments passed. I also used ruby keyword arguments, hard coded the number of concurrent processes, removed the options hash and added an ActiveRecord object check so it supports non ActiveRecord objects.

def make_concurrent_calls(object:, method:, method_args: nil)
    processes = 2.times.map do |i|
      ForkBreak::Process.new do |breakpoints|
        # Add a breakpoint after invoking the method
        original_method = object.method(method)
        object.reload if object.is_a?(ActiveRecord::Base)
        object.stub(method) do |*args|
          value = original_method.call(*args)
          breakpoints << method
          value
        end
        if method_args.present?
          args = method_args.collect{|k,v| v}
          object.send(method, *args)
        else
          object.send(method)
        end
      end
    end
    processes.each{ |process| process.run_until(method).wait }
    processes.each{ |process| process.finish.wait }
  end

Thanks for the post, super helpful. Can you tell me how the make_concurrent_calls method would look if the passed in method required arguments? For instance:
make_concurrent_calls(@user, :update(params), {})

Achievements
1 Karma
0 Total ProTip Views
Interests & Skills