Class: Bluepill::Trigger
- Inherits:
-
Object
show all
- Defined in:
- lib/bluepill/trigger.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(process, options = {}) ⇒ Trigger
Returns a new instance of Trigger.
15
16
17
18
19
20
|
# File 'lib/bluepill/trigger.rb', line 15
def initialize(process, options = {})
self.process = process
self.logger = options[:logger]
self.mutex = Mutex.new
self.scheduled_events = []
end
|
Instance Attribute Details
#logger ⇒ Object
Returns the value of attribute logger.
13
14
15
|
# File 'lib/bluepill/trigger.rb', line 13
def logger
@logger
end
|
#mutex ⇒ Object
Returns the value of attribute mutex.
13
14
15
|
# File 'lib/bluepill/trigger.rb', line 13
def mutex
@mutex
end
|
#process ⇒ Object
Returns the value of attribute process.
13
14
15
|
# File 'lib/bluepill/trigger.rb', line 13
def process
@process
end
|
#scheduled_events ⇒ Object
Returns the value of attribute scheduled_events.
13
14
15
|
# File 'lib/bluepill/trigger.rb', line 13
def scheduled_events
@scheduled_events
end
|
Class Method Details
.[](name) ⇒ Object
9
10
11
|
# File 'lib/bluepill/trigger.rb', line 9
def self.[](name)
@implementations[name]
end
|
.inherited(klass) ⇒ Object
5
6
7
|
# File 'lib/bluepill/trigger.rb', line 5
def self.inherited(klass)
@implementations[klass.name.split('::').last.underscore.to_sym] = klass
end
|
Instance Method Details
#cancel_all_events ⇒ Object
52
53
54
55
56
57
|
# File 'lib/bluepill/trigger.rb', line 52
def cancel_all_events
self.logger.info "Canceling all scheduled events"
self.mutex.synchronize do
self.scheduled_events.each {|_, thread| thread.kill}
end
end
|
#dispatch!(event) ⇒ Object
30
31
32
|
# File 'lib/bluepill/trigger.rb', line 30
def dispatch!(event)
self.process.dispatch!(event, self.class.name.split("::").last)
end
|
#notify(transition) ⇒ Object
26
27
28
|
# File 'lib/bluepill/trigger.rb', line 26
def notify(transition)
raise "Implement in subclass"
end
|
#reset! ⇒ Object
22
23
24
|
# File 'lib/bluepill/trigger.rb', line 22
def reset!
self.cancel_all_events
end
|
#schedule_event(event, delay) ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/bluepill/trigger.rb', line 34
def schedule_event(event, delay)
thread = Thread.new(self) do |trigger|
begin
sleep delay.to_f
trigger.dispatch!(event)
trigger.mutex.synchronize do
trigger.scheduled_events.delete_if { |_, thread| thread == Thread.current }
end
rescue StandardError => e
trigger.logger.err(e)
trigger.logger.err(e.backtrace.join("\n"))
end
end
self.scheduled_events.push([event, thread])
end
|