Class: Fusuma::Plugin::Inputs::TimerInput

Inherits:
Input
  • Object
show all
Includes:
Singleton
Defined in:
lib/fusuma/plugin/inputs/timer_input.rb

Overview

libinput commands wrapper

Constant Summary collapse

DEFAULT_INTERVAL =
5
EPSILON_TIME =
0.02

Instance Attribute Summary collapse

Attributes inherited from Input

#tag

Instance Method Summary collapse

Methods inherited from Input

#create_event, #read_from_io, select

Methods inherited from Base

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

Constructor Details

#initialize(*args, interval: nil) ⇒ TimerInput

Returns a new instance of TimerInput.



20
21
22
23
24
# File 'lib/fusuma/plugin/inputs/timer_input.rb', line 20

def initialize(*args, interval: nil)
  super(*args)
  @interval = interval || config_params(:interval) || DEFAULT_INTERVAL
  @early_wake_queue = Queue.new
end

Instance Attribute Details

#intervalObject (readonly)

Returns the value of attribute interval.



26
27
28
# File 'lib/fusuma/plugin/inputs/timer_input.rb', line 26

def interval
  @interval
end

#pidObject (readonly)

Returns the value of attribute pid.



26
27
28
# File 'lib/fusuma/plugin/inputs/timer_input.rb', line 26

def pid
  @pid
end

Instance Method Details

#config_param_typesObject



14
15
16
17
18
# File 'lib/fusuma/plugin/inputs/timer_input.rb', line 14

def config_param_types
  {
    interval: [Float]
  }
end

#ioObject



28
29
30
31
32
33
34
35
# File 'lib/fusuma/plugin/inputs/timer_input.rb', line 28

def io
  @io ||= begin
    reader, writer = create_io
    @pid = start(reader, writer)

    reader
  end
end

#start(reader, writer) ⇒ Object



37
38
39
40
41
42
# File 'lib/fusuma/plugin/inputs/timer_input.rb', line 37

def start(reader, writer)
  Thread.new do
    timer_loop(writer)
  end
  nil
end

#timer_loop(writer) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/fusuma/plugin/inputs/timer_input.rb', line 44

def timer_loop(writer)
  delta_t = @interval
  next_wake = Time.now + delta_t
  loop do
    sleep_time = next_wake - Time.now
    if sleep_time <= 0
      raise Timeout::Error
    end

    Timeout.timeout(sleep_time) do
      next_wake = [@early_wake_queue.deq, next_wake].min
    end
  rescue Timeout::Error
    writer.puts "timer"
    next_wake = Time.now + delta_t
  end
rescue Errno::EPIPE
  exit 0
rescue => e
  MultiLogger.error e
end

#wake_early(t) ⇒ Object



66
67
68
# File 'lib/fusuma/plugin/inputs/timer_input.rb', line 66

def wake_early(t)
  @early_wake_queue.push(t + EPSILON_TIME)
end