Class: SolidObjects::Supervisor

Inherits:
Object
  • Object
show all
Defined in:
lib/solid_objects/supervisor.rb,
sig/generated/lib/solid_objects/supervisor.rbs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(worker_count: SolidObjects.configuration.worker_count, effect_worker_count: SolidObjects.configuration.effect_worker_count, broadcast_worker_count: SolidObjects.configuration.broadcast_worker_count, reminder_scheduler_count: SolidObjects.configuration.reminder_scheduler_count) ⇒ Supervisor

Returns a new instance of Supervisor.

RBS:

  • (?worker_count: Integer, ?effect_worker_count: Integer, ?broadcast_worker_count: Integer, ?reminder_scheduler_count: Integer) -> void

Parameters:

  • worker_count: (Integer) (defaults to: SolidObjects.configuration.worker_count)
  • effect_worker_count: (Integer) (defaults to: SolidObjects.configuration.effect_worker_count)
  • broadcast_worker_count: (Integer) (defaults to: SolidObjects.configuration.broadcast_worker_count)
  • reminder_scheduler_count: (Integer) (defaults to: SolidObjects.configuration.reminder_scheduler_count)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/solid_objects/supervisor.rb', line 10

def initialize(
  worker_count: SolidObjects.configuration.worker_count,
  effect_worker_count: SolidObjects.configuration.effect_worker_count,
  broadcast_worker_count: SolidObjects.configuration.broadcast_worker_count,
  reminder_scheduler_count: SolidObjects.configuration.reminder_scheduler_count
)
  @components = build_components(
    worker_count:,
    effect_worker_count:,
    broadcast_worker_count:,
    reminder_scheduler_count:
  )
  @threads = []
  @started = false
end

Instance Attribute Details

#componentsObject (readonly)

Returns the value of attribute components.

Returns:

  • (Object)


56
57
58
# File 'lib/solid_objects/supervisor.rb', line 56

def components
  @components
end

#threadsObject (readonly)

Returns the value of attribute threads.

Returns:

  • (Object)


56
57
58
# File 'lib/solid_objects/supervisor.rb', line 56

def threads
  @threads
end

Instance Method Details

#build_components(worker_count:, effect_worker_count:, broadcast_worker_count:, reminder_scheduler_count:) ⇒ Array[Worker | EffectExecutor | ReminderScheduler | BroadcastExecutor]

RBS:

  • (worker_count: Integer, effect_worker_count: Integer, broadcast_worker_count: Integer, reminder_scheduler_count: Integer) -> Array[Worker | EffectExecutor | ReminderScheduler | BroadcastExecutor]

Parameters:

  • worker_count: (Integer)
  • effect_worker_count: (Integer)
  • broadcast_worker_count: (Integer)
  • reminder_scheduler_count: (Integer)

Returns:



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/solid_objects/supervisor.rb', line 59

def build_components(
  worker_count:,
  effect_worker_count:,
  broadcast_worker_count:,
  reminder_scheduler_count:
)
  Array.new(worker_count) { Worker.new } +
    Array.new(effect_worker_count) { EffectExecutor.new } +
    Array.new(broadcast_worker_count) { BroadcastExecutor.new } +
    Array.new(reminder_scheduler_count) { ReminderScheduler.new }
end

#join_until_timeoutvoid

This method returns an undefined value.

RBS:

  • () -> void



72
73
74
75
76
77
78
79
80
# File 'lib/solid_objects/supervisor.rb', line 72

def join_until_timeout
  deadline = monotonic_now + SolidObjects.configuration.shutdown_timeout
  threads.each do |thread|
    remaining = deadline - monotonic_now
    break unless remaining.positive?

    thread.join(remaining)
  end
end

#monotonic_nowFloat

RBS:

  • () -> Float

Returns:

  • (Float)


83
84
85
# File 'lib/solid_objects/supervisor.rb', line 83

def monotonic_now
  ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
end

#runvoid

This method returns an undefined value.

RBS:

  • () -> void



27
28
29
30
31
32
# File 'lib/solid_objects/supervisor.rb', line 27

def run
  start
  threads.each(&:join)
ensure
  stop
end

#startvoid

This method returns an undefined value.

RBS:

  • () -> void



35
36
37
38
39
40
41
# File 'lib/solid_objects/supervisor.rb', line 35

def start
  return if @started

  @started = true
  @threads = components.map { |component| Thread.new { component.run } }
  SolidObjects.instrument(:"supervisor.started", component_count: components.length)
end

#stopvoid

This method returns an undefined value.

RBS:

  • () -> void



44
45
46
47
48
49
50
51
52
# File 'lib/solid_objects/supervisor.rb', line 44

def stop
  return unless @started

  components.each(&:request_shutdown)
  join_until_timeout
  components.reject(&:stopped?).each(&:stop)
  @started = false
  SolidObjects.instrument(:"supervisor.stopped", component_count: components.length)
end