Class: Sourced::Supervisor

Inherits:
Object
  • Object
show all
Defined in:
lib/sourced/supervisor.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger: Sourced.config.logger, count: 2) ⇒ Supervisor

Returns a new instance of Supervisor.



13
14
15
16
17
# File 'lib/sourced/supervisor.rb', line 13

def initialize(logger: Sourced.config.logger, count: 2)
  @logger = logger
  @count = count
  @workers = []
end

Class Method Details

.startObject



9
10
11
# File 'lib/sourced/supervisor.rb', line 9

def self.start(...)
  new(...).start
end

Instance Method Details

#set_signal_handlersObject



40
41
42
43
# File 'lib/sourced/supervisor.rb', line 40

def set_signal_handlers
  Signal.trap('INT') { stop }
  Signal.trap('TERM') { stop }
end

#startObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/sourced/supervisor.rb', line 19

def start
  logger.info("Starting sync supervisor with #{@count} workers")
  set_signal_handlers
  @workers = @count.times.map do |i|
    Worker.new(logger:, name: "worker-#{i}")
  end
  Sync do |task|
    @workers.each do |wrk|
      task.async do
        wrk.poll
      end
    end
  end
end

#stopObject



34
35
36
37
38
# File 'lib/sourced/supervisor.rb', line 34

def stop
  logger.info("Stopping #{@workers.size} workers")
  @workers.each(&:stop)
  logger.info('All workers stopped')
end