Class: LogStash::Outputs::Application_insights::Timer

Inherits:
Object
  • Object
show all
Defined in:
lib/logstash/outputs/application_insights/timer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTimer

Returns a new instance of Timer.



63
64
65
66
67
68
# File 'lib/logstash/outputs/application_insights/timer.rb', line 63

def initialize
  @@timers_mutex.synchronize {
    @@timers << self
  }
  @state = :off
end

Instance Attribute Details

#callbackObject (readonly)

Returns the value of attribute callback.



29
30
31
# File 'lib/logstash/outputs/application_insights/timer.rb', line 29

def callback
  @callback
end

#expirationObject (readonly)

Returns the value of attribute expiration.



27
28
29
# File 'lib/logstash/outputs/application_insights/timer.rb', line 27

def expiration
  @expiration
end

#objectObject (readonly)

Returns the value of attribute object.



28
29
30
# File 'lib/logstash/outputs/application_insights/timer.rb', line 28

def object
  @object
end

#stateObject

Returns the value of attribute state.



26
27
28
# File 'lib/logstash/outputs/application_insights/timer.rb', line 26

def state
  @state
end

Class Method Details

.config(configuration) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/logstash/outputs/application_insights/timer.rb', line 31

def self.config ( configuration )
  @@configuration = configuration
  @@logger = configuration[:logger]
  @@timers = []
  @@timers_modified = false
  @@timers_mutex = Mutex.new

  Thread.new do
    loop do
      sleep( 1 )

      curr_time = Time.now.utc
      timers_triggerd = [  ]

      @@timers_mutex.synchronize {
        @@timers.each do |timer|
          if :on == timer.state && curr_time >= timer.expiration
            timer.state = :trigger
            timers_triggerd << [ timer.object, timer.callback ]
          end
        end
      }

      timers_triggerd.each do |pair|
        (object, callback) = pair
        callback.call( object )
      end
    end
  end

end

Instance Method Details

#cancelObject



80
81
82
83
84
85
86
87
# File 'lib/logstash/outputs/application_insights/timer.rb', line 80

def cancel
  @@timers_mutex.synchronize {
    state = @state
    @state = :off
    @@timers_modified = true if :on == state
    state != :trigger
  }
end

#set(expiration, object, &callback) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/logstash/outputs/application_insights/timer.rb', line 70

def set ( expiration, object, &callback )
  @@timers_mutex.synchronize {
    @@timers_modified= true
    @state = :on
    @object = object
    @expiration = expiration
    @callback = callback
  }
end