require 'flydata/command/base' require 'flydata/helper/server' require 'flydata/helper/config_parser' require 'flydata/util/shell' module Flydata module Command class Helper < Base DEFAULT_OPTIONS = { #daemon daemonize: false, supervisor: true, workers: 2, #logger log: nil, log_level: 'info', log_rotate_age: 10, log_rotate_size: 10*1024*1024 } def self.slop_start Slop.new do on 'c=', 'config=','config file path' on 'n', 'no-daemon', 'Start Helper as a regular program' on 'l=', 'log=', 'log file path' on 'e=', 'level=', 'log level' on 'r=', 'rotate=', 'Number of times logs are rotated before being removed' on 's=', 'size=', 'Size (in bytes) after which log is rotated' #For now, size needs to be specified in bytes (dont think it will be used often) end end def initialize(options = Slop.new) super @helper_config = Flydata::Helper::ConfigParser.parse_file(opts[:config])[:helper] create_helper_dirs end def start if running? log_info_stdout("Helper is already running.") else raw_start end end def stop if running? log_info_stdout("Stopping Helper.") run_command(stop_cmd) else log_info_stdout("Helper is not running.") end end def status if running? log_info_stdout("Helper is running.") else log_info_stdout("Helper is not running.") end end def restart if running? log_info_stdout("Restarting Helper.") run_command(kill_hup_cmd) else raw_start end end private def create_config_with_args Hash.new.tap do |c| c[:config_path] = opts[:config] c[:log] = opts[:log] ? opts[:log] : @helper_config.helper_log_path unless opts.no_daemon? c[:pid_path] = @helper_config.helper_pid_path c[:daemonize] = true end c[:log_level] = opts[:level] if opts[:level] c[:log_rotate_age] = opts[:rotate].to_i if opts[:rotate] c[:log_rotate_size] = opts[:size].to_i if opts[:size] end end def run_command(cmd) o, e, s = Util::Shell.run_cmd(cmd) log_error_stderr(e) if not e.to_s.empty? success = (s.to_i == 0) if success log_info_stdout("Done") else log_error_stderr("Failed") end success end def running? `#{running_cmd}`.to_i > 0 end def stop_cmd <