Module: Hutch::Consumer::ClassMethods

Defined in:
lib/hutch/consumer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#initial_group_sizeObject (readonly)

Returns the value of attribute initial_group_size.



43
44
45
# File 'lib/hutch/consumer.rb', line 43

def initial_group_size
  @initial_group_size
end

#queue_modeObject (readonly)

Returns the value of attribute queue_mode.



43
44
45
# File 'lib/hutch/consumer.rb', line 43

def queue_mode
  @queue_mode
end

#queue_typeObject (readonly)

Returns the value of attribute queue_type.



43
44
45
# File 'lib/hutch/consumer.rb', line 43

def queue_type
  @queue_type
end

Instance Method Details

#arguments(arguments = {}) ⇒ Object

Configures an optional argument that will be passed when declaring the queue. Prefer using a policy to this DSL: www.rabbitmq.com/parameters.html#policies



70
71
72
# File 'lib/hutch/consumer.rb', line 70

def arguments(arguments = {})
  @arguments = arguments
end

#classic_queueObject

Explicitly set the queue type to ‘classic’



56
57
58
# File 'lib/hutch/consumer.rb', line 56

def classic_queue
  @queue_type = 'classic'
end

#consume(*routing_keys) ⇒ Object

Add one or more routing keys to the set of routing keys the consumer wants to subscribe to.



36
37
38
39
40
41
# File 'lib/hutch/consumer.rb', line 36

def consume(*routing_keys)
  @routing_keys = self.routing_keys.union(routing_keys)
  # these are opt-in
  @queue_mode = nil
  @queue_type = nil
end

#get_argumentsObject

Returns consumer custom arguments.



95
96
97
98
99
100
101
102
103
# File 'lib/hutch/consumer.rb', line 95

def get_arguments
  all_arguments = @arguments || {}

  all_arguments['x-queue-mode'] = @queue_mode if @queue_mode
  all_arguments['x-queue-type'] = @queue_type if @queue_type
  all_arguments['x-quorum-initial-group-size'] = @initial_group_size if @initial_group_size

  all_arguments
end

#get_optionsObject



105
106
107
108
109
110
111
112
# File 'lib/hutch/consumer.rb', line 105

def get_options
  default_options = { durable: true }

  all_options = default_options.merge(@queue_options || {})
  all_options[:arguments] = get_arguments

  all_options
end

#get_queue_nameObject

The RabbitMQ queue name for the consumer. This is derived from the fully-qualified class name. Module separators are replaced with single colons, camelcased class names are converted to snake case.



87
88
89
90
91
92
# File 'lib/hutch/consumer.rb', line 87

def get_queue_name
  return @queue_name unless @queue_name.nil?
  queue_name = self.name.gsub(/::/, ':')
  queue_name.gsub!(/([^A-Z:])([A-Z])/) { "#{$1}_#{$2}" }
  queue_name.downcase
end

#get_serializerObject



119
120
121
# File 'lib/hutch/consumer.rb', line 119

def get_serializer
  @serializer
end

#lazy_queueObject

Explicitly set the queue mode to ‘lazy’



51
52
53
# File 'lib/hutch/consumer.rb', line 51

def lazy_queue
  @queue_mode = 'lazy'
end

#queue_name(name) ⇒ Object

Explicitly set the queue name



46
47
48
# File 'lib/hutch/consumer.rb', line 46

def queue_name(name)
  @queue_name = name
end

#queue_options(options = {}) ⇒ Object

Congfiures queue options that will be passed when declaring the queue.



75
76
77
# File 'lib/hutch/consumer.rb', line 75

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

#quorum_queue(options = {}) ⇒ Object

Explicitly set the queue type to ‘quorum’

Parameters:

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

    the options params related to quorum queue

Options Hash (options):

  • :initial_group_size (Integer)

    Initial Replication Factor



63
64
65
66
# File 'lib/hutch/consumer.rb', line 63

def quorum_queue(options = {})
  @queue_type = 'quorum'
  @initial_group_size = options[:initial_group_size]
end

#routing_keysObject

Accessor for the consumer’s routing key.



115
116
117
# File 'lib/hutch/consumer.rb', line 115

def routing_keys
  @routing_keys ||= Set.new
end

#serializer(name) ⇒ Object

Set custom serializer class, override global value



80
81
82
# File 'lib/hutch/consumer.rb', line 80

def serializer(name)
  @serializer = name
end