Module: Luban::Deployment::Service::Controller::Base

Included in:
Application::Controller, Luban::Deployment::Service::Controller
Defined in:
lib/luban/deployment/cli/service/controller.rb

Instance Method Summary collapse

Instance Method Details

#check_processObject



112
113
114
# File 'lib/luban/deployment/cli/service/controller.rb', line 112

def check_process
  update_result check_process!
end

#default_pending_intervalObject



171
# File 'lib/luban/deployment/cli/service/controller.rb', line 171

def default_pending_interval; 1; end

#default_pending_secondsObject



170
# File 'lib/luban/deployment/cli/service/controller.rb', line 170

def default_pending_seconds; 30; end

#kill_processObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/luban/deployment/cli/service/controller.rb', line 120

def kill_process
  if process_stopped?
    update_result "Skipped! Already stopped #{service_full_name}", status: :skipped
    return
  end

  unmonitor_process
  output = kill_process!
  if check_until { process_stopped? }
    update_result "Kill #{service_full_name}: [OK] #{output}"
  else
    update_result "Kill #{service_full_name}: [FAILED] #{output}"
  end
  remove_orphaned_pid_file
end

#monitor_commandObject



40
41
42
# File 'lib/luban/deployment/cli/service/controller.rb', line 40

def monitor_command
  @monitor_command ||= "#{monitor_executable} monitor #{service_entry}"
end

#monitor_executableObject



36
37
38
# File 'lib/luban/deployment/cli/service/controller.rb', line 36

def monitor_executable
  @monitor_executable ||= env_path.join("#{stage}.#{process_monitor[:env]}", 'bin', process_monitor[:name])
end

#monitor_processObject



140
141
142
143
144
145
146
147
148
# File 'lib/luban/deployment/cli/service/controller.rb', line 140

def monitor_process
  if process_monitor_defined?
    if monitor_process!
      info "Turned on process monitor for #{service_entry}"
    else
      info "Failed to turn on process monitor for #{service_entry}"
    end
  end
end

#pidObject



6
7
8
# File 'lib/luban/deployment/cli/service/controller.rb', line 6

def pid
  capture(:cat, "#{pid_file_path} 2>/dev/null")
end

#pid_file_exists?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/luban/deployment/cli/service/controller.rb', line 26

def pid_file_exists?
  file?(pid_file_path, "-s") # file is NOT zero size
end

#pid_file_missing?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/luban/deployment/cli/service/controller.rb', line 22

def pid_file_missing?
  process_started? and !pid_file_exists?
end

#pid_file_orphaned?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/luban/deployment/cli/service/controller.rb', line 18

def pid_file_orphaned?
  process_stopped? and pid_file_exists?
end

#process_monitor_defined?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/luban/deployment/cli/service/controller.rb', line 136

def process_monitor_defined?
  !process_monitor[:name].nil?
end

#process_started?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/luban/deployment/cli/service/controller.rb', line 10

def process_started?
  !!process_grep.keys.first
end

#process_stopped?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/luban/deployment/cli/service/controller.rb', line 14

def process_stopped?
  !process_started?
end

#reload_monitor_commandObject



48
49
50
# File 'lib/luban/deployment/cli/service/controller.rb', line 48

def reload_monitor_command
  @reload_monitor_command ||= "#{monitor_executable} reload"
end

#reload_monitor_processObject



160
161
162
163
164
165
166
167
168
# File 'lib/luban/deployment/cli/service/controller.rb', line 160

def reload_monitor_process
  if process_monitor_defined?
    if reload_monitor_process!
      info "Reloaded process monitor for #{service_entry}"
    else
      info "Failed to reload process monitor for #{service_entry}"
    end
  end
end

#restart_processObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/luban/deployment/cli/service/controller.rb', line 86

def restart_process
  if process_started?
    unmonitor_process
    output = stop_process!
    if check_until { process_stopped? }
      remove_orphaned_pid_file
      info "Stop #{service_full_name}: [OK] #{output}"
    else
      remove_orphaned_pid_file
      update_result "Stop #{service_full_name}: [FAILED] #{output}",
                    status: :failed, level: :error
      return
    end
  end

  output = start_process!
  if check_until { process_started? }
    update_result "Restart #{service_full_name}: [OK] #{output}"
    monitor_process
  else
    remove_orphaned_pid_file
    update_result "Restart #{service_full_name}: [FAILED] #{output}", 
                  status: :failed, level: :error
  end
end

#show_processObject



116
117
118
# File 'lib/luban/deployment/cli/service/controller.rb', line 116

def show_process
  update_result show_process!
end

#start_processObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/luban/deployment/cli/service/controller.rb', line 52

def start_process
  if process_started?
    update_result "Skipped! Already started #{service_full_name}", status: :skipped
    return
  end

  output = start_process!
  if check_until { process_started? }
    update_result "Start #{service_full_name}: [OK] #{output}"
    monitor_process
  else
    remove_orphaned_pid_file
    update_result "Start #{service_full_name}: [FAILED] #{output}", 
                  status: :failed, level: :error
  end
end

#stop_processObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/luban/deployment/cli/service/controller.rb', line 69

def stop_process
  if process_stopped?
    update_result "Skipped! Already stopped #{service_full_name}", status: :skipped
    return
  end

  unmonitor_process
  output = stop_process! || 'OK'
  if check_until { process_stopped? }
    update_result "Stop #{service_full_name}: [OK] #{output}"
  else
    update_result "Stop #{service_full_name}: [FAILED] #{output}",
                  status: :failed, level: :error
  end
  remove_orphaned_pid_file
end

#unmonitor_commandObject



44
45
46
# File 'lib/luban/deployment/cli/service/controller.rb', line 44

def unmonitor_command
  @unmonitor_command ||= "#{monitor_executable} unmonitor #{service_entry}"
end

#unmonitor_processObject



150
151
152
153
154
155
156
157
158
# File 'lib/luban/deployment/cli/service/controller.rb', line 150

def unmonitor_process
  if process_monitor_defined?
    if unmonitor_process!
      info "Turned off process monitor for #{service_entry}"
    else
      info "Failed to turn off process monitor for #{service_entry}"
    end
  end
end