Class: Sponges::Commander

Inherits:
Object
  • Object
show all
Includes:
Alive
Defined in:
lib/sponges/commander.rb

Overview

This class concern is to send messages to supervisor. It’s used to send messages like ‘stop’ or ‘restart’

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Commander

Returns a new instance of Commander.



12
13
14
15
# File 'lib/sponges/commander.rb', line 12

def initialize(name, options = {})
  @name, @options = name, options
  @store = Sponges::Store.new(@name)
end

Instance Attribute Details

#storeObject (readonly)

Returns the value of attribute store.



10
11
12
# File 'lib/sponges/commander.rb', line 10

def store
  @store
end

Instance Method Details

#decrementObject

Decrement workers’s pool by one.



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/sponges/commander.rb', line 72

def decrement
  Sponges.logger.info "Runner #{@name} decrement message received."
  if supervisor_pid
    begin
      Process.kill :TTOU, supervisor_pid.to_i
    rescue Errno::ESRCH => e
      Sponges.logger.error e
    end
  else
    Sponges.logger.info "No supervisor found."
  end
end

#incrementObject

Increment workers’s pool by one.



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/sponges/commander.rb', line 57

def increment
  Sponges.logger.info "Runner #{@name} increment message received."
  if pid = supervisor_pid
    begin
      Process.kill :TTIN, supervisor_pid.to_i
    rescue Errno::ESRCH => e
      Sponges.logger.error e
    end
  else
    Sponges.logger.info "No supervisor found."
  end
end

#killObject

Kills the supervisor, and then all workers.



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

def kill
  Sponges.logger.info "Runner #{@name} kill message received."
  stop :KILL
  children_pids.each do|pid|
    begin
      kill_process(:KILL, pid, "Worker")
    rescue Errno::ESRCH => e
      # Don't panic
    ensure
      store.delete_children pid
    end
  end
end

#stop(signal = nil) ⇒ Object

Stops supervisor, signal depends on options given by Boson.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/sponges/commander.rb', line 35

def stop(signal = nil)
  signal ||= gracefully? ? :HUP : :QUIT
  Sponges.logger.info "Runner #{@name} stop message received."
  if pid = supervisor_pid
    if @options[:timeout]
      begin
        Timeout::timeout(@options[:timeout]) do
          kill_process(signal, pid)
        end
      rescue Timeout::Error
        kill
      end
    else
      kill_process(signal, pid)
    end
  else
    Sponges.logger.info "No supervisor found."
  end
end