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
20
# 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 = []
  options[:collector]
  @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



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

def author
  site_id
end

#check_required_settings(settings, required) ⇒ Object



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

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



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

def clear_deferred
  @deferred.clear
end

#defer(item) ⇒ Object



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

def defer item
  @deferred << item
end

#do_deferred(item) ⇒ Object



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

def do_deferred item
end

#do_start(task) ⇒ Object



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

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

#exitingObject



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

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

#idleObject



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

def idle
  loop do
    @task.sleep 60
  end
end

#ignore_errors(classes, &block) ⇒ Object



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

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

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



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

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



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

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



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

def restart
  stop
  start
end

#startObject



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

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



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

def stop
  @task.stop if @task
end