Class: Resqued::ReplaceMaster

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

Class Method Summary collapse

Class Method Details

.exec!(state) ⇒ Object

Public: Replace the current master process with a new one, while preserving state.



7
8
9
# File 'lib/resqued/replace_master.rb', line 7

def self.exec!(state)
  exec Resqued::START_CTX["$0"], "--replace", store_state(state), exec_opts(state)
end

.exec_opts(state) ⇒ Object

Internal: Returns exec options for each open socket in ‘state’.



12
13
14
15
16
17
18
19
20
21
# File 'lib/resqued/replace_master.rb', line 12

def self.exec_opts(state)
  exec_opts = {}
  state.sockets.each do |sock|
    exec_opts[sock.to_i] = sock
  end
  if pwd = Resqued::START_CTX["pwd"]
    exec_opts[:chdir] = pwd
  end
  return exec_opts
end

.restore_state(state, path) ⇒ Object

Internal: Restore the master’s state, and remove the state file.



36
37
38
39
40
41
# File 'lib/resqued/replace_master.rb', line 36

def self.restore_state(state, path)
  data = YAML.safe_load(File.read(path), permitted_classes: [Symbol], aliases: true)
  Resqued::START_CTX.replace(data[:start_ctx] || {})
  state.restore(data[:state])
  File.unlink(path) rescue nil
end

.store_state(state) ⇒ Object

Internal: Write out current state to a file, so that a new master can pick up from where we left off.



24
25
26
27
28
29
30
31
32
33
# File 'lib/resqued/replace_master.rb', line 24

def self.store_state(state)
  data = { version: Resqued::VERSION }
  data[:start_ctx] = Resqued::START_CTX
  data[:state] = state.to_h

  f = Tempfile.create "resqued-state"
  f.write(YAML.dump(data))
  f.close
  return f.path
end