Class: RSMP::Node

Inherits:
Object
  • Object
show all
Includes:
Inspect, Logging, Wait
Defined in:
lib/rsmp/node.rb

Direct Known Subclasses

Site, Supervisor

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Inspect

#inspect, #inspector

Methods included from Wait

#wait_for

Methods included from Logging

#initialize_logging, #log

Constructor Details

#initialize(options) ⇒ Node

Returns a new instance of Node.



11
12
13
14
15
16
17
18
19
# File 'lib/rsmp/node.rb', line 11

def initialize options
  initialize_logging options
  @task = options[:task]
  @deferred = []
  @clock = Clock.new
  @error_queue = Async::Queue.new
  @ignore_errors = []
  @collect = options[:collect]
end

Instance Attribute Details

#archiveObject (readonly)

Returns the value of attribute archive.



9
10
11
# File 'lib/rsmp/node.rb', line 9

def archive
  @archive
end

#clockObject (readonly)

Returns the value of attribute clock.



9
10
11
# File 'lib/rsmp/node.rb', line 9

def clock
  @clock
end

#collectorObject (readonly)

Returns the value of attribute collector.



9
10
11
# File 'lib/rsmp/node.rb', line 9

def collector
  @collector
end

#deferredObject (readonly)

Returns the value of attribute deferred.



9
10
11
# File 'lib/rsmp/node.rb', line 9

def deferred
  @deferred
end

#error_queueObject (readonly)

Returns the value of attribute error_queue.



9
10
11
# File 'lib/rsmp/node.rb', line 9

def error_queue
  @error_queue
end

#loggerObject (readonly)

Returns the value of attribute logger.



9
10
11
# File 'lib/rsmp/node.rb', line 9

def logger
  @logger
end

#taskObject (readonly)

Returns the value of attribute task.



9
10
11
# File 'lib/rsmp/node.rb', line 9

def task
  @task
end

Instance Method Details

#authorObject



104
105
106
# File 'lib/rsmp/node.rb', line 104

def author
  site_id
end

#check_required_settings(settings, required) ⇒ Object



97
98
99
100
101
102
# File 'lib/rsmp/node.rb', line 97

def check_required_settings settings, required
  raise ArgumentError.new "Settings is empty" unless settings
  required.each do |setting|
    raise ArgumentError.new "Missing setting: #{setting}" unless settings.include? setting.to_s
  end 
end

#clear_deferredObject



51
52
53
# File 'lib/rsmp/node.rb', line 51

def clear_deferred
  @deferred.clear
end

#defer(item) ⇒ Object



36
37
38
# File 'lib/rsmp/node.rb', line 36

def defer item
  @deferred << item
end

#do_deferred(item) ⇒ Object



48
49
# File 'lib/rsmp/node.rb', line 48

def do_deferred item
end

#do_start(task) ⇒ Object



55
56
57
58
59
60
# File 'lib/rsmp/node.rb', line 55

def do_start task
  task.annotate self.class.to_s
  @task = task
  start_action
  idle
end

#exitingObject



93
94
95
# File 'lib/rsmp/node.rb', line 93

def exiting
  log "Exiting", level: :info
end

#idleObject



78
79
80
81
82
# File 'lib/rsmp/node.rb', line 78

def idle
  loop do
    @task.sleep 60
  end
end

#ignore_errors(classes, &block) ⇒ Object



21
22
23
24
25
26
# File 'lib/rsmp/node.rb', line 21

def ignore_errors classes, &block
  was, @ignore_errors = @ignore_errors, [classes].flatten
  yield
ensure
  @ignore_errors = was
end

#notify_error(e, options = {}) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/rsmp/node.rb', line 28

def notify_error e, options={}
  return if @ignore_errors.find { |klass| e.is_a? klass }
  if options[:level] == :internal
    log ["#{e.to_s} in task: #{Async::Task.current.to_s}",e.backtrace].flatten.join("\n"), level: :error
  end
  @error_queue.enqueue e
end

#process_deferredObject



40
41
42
43
44
45
46
# File 'lib/rsmp/node.rb', line 40

def process_deferred
  cloned = @deferred.clone    # clone in case do_deferred restarts the current task
  @deferred.clear
  cloned.each do |item|
    do_deferred item
  end
end

#restartObject



88
89
90
91
# File 'lib/rsmp/node.rb', line 88

def restart
  stop
  start
end

#startObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rsmp/node.rb', line 62

def start
  starting
  if @task
    do_start @task
  else
    Async do |task|
      do_start task
    end
  end
rescue Errno::EADDRINUSE => e
  log "Cannot start: #{e.to_s}", level: :error
rescue SystemExit, SignalException, Interrupt
  @logger.unmute_all
  exiting
end

#stopObject



84
85
86
# File 'lib/rsmp/node.rb', line 84

def stop
  @task.stop if @task
end