Class: Plumbing::Actor::Async

Inherits:
Object
  • Object
show all
Defined in:
lib/plumbing/actor/async.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target) ⇒ Async

Returns a new instance of Async.



10
11
12
13
# File 'lib/plumbing/actor/async.rb', line 10

def initialize target
  @target = target
  @semaphore = ::Async::Semaphore.new(Plumbing.config.max_concurrency)
end

Instance Attribute Details

#targetObject (readonly)

Returns the value of attribute target.



8
9
10
# File 'lib/plumbing/actor/async.rb', line 8

def target
  @target
end

Instance Method Details

#in_context?Boolean

Returns:

  • (Boolean)


33
# File 'lib/plumbing/actor/async.rb', line 33

def in_context? = true

#safelyObject



26
27
28
29
30
31
# File 'lib/plumbing/actor/async.rb', line 26

def safely(&)
  Plumbing.config.logger.debug { "-> #{@target.class}#perform_safely" }
  send_message(:perform_safely, &)
  sleep 0.01
  nil
end

#send_message(message_name, *args, **params, &block) ⇒ Object

Send the message to the target and wrap the result



16
17
18
19
20
21
22
23
24
# File 'lib/plumbing/actor/async.rb', line 16

def send_message(message_name, *args, **params, &block)
  Plumbing.config.logger.debug { "-> #{@target.class}##{message_name}(#{args.inspect}, #{params.inspect})" }
  task = @semaphore.async do
    Plumbing.config.logger.debug { "---> #{@target.class}##{message_name}(#{args.inspect}, #{params.inspect})" }
    @target.send(message_name, *args, **params, &block)
  end
  sleep 0.01
  Result.new(task)
end

#stopObject



35
# File 'lib/plumbing/actor/async.rb', line 35

def stop = nil