2
require 'puppet/indirector'
3
require 'puppet/util/instance_loader'
5
# Implements a message queue client type plugin registry for use by the indirector facility.
6
# Client modules for speaking a particular protocol (e.g. Stomp::Client for Stomp message
7
# brokers, Memcached for Starling and Sparrow, etc.) register themselves with this module.
9
# Client classes are expected to live under the Puppet::Util::Queue namespace and corresponding
10
# directory; the attempted use of a client via its typename (see below) will cause Puppet::Util::Queue
11
# to attempt to load the corresponding plugin if it is not yet loaded. The client class registers itself
12
# with Puppet::Util::Queue and should use the same type name as the autloader expects for the plugin file.
13
# class Puppet::Util::Queue::SpecialMagicalClient < Messaging::SpecialMagic
15
# Puppet::Util::Queue.register_queue_type_class(self)
18
# This module reduces the rightmost segment of the class name into a pretty symbol that will
19
# serve as the queuing client's name. Which means that the "SpecialMagicalClient" above will
20
# be named <em>:special_magical_client</em> within the registry.
22
# Another class/module may mix-in this module, and may then make use of the registered clients.
24
# # mix it in at the class object level rather than instance level
25
# extend ::Puppet::Util::Queue
28
# Queue::Fue instances can get a message queue client through the registry through the mixed-in method
29
# +client+, which will return a class-wide singleton client instance, determined by +client_class+.
31
# The client plugins are expected to implement an interface similar to that of Stomp::Client:
32
# * <tt>new()</tt> should return a connected, ready-to-go client instance. Note that no arguments are passed in.
33
# * <tt>send_message(queue, message)</tt> should send the _message_ to the specified _queue_.
34
# * <tt>subscribe(queue)</tt> _block_ subscribes to _queue_ and executes _block_ upon receiving a message.
35
# * _queue_ names are simple names independent of the message broker or client library. No "/queue/" prefixes like in Stomp::Client.
36
module Puppet::Util::Queue
37
extend Puppet::Util::InstanceLoader
38
instance_load :queue_clients, 'puppet/util/queue'
40
# Adds a new class/queue-type pair to the registry. The _type_ argument is optional; if not provided,
41
# _type_ defaults to a lowercased, underscored symbol programmatically derived from the rightmost
42
# namespace of <em>klass.name</em>.
44
# # register with default name +:you+
45
# register_queue_type(Foo::You)
47
# # register with explicit queue type name +:myself+
48
# register_queue_type(Foo::Me, :myself)
50
# If the type is already registered, an exception is thrown. No checking is performed of _klass_,
51
# however; a given class could be registered any number of times, as long as the _type_ differs with
53
def self.register_queue_type(klass, type = nil)
54
type ||= queue_type_from_class(klass)
55
raise Puppet::Error, "Queue type %s is already registered" % type.to_s if instance_hash(:queue_clients).include?(type)
56
instance_hash(:queue_clients)[type] = klass
59
# Given a queue type symbol, returns the associated +Class+ object. If the queue type is unknown
60
# (meaning it hasn't been registered with this module), an exception is thrown.
61
def self.queue_type_to_class(type)
62
c = loaded_instance :queue_clients, type
63
raise Puppet::Error, "Queue type %s is unknown." % type unless c
67
# Given a class object _klass_, returns the programmatic default queue type name symbol for _klass_.
68
# The algorithm is as shown in earlier examples; the last namespace segment of _klass.name_ is taken
69
# and converted from mixed case to underscore-separated lowercase, and interned.
70
# queue_type_from_class(Foo) -> :foo
71
# queue_type_from_class(Foo::Too) -> :too
72
# queue_type_from_class(Foo::ForYouTwo) -> :for_you_too
74
# The implicit assumption here, consistent with Puppet's approach to plugins in general,
75
# is that all your client modules live in the same namespace, such that reduction to
76
# a flat namespace of symbols is reasonably safe.
77
def self.queue_type_from_class(klass)
78
# convert last segment of classname from studly caps to lower case with underscores, and symbolize
79
klass.name.split('::').pop.sub(/^[A-Z]/) {|c| c.downcase}.gsub(/[A-Z]/) {|c| '_' + c.downcase }.intern
82
# The class object for the client to be used, determined by queue configuration
84
# Looks to the <tt>:queue_type</tt> configuration entry in the running application for
85
# the default queue type to use.
87
Puppet::Util::Queue.queue_type_to_class(Puppet[:queue_type])
90
# Returns (instantiating as necessary) the singleton queue client instance, according to the
91
# client_class. No arguments go to the client class constructor, meaning its up to the client class
92
# to know how to determine its queue message source (presumably through Puppet configuration data).
94
@client ||= client_class.new