Class: Julewire::Ractor::Fanout

Inherits:
Object
  • Object
show all
Defined in:
lib/julewire/ractor/fanout.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(destinations:, name: :ractor_fanout, on_failure: nil) ⇒ Fanout

Returns a new instance of Fanout.

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
17
18
# File 'lib/julewire/ractor/fanout.rb', line 8

def initialize(destinations:, name: :ractor_fanout, on_failure: nil)
  @name = Core::Destinations.normalize_name(name)
  raise ArgumentError, "destinations must be an Array" unless destinations.instance_of?(Array)

  @destinations = destinations.map { normalize_destination(it) }
  raise ArgumentError, "destinations must not be empty" if @destinations.empty?

  Core::Validation.validate_callable!(on_failure, name: :on_failure, allow_nil: true)
  @on_failure = on_failure
  @health = Core::Integration::DestinationHealth.new(counter_keys: [], failure_counter: nil)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/julewire/ractor/fanout.rb', line 6

def name
  @name
end

Instance Method Details

#after_fork!Object



35
36
37
38
39
40
# File 'lib/julewire/ractor/fanout.rb', line 35

def after_fork!
  @health.recover_if_successful do
    @destinations.each { call_destination_after_fork(it) }
  end
  self
end

#before_fork!(timeout: nil) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/julewire/ractor/fanout.rb', line 42

def before_fork!(timeout: nil)
  prepared = []
  Core::Validation.validate_timeout!(timeout, name: :timeout)
  deadline = Core::Scheduling::Deadline.for(timeout)
  @destinations.each do |destination|
    next unless destination.respond_to?(:before_fork!)

    prepared << destination
    result = destination.before_fork!(timeout: Core::Scheduling::Deadline.remaining(deadline))
    raise Core::Error, "destination #{destination.name} rejected before_fork" if result == false
  end
  self
rescue StandardError
  prepared.reverse_each { call_destination_after_fork(it) }
  raise
end

#close(timeout: nil) ⇒ Object



31
32
33
# File 'lib/julewire/ractor/fanout.rb', line 31

def close(timeout: nil)
  call_lifecycle(:close, timeout: timeout)
end

#emit(record) ⇒ Object



20
21
22
23
24
25
# File 'lib/julewire/ractor/fanout.rb', line 20

def emit(record)
  @health.recover_if_successful do
    @destinations.each { emit_to_destination(it, record) }
  end
  nil
end

#flush(timeout: nil) ⇒ Object



27
28
29
# File 'lib/julewire/ractor/fanout.rb', line 27

def flush(timeout: nil)
  call_lifecycle(:flush, timeout: timeout)
end

#healthObject



61
62
63
64
65
66
67
# File 'lib/julewire/ractor/fanout.rb', line 61

def health
  destinations = @destinations.to_h { [it.name, destination_health(it)] }
  @health.snapshot(
    destinations: destinations,
    status: health_status(destinations)
  )
end

#resource_identityObject



59
# File 'lib/julewire/ractor/fanout.rb', line 59

def resource_identity = self