Class: Fluent::Ctl

Inherits:
Object
  • Object
show all
Includes:
Windows::ServiceConstants, Windows::ServiceFunctions, Windows::ServiceStructs
Defined in:
lib/fluent/command/ctl.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{}
COMMAND_MAP =
{
  shutdown: :TERM,
  restart: :HUP,
  flush: :USR1,
  reload: :USR2,
  dump: :CONT,
}
WINSVC_CONTROL_CODE_MAP =
{
  shutdown: SERVICE_CONTROL_STOP,
  # 128 - 255: user-defined control code
  # See https://docs.microsoft.com/en-us/windows/win32/api/winsvc/nf-winsvc-controlservice
  restart: 128,
  flush: 129,
  reload: SERVICE_CONTROL_PARAMCHANGE,
  dump: 130,
}

Instance Method Summary collapse

Constructor Details

#initialize(argv = ARGV) ⇒ Ctl

Returns a new instance of Ctl.



60
61
62
63
64
65
66
67
# File 'lib/fluent/command/ctl.rb', line 60

def initialize(argv = ARGV)
  @argv = argv
  @options = {}
  @opt_parser = OptionParser.new
  configure_option_parser
  @options.merge!(DEFAULT_OPTIONS)
  parse_options!
end

Instance Method Details

#callObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/fluent/command/ctl.rb', line 93

def call
  if Fluent.windows?
    if /^[0-9]+$/.match?(@pid_or_svcname)
      # Use as PID
      return call_windows_event(@command, "fluentd_#{@pid_or_svcname}")
    end

    unless call_winsvc_control_code(@command, @pid_or_svcname)
      puts "Cannot send control code to #{@pid_or_svcname} service, try to send an event with this name ..."
      call_windows_event(@command, @pid_or_svcname)
    end
  else
    call_signal(@command, @pid_or_svcname)
  end
end

#help_textObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/fluent/command/ctl.rb', line 69

def help_text
  text = "\n"
  if Fluent.windows?
    text << "Usage: #{$PROGRAM_NAME} COMMAND [PID_OR_SVCNAME]\n"
  else
    text << "Usage: #{$PROGRAM_NAME} COMMAND PID\n"
  end
  text << "\n"
  text << "Commands: \n"
  COMMAND_MAP.each do |key, value|
    text << "  #{key}\n"
  end
  text
end

#usage(msg = nil) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/fluent/command/ctl.rb', line 84

def usage(msg = nil)
  puts help_text
  if msg
    puts
    puts "Error: #{msg}"
  end
  exit 1
end