Class: Resqued::MasterState

Inherits:
Object
  • Object
show all
Defined in:
lib/resqued/master_state.rb

Overview

Tracks state from the master process. On re-exec, this object will be serialized and passed to the new master.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMasterState

Returns a new instance of MasterState.



5
6
7
8
# File 'lib/resqued/master_state.rb', line 5

def initialize
  @listeners_created = 0
  @listener_states = {}
end

Instance Attribute Details

#config_pathsObject (readonly)

Paths of app’s resqued configuration files. The paths are passed to the listener, and the listener reads the config so that it knows which workers to create.



70
71
72
# File 'lib/resqued/master_state.rb', line 70

def config_paths
  @config_paths
end

#current_listener_pidObject

The PID of the newest listener. This is the one listener that should continue running.



74
75
76
# File 'lib/resqued/master_state.rb', line 74

def current_listener_pid
  @current_listener_pid
end

#exec_on_hupObject (readonly)

(Deprecated.) If true, on SIGHUP re-exec the master process before starting a new listener.



78
79
80
# File 'lib/resqued/master_state.rb', line 78

def exec_on_hup
  @exec_on_hup
end

#fast_exitObject (readonly)

If true, on SIGTERM/SIGQUIT/SIGINT, don’t wait for listeners to exit before the master exits.



82
83
84
# File 'lib/resqued/master_state.rb', line 82

def fast_exit
  @fast_exit
end

#last_good_listener_pidObject

The PID of the newest listener that booted successfully. This listener won’t be stopped until a newer listener boots successfully.



87
88
89
# File 'lib/resqued/master_state.rb', line 87

def last_good_listener_pid
  @last_good_listener_pid
end

#listener_statesObject (readonly)

A hash of pid => ListenerState for all running listeners.



93
94
95
# File 'lib/resqued/master_state.rb', line 93

def listener_states
  @listener_states
end

#listeners_createdObject

The number of listeners that have been created by this master.



90
91
92
# File 'lib/resqued/master_state.rb', line 90

def listeners_created
  @listeners_created
end

#pausedObject

Set to true when this master is paused and should not be running any listeners.



97
98
99
# File 'lib/resqued/master_state.rb', line 97

def paused
  @paused
end

#pidfileObject (readonly)

If set, the master’s PID will be written to this file.



100
101
102
# File 'lib/resqued/master_state.rb', line 100

def pidfile
  @pidfile
end

Instance Method Details

#init(options) ⇒ Object

Public: When starting fresh, from command-line options, assign the initial values.



12
13
14
15
16
17
# File 'lib/resqued/master_state.rb', line 12

def init(options)
  @config_paths = options.fetch(:config_paths)
  @exec_on_hup  = options.fetch(:exec_on_hup, false)
  @fast_exit    = options.fetch(:fast_exit, false)
  @pidfile      = options.fetch(:master_pidfile, nil)
end

#restore(data) ⇒ Object

Public: Restore state from a serialized form.



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

def restore(data)
  @config_paths = data[:config_paths]
  @current_listener_pid = data[:current_listener_pid]
  @exec_on_hup = data[:exec_on_hup]
  @fast_exit = data[:fast_exit]
  @last_good_listener_pid = data[:last_good_listener_pid]
  @listeners_created = data[:listeners_created]
  data[:listener_states].each do |lsh|
    @listener_states[lsh[:pid]] = ListenerState.new.tap do |ls|
      ls.master_socket = lsh[:master_socket] && Socket.for_fd(lsh[:master_socket])
      ls.options = lsh[:options]
      ls.pid = lsh[:pid]
      ls.worker_pids = lsh[:worker_pids]
    end
  end
  @paused = data[:paused]
  @pidfile = data[:pidfile]
end

#socketsObject

Public: Return an array of open sockets or other file handles that should be forwarded to a new master.



63
64
65
# File 'lib/resqued/master_state.rb', line 63

def sockets
  @listener_states.values.map { |l| l.master_socket }.compact
end

#to_hObject

Public: Return this state so that it can be serialized.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/resqued/master_state.rb', line 40

def to_h
  {
    config_paths: @config_paths,
    current_listener_pid: @current_listener_pid,
    exec_on_hup: @exec_on_hup,
    fast_exit: @fast_exit,
    last_good_listener_pid: @last_good_listener_pid,
    listeners_created: @listeners_created,
    listener_states: @listener_states.values.map { |ls|
      {
        master_socket: ls.master_socket&.to_i,
        options: ls.options,
        pid: ls.pid,
        worker_pids: ls.worker_pids,
      }
    },
    paused: @paused,
    pidfile: @pidfile,
  }
end