Skip to content

Commit

Permalink
Merge pull request #892 from okkez/remove-circular-require
Browse files Browse the repository at this point in the history
Remove redundant `require`
  • Loading branch information
tagomoris committed Apr 21, 2016
2 parents 87b39e8 + cc73ce2 commit 985a912
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 71 deletions.
5 changes: 2 additions & 3 deletions lib/fluent/agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#

require 'fluent/configurable'
require 'fluent/engine'
require 'fluent/plugin'
require 'fluent/output'

Expand All @@ -29,7 +28,7 @@ module Fluent
class Agent
include Configurable

def initialize(opts = {})
def initialize(log:)
super()

@context = nil
Expand All @@ -38,7 +37,7 @@ def initialize(opts = {})
@started_outputs = []
@started_filters = []

@log = Engine.log
@log = log
@event_router = EventRouter.new(NoMatchMatch.new(log), self)
@error_collector = nil
end
Expand Down
15 changes: 8 additions & 7 deletions lib/fluent/config/configure_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
# limitations under the License.
#

require 'fluent/configurable'

module Fluent
module Config
class ConfigureProxy
Expand Down Expand Up @@ -48,6 +46,7 @@ def initialize(name, opts = {})
@required = opts[:required]
@multi = opts[:multi]
@alias = opts[:alias]
@type_lookup = opts[:type_lookup]

raise "init and required are exclusive" if @init && @required

Expand Down Expand Up @@ -222,7 +221,7 @@ def parameter_configuration(name, *args, &block)

begin
type = :string if type.nil?
block ||= Configurable.lookup_type(type)
block ||= @type_lookup.call(type)
rescue ConfigError
# override error message
raise ArgumentError, "#{self.name}: unknown config_argument type `#{type}'"
Expand Down Expand Up @@ -295,17 +294,19 @@ def desc(description)
@current_description = description
end

def config_section(name, *args, &block)
def config_section(name, opts = {}, &block)
unless block_given?
raise ArgumentError, "#{self.name}: config_section requires block parameter"
end
name = name.to_sym

unless args.empty? || args.size == 1 && args.first.is_a?(Hash)
raise ArgumentError, "#{self.name}: unknown config_section arguments: #{args.inspect}"
unless opts.is_a?(Hash)
raise ArgumentError, "#{self.name}: unknown config_section arguments: #{opts.inspect}"
end

sub_proxy = ConfigureProxy.new(name, *args)
opts[:type_lookup] = @type_lookup

sub_proxy = ConfigureProxy.new(name, opts)
sub_proxy.instance_exec(&block)

if sub_proxy.init?
Expand Down
11 changes: 0 additions & 11 deletions lib/fluent/config/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
require 'json'

require 'fluent/config/error'
require 'fluent/configurable'

module Fluent
module Config
Expand Down Expand Up @@ -134,14 +133,4 @@ def self.bool_value(str)
end
}
end

Configurable.register_type(:string, Config::STRING_TYPE)
Configurable.register_type(:enum, Config::ENUM_TYPE)
Configurable.register_type(:integer, Config::INTEGER_TYPE)
Configurable.register_type(:float, Config::FLOAT_TYPE)
Configurable.register_type(:size, Config::SIZE_TYPE)
Configurable.register_type(:bool, Config::BOOL_TYPE)
Configurable.register_type(:time, Config::TIME_TYPE)
Configurable.register_type(:hash, Config::HASH_TYPE)
Configurable.register_type(:array, Config::ARRAY_TYPE)
end
21 changes: 17 additions & 4 deletions lib/fluent/configurable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
require 'fluent/config/error'
require 'fluent/registry'
require 'fluent/plugin'
require 'fluent/config/types'

module Fluent
module Configurable
Expand Down Expand Up @@ -90,6 +91,20 @@ def self.lookup_type(type)
CONFIG_TYPE_REGISTRY.lookup(type)
end

{
string: Config::STRING_TYPE,
enum: Config::ENUM_TYPE,
integer: Config::INTEGER_TYPE,
float: Config::FLOAT_TYPE,
size: Config::SIZE_TYPE,
bool: Config::BOOL_TYPE,
time: Config::TIME_TYPE,
hash: Config::HASH_TYPE,
array: Config::ARRAY_TYPE,
}.each do |name, type|
register_type(name, type)
end

