Class: Wal::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/wal/runner.rb

Defined Under Namespace

Classes: Worker

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config:, db_config:) ⇒ Runner

Returns a new instance of Runner.



92
93
94
95
96
# File 'lib/wal/runner.rb', line 92

def initialize(config:, db_config:)
  @config = config
  @db_config = db_config
  @child_pids = []
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



90
91
92
# File 'lib/wal/runner.rb', line 90

def config
  @config
end

#db_configObject (readonly)

Returns the value of attribute db_config.



90
91
92
# File 'lib/wal/runner.rb', line 90

def db_config
  @db_config
end

Instance Method Details

#run_forked_workers(workers_slots) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/wal/runner.rb', line 115

def run_forked_workers(workers_slots)
  Wal.hooks[:before_fork]&.call(workers_slots)

  workers_slots.each do |worker_name, slot_configs|
    pid = fork_worker(worker_name, slot_configs)
    @child_pids << pid
    puts "[#{worker_name}] Spawned worker '#{worker_name}' with PID #{pid}"
  end

  @ping_thread = start_ping_thread
  setup_signal_handlers
  wait_for_workers
end

#run_single_worker(worker_name, slot_configs) ⇒ Object



108
109
110
111
112
113
# File 'lib/wal/runner.rb', line 108

def run_single_worker((worker_name, slot_configs))
  @ping_thread = start_ping_thread
  puts "[#{worker_name}] Starting worker process (PID: #{Process.pid})"
  worker = Worker.new(name: worker_name, slot_configs: slot_configs, db_config: db_config)
  worker.run
end

#startObject



98
99
100
101
102
103
104
105
106
# File 'lib/wal/runner.rb', line 98

def start
  workers_slots = config["slots"].group_by { |_slot, slot_config| slot_config["worker"] || "default" }

  if workers_slots.size == 1
    run_single_worker(workers_slots.first)
  else
    run_forked_workers(workers_slots)
  end
end