module Luban::Deployment::Service::Controller::Base

Public Instance Methods

check_monitor_status(output: :info) click to toggle source
# File lib/luban/deployment/cli/service/controller.rb, line 170
def check_monitor_status(output: :info)
  send(output, check_monitor_status!)
end
check_process() click to toggle source
# File lib/luban/deployment/cli/service/controller.rb, line 106
def check_process
  update_result check_process!
end
default_pending_interval() click to toggle source
# File lib/luban/deployment/cli/service/controller.rb, line 175
def default_pending_interval; 1; end
default_pending_seconds() click to toggle source
# File lib/luban/deployment/cli/service/controller.rb, line 174
def default_pending_seconds; 30; end
kill_process() click to toggle source
# File lib/luban/deployment/cli/service/controller.rb, line 114
def kill_process
  if process_stopped?
    update_result "Skipped! Already stopped #{service_full_name}", status: :skipped
    return
  end

  unmonitor_process if monitorable?
  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_check() click to toggle source
# File lib/luban/deployment/cli/service/controller.rb, line 142
def monitor_check
  check_monitor_status(output: :update_result)
end
monitor_control_file_path() click to toggle source
# File lib/luban/deployment/cli/service/controller.rb, line 41
def monitor_control_file_path
  @monitor_control_file_path ||= env_path.join(process_monitor[:env], 'shared', 'profile',
                                               process_monitor[:name], monitor_control_file_name)
end
monitor_executable() click to toggle source
# File lib/luban/deployment/cli/service/controller.rb, line 36
def monitor_executable
  @monitor_executable ||= env_path.join(process_monitor[:env], 'bin', 
                                        process_monitor[:name])
end
monitor_off() click to toggle source
# File lib/luban/deployment/cli/service/controller.rb, line 134
def monitor_off
  unmonitor_process(output: :update_result)
end
monitor_on() click to toggle source
# File lib/luban/deployment/cli/service/controller.rb, line 130
def monitor_on
  monitor_process(output: :update_result)
end
monitor_process(output: :info) click to toggle source
# File lib/luban/deployment/cli/service/controller.rb, line 146
def monitor_process(output: :info)
  if monitor_process!
    send(output, "Turned on process monitor for #{service_entry}")
  else
    send(output, "Failed to turn on process monitor for #{service_entry}")
  end
end
monitor_reload() click to toggle source
# File lib/luban/deployment/cli/service/controller.rb, line 138
def monitor_reload
  reload_monitor_process(output: :update_result)
end
pid() click to toggle source
# File lib/luban/deployment/cli/service/controller.rb, line 6
def pid
  capture(:cat, "#{pid_file_path} 2>/dev/null")
end
pid_file_exists?() click to toggle source
# 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?() click to toggle source
# File lib/luban/deployment/cli/service/controller.rb, line 22
def pid_file_missing?
  process_started? and !pid_file_exists?
end
pid_file_orphaned?() click to toggle source
# File lib/luban/deployment/cli/service/controller.rb, line 18
def pid_file_orphaned?
  process_stopped? and pid_file_exists?
end
process_started?() click to toggle source
# File lib/luban/deployment/cli/service/controller.rb, line 10
def process_started?
  !!process_grep.keys.first
end
process_stopped?() click to toggle source
# File lib/luban/deployment/cli/service/controller.rb, line 14
def process_stopped?
  !process_started?
end
reload_monitor_process(output: :info) click to toggle source
# File lib/luban/deployment/cli/service/controller.rb, line 162
def reload_monitor_process(output: :info)
  if reload_monitor_process!
    send(output, "Reloaded process monitor for #{service_entry}")
  else
    send(output, "Failed to reload process monitor for #{service_entry}")
  end
end
restart_process() click to toggle source
# File lib/luban/deployment/cli/service/controller.rb, line 80
def restart_process
  if process_started?
    unmonitor_process if monitorable?
    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 if monitorable?
  else
    remove_orphaned_pid_file
    update_result "Restart #{service_full_name}: [FAILED] #{output}", 
                  status: :failed, level: :error
  end
end
show_process() click to toggle source
# File lib/luban/deployment/cli/service/controller.rb, line 110
def show_process
  update_result show_process!
end
start_process() click to toggle source
# File lib/luban/deployment/cli/service/controller.rb, line 46
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 if monitorable?
  else
    remove_orphaned_pid_file
    update_result "Start #{service_full_name}: [FAILED] #{output}", 
                  status: :failed, level: :error
  end
