Class: Pgbus::Process::Supervisor

Inherits:
Object
  • Object
show all
Includes:
SignalHandler
Defined in:
lib/pgbus/process/supervisor.rb

Constant Summary collapse

FORK_WAIT =

seconds between fork checks

1
WATCHDOG_INTERVAL =

seconds between stall checks

10

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SignalHandler

included, #interruptible_sleep, #process_signals, #restore_signals, #setup_signals

Constructor Details

#initialize(config: Pgbus.configuration) ⇒ Supervisor

Returns a new instance of Supervisor.



13
14
15
16
17
18
# File 'lib/pgbus/process/supervisor.rb', line 13

def initialize(config: Pgbus.configuration)
  @config = config
  @forks = {}
  @shutting_down = false
  @last_watchdog_at = monotonic_now
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



11
12
13
# File 'lib/pgbus/process/supervisor.rb', line 11

def config
  @config
end

Instance Method Details

#graceful_shutdownObject



40
41
42
43
44
# File 'lib/pgbus/process/supervisor.rb', line 40

def graceful_shutdown
  Pgbus.logger.info { "[Pgbus] Supervisor: graceful shutdown requested" }
  @shutting_down = true
  signal_children("TERM")
end

#immediate_shutdownObject



46
47
48
49
50
# File 'lib/pgbus/process/supervisor.rb', line 46

def immediate_shutdown
  Pgbus.logger.warn { "[Pgbus] Supervisor: immediate shutdown requested" }
  @shutting_down = true
  signal_children("QUIT")
end

#runObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/pgbus/process/supervisor.rb', line 20

def run
  setup_signals
  start_heartbeat

  Pgbus.logger.info { "[Pgbus] Supervisor starting pid=#{::Process.pid}" }

  # Bootstrap queues once in the parent process before forking children.
  # This avoids the deadlock that occurs when multiple forked children
  # race to call enable_notify_insert (DROP TRIGGER + CREATE TRIGGER)
  # concurrently on the same queue tables. Children still call
  # bootstrap_queues post-fork but the idempotent check in
  # notify_trigger_current? makes those calls cheap no-ops.
  bootstrap_queues

  boot_processes
  monitor_loop
ensure
  shutdown
end