Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract method Input#read_from_io #315

Merged
merged 1 commit into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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