Class: MCollective::Registration::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/mcollective/registration/base.rb

Overview

This is a base class that other registration plugins can use to handle regular announcements to the mcollective

The configuration file determines how often registration messages gets sent using the registerinterval option, the plugin runs in the background in a thread.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.inherited(klass) ⇒ Object

Register plugins that inherits base



11
12
13
# File 'lib/mcollective/registration/base.rb', line 11

def self.inherited(klass)
  PluginManager << {:type => "registration_plugin", :class => klass.to_s}
end

Instance Method Details

#configObject



36
37
38
# File 'lib/mcollective/registration/base.rb', line 36

def config
  Config.instance
end

#intervalObject



59
60
61
# File 'lib/mcollective/registration/base.rb', line 59

def interval
  config.registerinterval
end

#msg_filterObject



40
41
42
43
44
# File 'lib/mcollective/registration/base.rb', line 40

def msg_filter
  filter = Util.empty_filter
  filter["agent"] << "registration"
  filter
end

#publish(message) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/mcollective/registration/base.rb', line 63

def publish(message)
  unless message
    Log.debug("Skipping registration due to nil body")
  else
    req = Message.new(message, nil, {:type => :request, :agent => "registration", :collective => target_collective, :filter => msg_filter})
    req.encode!

    Log.debug("Sending registration #{req.requestid} to collective #{req.collective}")

    req.publish
  end
end

#run(connection) ⇒ Object

Creates a background thread that periodically send a registration notice.

The actual registration notices comes from the ‘body’ method of the registration plugins.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/mcollective/registration/base.rb', line 19

def run(connection)
  return false if interval == 0

  Thread.new do
    loop do
      begin
        publish(body)

        sleep interval
      rescue Exception => e
        Log.error("Sending registration message failed: #{e}")
        sleep interval
      end
    end
  end
end

#target_collectiveObject



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/mcollective/registration/base.rb', line 46

def target_collective
  main_collective = config.main_collective

  collective = config.registration_collective || main_collective

  unless config.collectives.include?(collective)
    Log.warn("Sending registration to #{main_collective}: #{collective} is not a valid collective")
    collective = main_collective
  end

  return collective
end