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

: (*nil, ?interval: nil) -> void



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

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.



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

def interval
  @interval
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

: () -> IO



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

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

    reader
  end
end

#start(reader, writer) ⇒ Object

: (IO, IO) -> Thread



40
41
42
43
44
# File 'lib/fusuma/plugin/inputs/timer_input.rb', line 40

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

#timer_loop(writer) ⇒ Object



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

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

: (Time) -> Thread::Queue



69
70
71
# File 'lib/fusuma/plugin/inputs/timer_input.rb', line 69

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