end
stop_process() click to toggle source
# File lib/luban/deployment/cli/service/controller.rb, line 63
def stop_process
  if process_stopped?
    update_result "Skipped! Already stopped #{service_full_name}", status: :skipped
    return
  end

  unmonitor_process if monitorable?
  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_process(output: :info) click to toggle source
# File lib/luban/deployment/cli/service/controller.rb, line 154
def unmonitor_process(output: :info)
  if unmonitor_process!
    send(output, "Turned off process monitor for #{service_entry}")
  else
    send(output, "Failed to turn off process monitor for #{service_entry}")
  end
end

Protected Instance Methods

check_monitor_status!() click to toggle source
# File lib/luban/deployment/cli/service/controller.rb, line 253
def check_monitor_status!
  capture(check_monitor_command(service_entry, summary: task.opts.summary))
end
check_process!() click to toggle source
# File lib/luban/deployment/cli/service/controller.rb, line 206
def check_process!
  if pid_file_missing?
    "#{service_full_name}: started but PID file(s) do NOT exist in #{pids_path}"
  elsif process_started?
    "#{service_full_name}: started - PID(s) #{pid}"
  elsif pid_file_orphaned?
    "#{service_full_name}: stopped but PID file(s) exist in #{pids_path}"
  else
    "#{service_full_name}: stopped"
  end
end
check_until(pending_seconds: default_pending_seconds, pending_interval: default_pending_interval) { || ... } click to toggle source
# File lib/luban/deployment/cli/service/controller.rb, line 193
def check_until(pending_seconds: default_pending_seconds, 
                pending_interval: default_pending_interval)
  succeeded = false
  (pending_seconds/pending_interval).times do
    sleep pending_interval
    break if (succeeded = yield)
  end
  succeeded
end
init() click to toggle source
Calls superclass method
# File lib/luban/deployment/cli/service/controller.rb, line 179
def init
  super
  load_process_monitor_commands if monitorable?
end
kill_process!(pattern = process_pattern) click to toggle source
# File lib/luban/deployment/cli/service/controller.rb, line 218
def kill_process!(pattern = process_pattern)
  capture(:pkill, "-9 -f \"#{pattern}\"")
end
load_process_monitor_commands() click to toggle source
# File lib/luban/deployment/cli/service/controller.rb, line 184
def load_process_monitor_commands
  singleton_class.send(:include, 
    process_monitor_module.const_get("Controller::Commands::Public"))
end
monitor_process!() click to toggle source
# File lib/luban/deployment/cli/service/controller.rb, line 241
def monitor_process!
  test(monitor_command(service_entry))
end
process_grep(pattern = process_pattern) click to toggle source
# File lib/luban/deployment/cli/service/controller.rb, line 222
def process_grep(pattern = process_pattern)
  capture(:pgrep, "-l -f -a \"#{pattern}\" 2>/dev/null").split("\n").inject({}) do |h, p|
    pid, pname = p.split(' ', 2)
    h[pid] = pname
    h
  end
end
process_monitor_module() click to toggle source
# File lib/luban/deployment/cli/service/controller.rb, line 189
def process_monitor_module
  Luban::Deployment::Package::Base.package_class(process_monitor[:name])
end
reload_monitor_process!() click to toggle source
# File lib/luban/deployment/cli/service/controller.rb, line 249
def reload_monitor_process!
  test(reload_monitor_command)
end
remove_orphaned_pid_file() click to toggle source
# File lib/luban/deployment/cli/service/controller.rb, line 237
def remove_orphaned_pid_file
  rm(pid_file_path) if pid_file_orphaned?
end
show_process!() click to toggle source
# File lib/luban/deployment/cli/service/controller.rb, line 230
def show_process!
  result = process_grep.inject("") do |s, (pid, cmd)|
             s += "#{pid} : #{cmd}\n"
           end
  result.empty? ? "No processes are found up and running." : result
end
start_process!() click to toggle source
# File lib/luban/deployment/cli/service/controller.rb, line 203
def start_process!; capture(start_command); end
stop_process!() click to toggle source
# File lib/luban/deployment/cli/service/controller.rb, line 204
def stop_process!; capture(stop_command); end
unmonitor_process!() click to toggle source
# File lib/luban/deployment/cli/service/controller.rb, line 245
def unmonitor_process!
  test(unmonitor_command(service_entry))
end