Module: Fluent::PluginHelper::Parser

Defined in:
lib/fluent/plugin_helper/parser.rb

Defined Under Namespace

Modules: ParserParams

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#_parsersObject (readonly)

for tests



74
75
76
# File 'lib/fluent/plugin_helper/parser.rb', line 74

def _parsers
  @_parsers
end

Class Method Details

.included(mod) ⇒ Object



70
71
72
# File 'lib/fluent/plugin_helper/parser.rb', line 70

def self.included(mod)
  mod.include ParserParams
end

Instance Method Details

#after_shutdownObject



129
130
131
132
# File 'lib/fluent/plugin_helper/parser.rb', line 129

def after_shutdown
  parser_operate(:after_shutdown)
  super
end

#before_shutdownObject



119
120
121
122
# File 'lib/fluent/plugin_helper/parser.rb', line 119

def before_shutdown
  parser_operate(:before_shutdown)
  super
end

#closeObject



134
135
136
137
# File 'lib/fluent/plugin_helper/parser.rb', line 134

def close
  parser_operate(:close)
  super
end

#configure(conf) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/fluent/plugin_helper/parser.rb', line 82

def configure(conf)
  super

  @parser_configs.each do |section|
    if @_parsers[section.usage]
      raise Fluent::ConfigError, "duplicated parsers configured: #{section.usage}"
    end
    parser = Plugin.new_parser(section[:@type], parent: self)
    parser.configure(section.corresponding_config_element)
    @_parsers[section.usage] = parser
  end
end

#initializeObject



76
77
78
79
80
# File 'lib/fluent/plugin_helper/parser.rb', line 76

def initialize
  super
  @_parsers_started = false
  @_parsers = {} # usage => parser
end

#parser_create(usage: '', type: nil, conf: nil, default_type: nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/fluent/plugin_helper/parser.rb', line 25

def parser_create(usage: '', type: nil, conf: nil, default_type: nil)
  parser = @_parsers[usage]
  return parser if parser && !type && !conf

  type = if type
           type
         elsif conf && conf.respond_to?(:[])
           raise Fluent::ConfigError, "@type is required in <parse>" unless conf['@type']
           conf['@type']
         elsif default_type
           default_type
         else
           raise ArgumentError, "BUG: both type and conf are not specified"
         end
  parser = Fluent::Plugin.new_parser(type, parent: self)
  config = case conf
           when Fluent::Config::Element
             conf
           when Hash
             # in code, programmer may use symbols as keys, but Element needs strings
             conf = Hash[conf.map{|k,v| [k.to_s, v]}]
             Fluent::Config::Element.new('parse', usage, conf, [])
           when nil
             Fluent::Config::Element.new('parse', usage, {}, [])
           else
             raise ArgumentError, "BUG: conf must be a Element, Hash (or unspecified), but '#{conf.class}'"
           end
  parser.configure(config)
  if @_parsers_started
    parser.start
  end

  @_parsers[usage] = parser
  parser
end

#parser_operate(method_name, &block) ⇒ Object



103
104
105
106
107
108
109
110
111
112
# File 'lib/fluent/plugin_helper/parser.rb', line 103

def parser_operate(method_name, &block)
  @_parsers.each_pair do |usage, parser|
    begin
      parser.__send__(method_name)
      block.call(parser) if block_given?
    rescue => e
      log.error "unexpected error while #{method_name}", usage: usage, parser: parser, error: e
    end
  end
end

#shutdownObject



124
125
126
127
# File 'lib/fluent/plugin_helper/parser.rb', line 124

def shutdown
  parser_operate(:shutdown)
  super
end

#startObject



95
96
97
98
99
100
101
# File 'lib/fluent/plugin_helper/parser.rb', line 95

def start
  super
  @_parsers_started = true
  @_parsers.each_pair do |usage, parser|
    parser.start
  end
end

#stopObject



114
115
116
117
# File 'lib/fluent/plugin_helper/parser.rb', line 114

def stop
  super
  parser_operate(:stop)
end

#terminateObject



139
140
141
142
143
144
# File 'lib/fluent/plugin_helper/parser.rb', line 139

def terminate
  parser_operate(:terminate)
  @_parsers_started = false
  @_parsers = {}
  super
end