Class: Fluent::ExecCronInput

Inherits:
Input
  • Object
show all
Defined in:
lib/fluent/plugin/in_exec_cron.rb

Constant Summary collapse

SUPPORTED_FORMAT =
{
  'tsv' => :tsv,
  'json' => :json,
  'msgpack' => :msgpack,
}

Instance Method Summary collapse

Constructor Details

#initializeExecCronInput

Returns a new instance of ExecCronInput.



5
6
7
8
9
10
11
# File 'lib/fluent/plugin/in_exec_cron.rb', line 5

def initialize
  super
  require 'fluent/plugin/exec_util'
  require 'fluent/timezone'
  require 'parse-cron'
  require 'erb'
end

Instance Method Details

#configure(conf) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/fluent/plugin/in_exec_cron.rb', line 40

def configure(conf)
  super

  if localtime = conf['localtime']
    @localtime = true
  elsif utc = conf['utc']
    @localtime = false
  end

  if conf['timezone']
    @timezone = conf['timezone']
    Fluent::Timezone.validate!(@timezone)
  end

  if !@tag && !@tag_key
    raise ConfigError, "'tag' or 'tag_key' option is required on exec input"
  end

  if @time_key
    if @time_format
      f = @time_format
      @time_parse_proc = Proc.new {|str| Time.strptime(str, f).to_i }
    else
      @time_parse_proc = Proc.new {|str| str.to_i }
    end
  end

  case @format
  when :tsv
    if @keys.empty?
      raise ConfigError, "keys option is required on exec input for tsv format"
    end
    @parser = ExecUtil::TSVParser.new(@keys, method(:on_message))
  when :json
    @parser = ExecUtil::JSONParser.new(method(:on_message))
  when :msgpack
    @parser = ExecUtil::MessagePackParser.new(method(:on_message))
  end

  begin
    @cron_parser = CronParser.new(@cron)
  rescue => e
    raise ConfigError, "invalid cron expression. [#{@cron}]"
  end
  @command = ERB.new(@command.gsub(/\$\{([^}]+)\}/, '<%= \1 %>'))
end

#run_periodicObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/fluent/plugin/in_exec_cron.rb', line 101

def run_periodic
  until @finished
    begin
      secs = @cron_parser.next(Time.now) - Time.now
      sleep secs
      io = IO.popen(@command.result(binding), "r")
      @parser.call(io)
      Process.waitpid(io.pid)
    rescue
      log.error "exec failed to run or shutdown child process", :error => $!.to_s, :error_class => $!.class.to_s
      log.warn_backtrace $!.backtrace
    end
  end
end

#shutdownObject



92
93
94
95
96
97
98
99
# File 'lib/fluent/plugin/in_exec_cron.rb', line 92

def shutdown
  @finished = true
  if @graceful_shutdown
    @thread.join
  else
    Thread.kill(@thread)
  end
end

#startObject



87
88
89
90
# File 'lib/fluent/plugin/in_exec_cron.rb', line 87

def start
  @finished = false
  @thread = Thread.new(&method(:run_periodic))
end