module Splash::Daemon::Controller

Daemon Controller Module

Public Instance Methods

startdaemon(options = {}) click to toggle source

Start the Splash Daemon @param [Hash] options @option options [Symbol] :quiet activate quiet mode for log (limit to :fatal) @option options [Symbol] :foreground run daemon in foreground @option options [Symbol] :purge clean input queue for daemon on configured transport @return [Hash] Exiter Case (:quiet_exit, :already_exist, :unknown_error or other)

# File lib/splash/daemon/controller.rb, line 25
def startdaemon(options = {})
  config = get_config
  log = get_logger
  log.level = :fatal if options[:quiet]
  unless verify_service url: config.prometheus_pushgateway_url then
    return {:case => :service_dependence_missing, :more => 'Prometheus Gateway'}
  end
  realpid = get_processes pattern: get_config.daemon_process_name
  foreground  = get_processes patterns: [ "splash", "foreground" ]
  unless foreground.empty? or options[:foreground] then
    return {:case => :already_exist, :more => "Splash Process already launched on foreground "}
  end

  unless File::exist? config.daemon_full_pid_path then
    unless realpid.empty? then
      return {:case => :already_exist, :more => "Splash Process already launched "}
    end
    if options[:purge] then
      transport = get_default_client
      if transport.class == Hash  and transport.include? :case then
        splash_exit transport
      else
        queue = "splash.#{Socket.gethostname}.input"
        transport.purge queue: queue
        log.info "Queue : #{queue} purged"
      end
    end
    daemon_config = {:description => config.daemon_process_name,
        :pid_file => config.daemon_full_pid_path,
        :stdout_trace => config.daemon_full_stdout_trace_path,
        :stderr_trace => config.daemon_full_stderr_trace_path,
        :foreground => options[:foreground]
      }

    ["int","term","hup"].each do |type| daemon_config["sig#{type}_handler".to_sym] = Proc::new {  ObjectSpace.each_object(Splash::Daemon::Orchestrator::Scheduler).first.terminate } end
    res = daemonize daemon_config do
        Scheduler::new options
    end
    sleep 1
    if res == 0 then
      pid = `cat #{config.daemon_full_pid_path}`.to_i
      log.ok "Splash Daemon Started, with PID : #{pid}"
      return {:case => :quiet_exit, :more => "Splash Daemon successfully loaded."}
    else
      return {:case => :unknown_error, :more => "Splash Daemon loading error, see logs for more details."}
    end

  else
    return {:case => :already_exist, :more => "Pid File, please verify if Splash daemon is running."}
  end
end
statusdaemon(options = {}) click to toggle source

Status of the Splash Daemon, display status @param [Hash] options ignored @return [Hash] Exiter Case (:status_ko, :status_ok)

# File lib/splash/daemon/controller.rb, line 103
def statusdaemon(options = {})
  log = get_logger
  config = get_config
  pid = realpid = ''
  pid = `cat #{config.daemon_full_pid_path}`.to_s if File.exist?(config.daemon_full_pid_path)
  listpid = get_processes({ :pattern => get_config.daemon_process_name})
  pid.chomp!
  if listpid.empty? then
    realpid = ''
  else
    realpid = listpid.first
  end
  unless realpid.empty? then
    log.item "Splash Process is running with PID #{realpid} "
  else
    log.item 'Splash Process not found '
  end
  unless pid.empty? then
    log.item "and PID file exist with PID #{pid}"
  else
    log.item "and PID file don't exist"
  end
  if pid == realpid then
    return {:case => :status_ok }
  elsif pid.empty? then
    return {:case => :status_ko, :more => "PID File error, you have to kill process manualy, with : '(sudo )kill -TERM #{realpid}'"}
  elsif realpid.empty? then
    return {:case => :status_ko, :more => "Process Splash Dameon missing, run 'splash daemon stop' to reload properly"}
  end
end
stopdaemon(options = {}) click to toggle source

Stop the Splash Daemon @param [Hash] options @option options [Symbol] :quiet activate quiet mode for log (limit to :fatal) @return [Hash] Exiter Case (:quiet_exit, :not_found, other)

# File lib/splash/daemon/controller.rb, line 81
def stopdaemon(options = {})
    config = get_config
    log = get_logger
    log.level = :fatal if options[:quiet]
    if File.exist?(config.daemon_full_pid_path) then
      begin
        pid = `cat #{config.daemon_full_pid_path}`.to_i
        Process.kill("TERM", pid)
        acase = {:case => :quiet_exit, :more => 'Splash stopped succesfully'}
      rescue Errno::ESRCH
        acase =  {:case => :not_found, :more => "Process of PID : #{pid} not found"}
      end
      FileUtils::rm config.daemon_full_pid_path if File::exist? config.daemon_full_pid_path
    else
      acase =  {:case => :not_found, :more => "Splash is not running"}
    end
    return acase
end