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
# 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
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

#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


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

def author
  site_id
end

#check_required_settings(settings, required) ⇒ Object


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

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

#defer(item) ⇒ Object


26
27
28
# File 'lib/rsmp/node.rb', line 26

def defer item
  @deferred << item
end

#do_deferred(item) ⇒ Object


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

def do_deferred item
end

#do_start(task) ⇒ Object


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

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

#exitingObject


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

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

#idleObject


64
65
66
67
68
# File 'lib/rsmp/node.rb', line 64

def idle
  loop do
    @task.sleep 60
  end
end

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


19
20
21
22
23
24
# File 'lib/rsmp/node.rb', line 19

def notify_error e, options={}
  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


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

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


74
75
76
77
# File 'lib/rsmp/node.rb', line 74

def restart
  stop
  start
end

#startObject


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rsmp/node.rb', line 48

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


70
71
72
# File 'lib/rsmp/node.rb', line 70

def stop
  @task.stop if @task
end