Class: Bg::Asyncable::Wrapper

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(global_id, delay: 0) ⇒ Wrapper

Returns a new instance of Wrapper.



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

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

Instance Attribute Details

#delayObject (readonly)

Returns the value of attribute delay.



9
10
11
# File 'lib/bg/asyncable.rb', line 9

def delay
  @delay
end

#global_idObject (readonly)

Returns the value of attribute global_id.



9
10
11
# File 'lib/bg/asyncable.rb', line 9

def global_id
  @global_id
end

Instance Method Details

#invoke_method(name, *args) ⇒ Object



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

def invoke_method(name, *args)
  sleep delay if delay > 0
  ::ActiveRecord::Base.connection_pool.with_connection do
    global_id.find.send name, *args
  end
end