module ClassMethods
def configure_proxy_map
map = {}
Expand All @@ -100,7 +115,8 @@ def configure_proxy_map
def configure_proxy(mod_name)
map = configure_proxy_map
unless map[mod_name]
proxy = Fluent::Config::ConfigureProxy.new(mod_name, required: true, multi: false)
type_lookup = ->(type) { Fluent::Configurable.lookup_type(type) }
proxy = Fluent::Config::ConfigureProxy.new(mod_name, required: true, multi: false, type_lookup: type_lookup)
map[mod_name] = proxy
end
map[mod_name]
Expand Down Expand Up @@ -149,7 +165,4 @@ def dump(level = 0)
end
end
end

# load default types
require 'fluent/config/types'
end
2 changes: 1 addition & 1 deletion lib/fluent/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def init(system_config)
@suppress_config_dump = system_config.suppress_config_dump unless system_config.suppress_config_dump.nil?
@without_source = system_config.without_source unless system_config.without_source.nil?

@root_agent = RootAgent.new(@system_config)
@root_agent = RootAgent.new(log: log, system_config: @system_config)

MessagePackFactory.init

Expand Down
10 changes: 5 additions & 5 deletions lib/fluent/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
# limitations under the License.
#

require 'fluent/engine'
require 'fluent/msgpack_factory'

module Fluent
class EventStream
include Enumerable
include MessagePackFactory::Mixin

def repeatable?
false
Expand All @@ -29,15 +30,15 @@ def each(&block)
end

def to_msgpack_stream
out = Fluent::Engine.msgpack_factory.packer
out = msgpack_packer
each {|time,record|
out.write([time,record])
}
out.to_s
end

def to_msgpack_stream_forced_integer
out = Fluent::Engine.msgpack_factory.packer
out = msgpack_packer
each {|time,record|
out.write([time.to_i,record])
}
Expand Down Expand Up @@ -153,8 +154,7 @@ def repeatable?

def each(&block)
# TODO format check
unpacker = Fluent::Engine.msgpack_factory.unpacker
unpacker.feed_each(@data, &block)
msgpack_unpacker.feed_each(@data, &block)
nil
end

Expand Down
1 change: 0 additions & 1 deletion lib/fluent/filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

require 'fluent/configurable'
require 'fluent/plugin_id'
require 'fluent/engine'
require 'fluent/event'
require 'fluent/plugin' # to register itself to registry
require 'fluent/log'
Expand Down
4 changes: 2 additions & 2 deletions lib/fluent/label.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

module Fluent
class Label < Agent
def initialize(name, opts = {})
super(opts)
def initialize(name, log:)
super(log: log)

@context = name
@root_agent = nil
Expand Down
1 change: 0 additions & 1 deletion lib/fluent/output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

require 'fluent/configurable'
require 'fluent/plugin_id'
require 'fluent/engine'
require 'fluent/log'
require 'fluent/output_chain'
require 'fluent/plugin'
Expand Down
10 changes: 5 additions & 5 deletions lib/fluent/root_agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

require 'fluent/config/error'
require 'fluent/agent'
require 'fluent/engine'
require 'fluent/label'
require 'fluent/plugin'
require 'fluent/system_config'
require 'fluent/time'

module Fluent
#
Expand All @@ -47,8 +47,8 @@ module Fluent
class RootAgent < Agent
ERROR_LABEL = "@ERROR".freeze # @ERROR is built-in error label

def initialize(system_config = SystemConfig.new)
super
def initialize(log:, system_config: SystemConfig.new)
super(log: log)

@labels = {}
@inputs = []
Expand Down Expand Up @@ -159,7 +159,7 @@ def add_source(type, conf)
end

def add_label(name)
label = Label.new(name)
label = Label.new(name, log: log)
label.root_agent = self
@labels[name] = label
end
Expand Down Expand Up @@ -219,7 +219,7 @@ def emit_error_event(tag, time, record, error)
end

def handle_emits_error(tag, es, e)
now = Engine.now
now = EventTime.now
if @suppress_emit_error_log_interval.zero? || now > @next_emit_error_log_time
log.warn "emit transaction failed in @ERROR:", error: e, tag: tag
log.warn_backtrace
Expand Down
Loading

0 comments on commit 985a912

Please sign in to comment.