Module: Cognizant::Process::Actions::Restart

Included in:
Cognizant::Process::Actions
Defined in:
lib/cognizant/process/actions/restart.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#restart_after_commandString

The command to run after the process is restarted.

Returns:

  • (String)

    Defaults to nil



52
53
54
# File 'lib/cognizant/process/actions/restart.rb', line 52

def restart_after_command
  @restart_after_command
end

#restart_before_commandString

The command to run before the process is restarted. The exit status of this command determines whether or not to proceed.

Returns:

  • (String)

    Defaults to nil



12
13
14
# File 'lib/cognizant/process/actions/restart.rb', line 12

def restart_before_command
  @restart_before_command
end

#restart_commandString

The command to restart the process with. This command can optionally be similar in behavior to the stop command, since the process will anyways be automatically started again, if autostart is set to true. Also see restart_expect variable.

Returns:

  • (String)

    Defaults to nil



19
20
21
# File 'lib/cognizant/process/actions/restart.rb', line 19

def restart_command
  @restart_command
end

#restart_envHash

Environment variables for process during restart.

Returns:

  • (Hash)

    Defaults to {}



7
8
9
# File 'lib/cognizant/process/actions/restart.rb', line 7

def restart_env
  @restart_env
end

#restart_expect_stoppedtrue, false

Whether or not the process is expected to stop itself after the restart action is executed. If, upon restart action, the process will stop but not start again by itself, it should be set to true. If the process will start again within the timeout period, it should be set to false. For convenience, it defaults to false, if restart_command or restart_signals are set, as the restart action is then expected to start itself after a stop. !restart_signals.present?

Returns:

  • (true, false)

    Defaults to !restart_command.present? ||



39
40
41
# File 'lib/cognizant/process/actions/restart.rb', line 39

def restart_expect_stopped
  @restart_expect_stopped
end

#restart_signalsArray

The signals to pass to the process one by one attempting to restart it. Each signal is passed within the timeout period over equally divided intervals (min. 2 seconds). Override with signals without “KILL” to never force kill the process. Also see restart_expect variable. e.g. [“TERM”, “INT”]

Returns:

  • (Array)

    Defaults to [“QUIT”, “TERM”, “INT”]



28
29
30
# File 'lib/cognizant/process/actions/restart.rb', line 28

def restart_signals
  @restart_signals
end

#restart_timeoutString

The grace time period in seconds for the process to stop within (for restart). Covers the time period for the restart command or signals. After the timeout is over, the process is checked for running status and if not stopped, it re-enters the auto start lifecycle based on conditions. Timeout of request has the same effect as :stopped setting :restart_expect.

Returns:

  • (String)

    Defaults to 30



48
49
50
# File 'lib/cognizant/process/actions/restart.rb', line 48

def restart_timeout
  @restart_timeout
end

Instance Method Details

#_restart_result_handler(result, time_left = 0) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/cognizant/process/actions/restart.rb', line 92

def _restart_result_handler(result, time_left = 0)
  # If it is a boolean and value is true OR if it's an execution result and it succeeded.
  if (!!result == result and result) or (result.respond_to?(:succeeded?) and result.succeeded?)
    unlink_pid if not pid_running? and self.daemonize

    # We are not resetting @process_pid here to give process a second of grace period.

    unless self.restart_expect_stopped
      while (time_left >= 0 and not process_running?) do
        sleep 1
        time_left -= 1
        @process_pid = nil
      end
    end
  else
    @process_pid = nil
  end

  # Rollback the pending skips.
  skip_ticks_for(-time_left) if time_left > 0
end

#reset_attributes!Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/cognizant/process/actions/restart.rb', line 65

def reset_attributes!
  self.restart_env = {}
  self.restart_before_command = nil
  self.restart_command = nil
  self.restart_signals = nil
  self.restart_expect_stopped = nil
  self.restart_timeout = 30
  self.restart_after_command = nil
  super
end

#restart_expect_stopped!Object



54
55
56
# File 'lib/cognizant/process/actions/restart.rb', line 54

def restart_expect_stopped!
  self.restart_expect_stopped = true
end

#restart_processObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/cognizant/process/actions/restart.rb', line 76

def restart_process
  # We skip so that we're not reinformed about the required transition by the tick.
  skip_ticks_for(self.restart_timeout)

  options = {
    env:      self.env.merge(self.restart_env),
    before:   self.restart_before_command,
    command:  self.restart_command,
    signals:  self.restart_signals || ["QUIT", "TERM", "INT"],
    after:    self.restart_after_command,
    timeout:  self.restart_timeout
  }
  handle_action('_restart_result_handler', options)
end