Class: Fusuma::Plugin::Inputs::Input Abstract

Inherits:
Base
  • Object
show all
Defined in:
lib/fusuma/plugin/inputs/input.rb

Overview

This class is abstract.

Subclass and override #io to implement

Inherite this base

Direct Known Subclasses

LibinputCommandInput, TimerInput

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#config_index, #config_param_types, #config_params, inherited, plugins, #shutdown

Constructor Details

#initialize(*args) ⇒ Input

Returns a new instance of Input.



12
13
14
15
# File 'lib/fusuma/plugin/inputs/input.rb', line 12

def initialize(*args)
  super
  @tag = self.class.name.split("Inputs::").last.underscore
end

Instance Attribute Details

#tagObject (readonly)

Returns the value of attribute tag.



17
18
19
# File 'lib/fusuma/plugin/inputs/input.rb', line 17

def tag
  @tag
end

Class Method Details

.select(inputs) ⇒ Event

Wait multiple inputs until it becomes readable

Parameters:

Returns:

  • (Event)


22
23
24
25
26
27
28
29
# File 'lib/fusuma/plugin/inputs/input.rb', line 22

def self.select(inputs)
  ios = IO.select(inputs.map(&:io))
  io = ios&.first&.first

  input = inputs.find { |i| i.io == io }

  input.create_event(record: input.read_from_io)
end

Instance Method Details

#create_event(record: "dummy input") ⇒ Event

Returns:

  • (Event)


52
53
54
55
56
# File 'lib/fusuma/plugin/inputs/input.rb', line 52

def create_event(record: "dummy input")
  e = Events::Event.new(tag: tag, record: record)
  MultiLogger.debug(input_event: e)
  e
end

#ioIO

Returns:

  • (IO)

Raises:

  • (NotImplementedError)


47
48
49
# File 'lib/fusuma/plugin/inputs/input.rb', line 47

def io
  raise NotImplementedError, "override #{self.class.name}##{__method__}"
end

#read_from_ioString, 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

Returns:



35
36
37
38
39
40
41
42
43
44
# File 'lib/fusuma/plugin/inputs/input.rb', line 35

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