Class: Bg::Asyncable::Wrapper

Inherits:
Object
  • Object
show all
Includes:
Concurrent::Async
Defined in:
lib/bg/asyncable.rb

Instance Method Summary collapse

Constructor Details

#initialize(object, wait: 0) ⇒ Wrapper

Returns a new instance of Wrapper.



9
10
11
12
13
14
15
# File 'lib/bg/asyncable.rb', line 9

def initialize(object, wait: 0)
  # IMPORTANT: call super without any arguments
  # https://ruby-concurrency.github.io/concurrent-ruby/Concurrent/Async.html
  super()
  @object = object
  @wait = wait.to_f
end

Instance Method Details

#invoke_method(name, *args) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/bg/asyncable.rb', line 17

def invoke_method(name, *args)
  sleep @wait if @wait > 0
  base = self.is_a?(::ActiveRecord::Base) ? self.class : ::ActiveRecord::Base
  base.connection_pool.with_connection do
    @object.send name, *args
  end
end