Skip to content

Commit

Permalink
fix: cutting out read_from_io from Input class
Browse files Browse the repository at this point in the history
- To change the way to extract data after IO.select, override Input#read_from_io method
- When using binary encoded packed data containing "\n" in the data with input plugin using msgpack, an error occurred
- With the previous implementation, the input plugin depended on `io.readline(chomp: true)` when extracting data after IO.select, so the input plugin needed to send a string containing a newline
- By cutting out read_from_io method, each plugin can extend the way data is extracted
  • Loading branch information
iberianpig committed Jan 15, 2024
1 parent 3a6e285 commit 007b94f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
29 changes: 16 additions & 13 deletions lib/fusuma/plugin/inputs/input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
5 changes: 5 additions & 0 deletions spec/fusuma/plugin/inputs/input_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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") }
Expand Down

0 comments on commit 007b94f

Please sign in to comment.