Class: Fluent::EngineClass

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/engine.rb

Defined Under Namespace

Classes: NoMatchMatch

Constant Summary collapse

MATCH_CACHE_SIZE =
1024

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEngineClass

Returns a new instance of EngineClass.



22
23
24
25
26
27
28
29
# File 'lib/fluent/engine.rb', line 22

def initialize
  @matches = []
  @sources = []
  @match_cache = {}
  @match_cache_keys = []
  @started = []
  @default_loop = nil
end

Instance Attribute Details

#matchesObject (readonly)

Returns the value of attribute matches.



33
34
35
# File 'lib/fluent/engine.rb', line 33

def matches
  @matches
end

#sourcesObject (readonly)

Returns the value of attribute sources.



33
34
35
# File 'lib/fluent/engine.rb', line 33

def sources
  @sources
end

Instance Method Details

#configure(conf) ⇒ Object



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
86
87
88
89
90
91
92
93
94
# File 'lib/fluent/engine.rb', line 60

def configure(conf)
  $log.info "using configuration file: #{conf.to_s.rstrip}"

  conf.elements.select {|e|
    e.name == 'source'
  }.each {|e|
    type = e['type']
    unless type
      raise ConfigError, "Missing 'type' parameter on <source> directive"
    end
    $log.info "adding source type=#{type.dump}"

    input = Plugin.new_input(type)
    input.configure(e)

    @sources << input
  }

  conf.elements.select {|e|
    e.name == 'match'
  }.each {|e|
    type = e['type']
    pattern = e.arg
    unless type
      raise ConfigError, "Missing 'type' parameter on <match #{e.arg}> directive"
    end
    $log.info "adding match", :pattern=>pattern, :type=>type

    output = Plugin.new_output(type)
    output.configure(e)

    match = Match.new(pattern, output)
    @matches << match
  }
end

#emit(tag, time, record) ⇒ Object



100
101
102
# File 'lib/fluent/engine.rb', line 100

def emit(tag, time, record)
  emit_stream tag, OneEventStream.new(time, record)
end

#emit_array(tag, array) ⇒ Object



104
105
106
# File 'lib/fluent/engine.rb', line 104

def emit_array(tag, array)
  emit_stream tag, ArrayEventStream.new(array)
end

#emit_stream(tag, es) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/fluent/engine.rb', line 108

def emit_stream(tag, es)
  target = @match_cache[tag]
  unless target
    target = match(tag) || NoMatchMatch.new
    # this is not thread-safe but inconsistency doesn't
    # cause serious problems while locking causes.
    if @match_cache_keys.size >= MATCH_CACHE_SIZE
      @match_cache_keys.delete @match_cache_keys.shift
    end
    @match_cache[tag] = target
    @match_cache_keys << tag
  end
  target.emit(tag, es)
rescue
  $log.warn "emit transaction failed ", :error=>$!.to_s
  $log.warn_backtrace
  raise
end

#flush!Object



135
136
137
# File 'lib/fluent/engine.rb', line 135

def flush!
  flush_recursive(@matches)
end

#initObject



35
36
37
38
39
40
41
42
43
# File 'lib/fluent/engine.rb', line 35

def init
  BasicSocket.do_not_reverse_lookup = true
  Plugin.load_plugins
  if defined?(Encoding)
    Encoding.default_internal = 'ASCII-8BIT' if Encoding.respond_to?(:default_internal)
    Encoding.default_external = 'ASCII-8BIT' if Encoding.respond_to?(:default_external)
  end
  self
end

#load_plugin_dir(dir) ⇒ Object



96
97
98
# File 'lib/fluent/engine.rb', line 96

def load_plugin_dir(dir)
  Plugin.load_plugin_dir(dir)
end

#match(tag) ⇒ Object



127
128
129
# File 'lib/fluent/engine.rb', line 127

def match(tag)
  @matches.find {|m| m.match(tag) }
end

#match?(tag) ⇒ Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/fluent/engine.rb', line 131

def match?(tag)
  !!match(tag)
end

#nowObject



139
140
141
142
# File 'lib/fluent/engine.rb', line 139

def now
  # TODO thread update
  Time.now.to_i
end

#parse_config(io, fname, basepath = Dir.pwd) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/fluent/engine.rb', line 52

def parse_config(io, fname, basepath=Dir.pwd)
  conf = Config.parse(io, fname, basepath)
  configure(conf)
  conf.check_not_fetched {|key,e|
    $log.warn "parameter '#{key}' in #{e.to_s.strip} is not used."
  }
end

#read_config(path) ⇒ Object



45
46
47
48
49
50
# File 'lib/fluent/engine.rb', line 45

def read_config(path)
  $log.info "reading config file", :path=>path
  File.open(path) {|io|
    parse_config(io, File.basename(path), File.dirname(path))
  }
end

#runObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/fluent/engine.rb', line 144

def run
  begin
    start

    if match?($log.tag)
      $log.enable_event
    end

    # for empty loop
    @default_loop = Coolio::Loop.default
    @default_loop.attach Coolio::TimerWatcher.new(1, true)
    # TODO attach async watch for thread pool
    @default_loop.run

  rescue
    $log.error "unexpected error", :error=>$!.to_s
    $log.error_backtrace
  ensure
    shutdown
  end
end

#stopObject



166
167
168
169
170
171
172
173
# File 'lib/fluent/engine.rb', line 166

def stop
  $log.info "shutting down fluentd"
  if @default_loop
    @default_loop.stop
    @default_loop = nil
  end
  nil
end