diff --git a/lib/fusuma/plugin/inputs/input.rb b/lib/fusuma/plugin/inputs/input.rb index 19507f2..1f1eb0c 100644 --- a/lib/fusuma/plugin/inputs/input.rb +++ b/lib/fusuma/plugin/inputs/input.rb @@ -25,19 +25,22 @@ def self.select(inputs) input = inputs.find { |i| i.io == io } - begin - # NOTE: io.readline is blocking method - # each input plugin must write line to pipe (include `\n`) - line = io.readline(chomp: true) - rescue EOFError => e - MultiLogger.error "#{input.class.name}: #{e}" - MultiLogger.error "Shutdown fusuma process..." - Process.kill("TERM", Process.pid) - rescue => e - MultiLogger.error "#{input.class.name}: #{e}" - exit 1 - end - input.create_event(record: line) + input.create_event(record: input.read_from_io) + end + + # @return [String, Record] + # IO#readline is blocking method + # so input plugin must write line to pipe (include `\n`) + # or, override read_from_io and implement your own read method + def read_from_io + io.readline(chomp: true) + rescue EOFError => e + MultiLogger.error "#{self.class.name}: #{e}" + MultiLogger.error "Shutdown fusuma process..." + Process.kill("TERM", Process.pid) + rescue => e + MultiLogger.error "#{self.class.name}: #{e}" + exit 1 end # @return [Integer] diff --git a/spec/fusuma/plugin/inputs/input_spec.rb b/spec/fusuma/plugin/inputs/input_spec.rb index 1ae758a..4a37f82 100644 --- a/spec/fusuma/plugin/inputs/input_spec.rb +++ b/spec/fusuma/plugin/inputs/input_spec.rb @@ -66,6 +66,11 @@ def io it { is_expected.to be_a IO } end + describe "#read_from_io" do + subject { dummy_input.read_from_io } + it { is_expected.to eq "hoge" } + end + describe "#config_params" do subject { dummy_input.config_params(:dummy) } it { is_expected.to eq("dummy") }