Class: Rosette::Queuing::QueueConfigurator

Inherits:
Object
  • Object
show all
Defined in:
lib/rosette/queuing/queue_configurator.rb

Overview

Configuration used to initialize a queue implementation.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeQueueConfigurator

Creates a new QueueConfigurator instance.


21
22
23
24
# File 'lib/rosette/queuing/queue_configurator.rb', line 21

def initialize
  @queue_options = {}
  @queue_configs = []
end

Instance Attribute Details

#queue_configsArray (readonly)

Returns an array of queue config objects. These classes are provided by each type of queue. For example, the queue that processes commits (see [Rosette::Queuing::Commits]) defines its own configurator that gets instantiated and added to this array.

Returns:

  • (Array)

    an array of queue config objects. These classes are provided by each type of queue. For example, the queue that processes commits (see [Rosette::Queuing::Commits]) defines its own configurator that gets instantiated and added to this array.


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rosette/queuing/queue_configurator.rb', line 15

class QueueConfigurator
  attr_reader :queue_options, :queue_configs

  # Creates a new +QueueConfigurator+ instance.
  #
  # @return [QueueConfigurator]
  def initialize
    @queue_options = {}
    @queue_configs = []
  end

  # Sets an options hash that will be used to initialize the underlying
  # queue implementation (eg. resque, sidekiq, etc).
  #
  # @param [Hash] options The options hash to use to initialize the
  #   underlying queue implementation (eg. resque, sidekiq, etc).
  # @return [void]
  def set_queue_options(options = {})
    @queue_options = options
  end

  # Configures and adds a queue to process jobs from. Note that the term
  # "queue" here refers to a sequence of jobs, not a queue implementation.
  #
  # @param [String] queue_name The name of the queue to configure.
  # @return [void]
  def enable_queue(queue_name)
    if const = find_queue_configurator_const(queue_name)
      config = const.new(queue_name)
      yield config if block_given?
      queue_configs << config
    else
      raise ArgumentError, "'#{queue_name}' couldn't be found."
    end
  end

  # Looks up a queue configuration object by name.
  #
  # @param [String] queue_name The name of the queue to look up.
  # @return [Object, nil] The queue config object, or +nil+ if none could
  #   be found.
  def get_queue_config(queue_name)
    queue_configs.find { |q| q.name == queue_name }
  end

  protected

  def find_queue_configurator_const(name)
    const_str = Rosette::Core::StringUtils.camelize(name)

    if Rosette::Queuing.const_defined?(const_str)
      mod = Rosette::Queuing.const_get(const_str)

      if mod.const_defined?(:"#{const_str}QueueConfigurator")
        mod.const_get(:"#{const_str}QueueConfigurator")
      end
    end
  end
end

#queue_optionsHash (readonly)

Returns a hash of options to be used by the queue implementation.

Returns:

  • (Hash)

    a hash of options to be used by the queue implementation.


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rosette/queuing/queue_configurator.rb', line 15

class QueueConfigurator
  attr_reader :queue_options, :queue_configs

  # Creates a new +QueueConfigurator+ instance.
  #
  # @return [QueueConfigurator]
  def initialize
    @queue_options = {}
    @queue_configs = []
  end

  # Sets an options hash that will be used to initialize the underlying
  # queue implementation (eg. resque, sidekiq, etc).
  #
  # @param [Hash] options The options hash to use to initialize the
  #   underlying queue implementation (eg. resque, sidekiq, etc).
  # @return [void]
  def set_queue_options(options = {})
    @queue_options = options
  end

  # Configures and adds a queue to process jobs from. Note that the term
  # "queue" here refers to a sequence of jobs, not a queue implementation.
  #
  # @param [String] queue_name The name of the queue to configure.
  # @return [void]
  def enable_queue(queue_name)
    if const = find_queue_configurator_const(queue_name)
      config = const.new(queue_name)
      yield config if block_given?
      queue_configs << config
    else
      raise ArgumentError, "'#{queue_name}' couldn't be found."
    end
  end

  # Looks up a queue configuration object by name.
  #
  # @param [String] queue_name The name of the queue to look up.
  # @return [Object, nil] The queue config object, or +nil+ if none could
  #   be found.
  def get_queue_config(queue_name)
    queue_configs.find { |q| q.name == queue_name }
  end

  protected

  def find_queue_configurator_const(name)
    const_str = Rosette::Core::StringUtils.camelize(name)

    if Rosette::Queuing.const_defined?(const_str)
      mod = Rosette::Queuing.const_get(const_str)

      if mod.const_defined?(:"#{const_str}QueueConfigurator")
        mod.const_get(:"#{const_str}QueueConfigurator")
      end
    end
  end
end

Instance Method Details

#enable_queue(queue_name) ⇒ void

This method returns an undefined value.

Configures and adds a queue to process jobs from. Note that the term “queue” here refers to a sequence of jobs, not a queue implementation.

Parameters:

  • queue_name (String)

    The name of the queue to configure.


41
42
43
44
45
46
47
48
49
# File 'lib/rosette/queuing/queue_configurator.rb', line 41

def enable_queue(queue_name)
  if const = find_queue_configurator_const(queue_name)
    config = const.new(queue_name)
    yield config if block_given?
    queue_configs << config
  else
    raise ArgumentError, "'#{queue_name}' couldn't be found."
  end
end

#get_queue_config(queue_name) ⇒ Object?

Looks up a queue configuration object by name.

Parameters:

  • queue_name (String)

    The name of the queue to look up.

Returns:

  • (Object, nil)

    The queue config object, or nil if none could be found.


56
57
58
# File 'lib/rosette/queuing/queue_configurator.rb', line 56

def get_queue_config(queue_name)
  queue_configs.find { |q| q.name == queue_name }
end

#set_queue_options(options = {}) ⇒ void

This method returns an undefined value.

Sets an options hash that will be used to initialize the underlying queue implementation (eg. resque, sidekiq, etc).

Parameters:

  • options (Hash) (defaults to: {})

    The options hash to use to initialize the underlying queue implementation (eg. resque, sidekiq, etc).


32
33
34
# File 'lib/rosette/queuing/queue_configurator.rb', line 32

def set_queue_options(options = {})
  @queue_options = options
end