Skip to content

Commit

Permalink
Merge pull request #843 from fluent/introduce-plugin-helpers
Browse files Browse the repository at this point in the history
add new Plugin::Base class and PluginHelper modules
  • Loading branch information
tagomoris committed Mar 23, 2016
2 parents 33ab2c5 + 942b04b commit b19678e
Show file tree
Hide file tree
Showing 17 changed files with 1,696 additions and 20 deletions.
4 changes: 4 additions & 0 deletions lib/fluent/log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,10 @@ def flush
@out.flush
end

def reset
@out.reset if @out.respond_to?(:reset)
end

private

def dump_stacktrace(backtrace, level)
Expand Down
38 changes: 19 additions & 19 deletions lib/fluent/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
require 'fluent/config/error'

module Fluent
class Plugin
module Plugin
SEARCH_PATHS = []

# plugins for fluentd: fluent/plugin/type_NAME.rb
Expand All @@ -35,24 +35,6 @@ class Plugin

REGISTRIES = [INPUT_REGISTRY, OUTPUT_REGISTRY, FILTER_REGISTRY, BUFFER_REGISTRY, PARSER_REGISTRY, FORMATTER_REGISTRY]

def self.lookup_type_from_class(klass_or_its_name)
klass = if klass_or_its_name.is_a? Class
klass_or_its_name
elsif klass_or_its_name.is_a? String
eval(klass_or_its_name) # const_get can't handle qualified klass name (ex: A::B)
else
raise ArgumentError, "invalid argument type #{klass_or_its_name.class}: #{klass_or_its_name}"
end
REGISTRIES.reduce(nil){|a, r| a || r.reverse_lookup(klass) }
end

def self.add_plugin_dir(dir)
REGISTRIES.each do |r|
r.paths.push(dir)
end
nil
end

def self.register_input(type, klass)
register_impl('input', INPUT_REGISTRY, type, klass)
end
Expand Down Expand Up @@ -89,6 +71,24 @@ def self.register_formatter(type, klass_or_proc)
end
end

def self.lookup_type_from_class(klass_or_its_name)
klass = if klass_or_its_name.is_a? Class
klass_or_its_name
elsif klass_or_its_name.is_a? String
eval(klass_or_its_name) # const_get can't handle qualified klass name (ex: A::B)
else
raise ArgumentError, "invalid argument type #{klass_or_its_name.class}: #{klass_or_its_name}"
end
REGISTRIES.reduce(nil){|a, r| a || r.reverse_lookup(klass) }
end

def self.add_plugin_dir(dir)
REGISTRIES.each do |r|
r.paths.push(dir)
end
nil
end

def self.new_input(type)
new_impl('input', INPUT_REGISTRY, type)
end
Expand Down
100 changes: 100 additions & 0 deletions lib/fluent/plugin/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#
# Fluentd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'fluent/plugin'
require 'fluent/configurable'
require 'fluent/plugin_id'
require 'fluent/log'
require 'fluent/plugin_helper'

module Fluent
module Plugin
class Base
include Configurable
include PluginId
include PluginLoggerMixin
include PluginHelper::Mixin

State = Struct.new(:configure, :start, :stop, :shutdown, :close, :terminate)

def initialize
super
@state = State.new(false, false, false, false, false, false)
end

def has_router?
false
end

def configure(conf)
super
@state.configure = true
self
end

def start
@log.reset
@state.start = true
self
end

def stop
@state.stop = true
self
end

def shutdown
@state.shutdown = true
self
end

def close
@state.close = true
self
end

def terminate
@state.terminate = true
@log.reset
self
end

def configured?
@state.configure
end

def started?
@state.start
end

def stopped?
@state.stop
end

def shutdown?
@state.shutdown
end

def closed?
@state.close
end

def terminated?
@state.terminate
end
end
end
end
36 changes: 36 additions & 0 deletions lib/fluent/plugin_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#
# Fluentd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'fluent/plugin_helper/event_emitter'
require 'fluent/plugin_helper/thread'
require 'fluent/plugin_helper/event_loop'
require 'fluent/plugin_helper/timer'
require 'fluent/plugin_helper/child_process'

module Fluent
module PluginHelper
module Mixin
def self.included(mod)
mod.extend(Fluent::PluginHelper)
end
end

def helpers(*snake_case_symbols)
helper_modules = snake_case_symbols.map{|name| Fluent::PluginHelper.const_get(name.to_s.split('_').map(&:capitalize).join) }
include *helper_modules
end
end
end
Loading

0 comments on commit b19678e

Please sign in to comment.