Class: QueueBus::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/queue_bus/config.rb

Overview

This class contains all the configuration for a running queue bus application.

Defined Under Namespace

Classes: Wrap

Constant Summary collapse

LOCAL_MODE_VAR =
:queue_bus_local_mode
CONTEXT_VAR =
:queue_bus_context

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



14
15
16
17
18
# File 'lib/queue_bus/config.rb', line 14

def initialize
  @worker_middleware_stack = QueueBus::Middleware::Stack.new
  @incoming_queue = 'bus_incoming'
  @hostname = Socket.gethostname
end

Instance Attribute Details

#contextObject

Returns the current context of QueueBus



39
40
41
42
43
44
45
# File 'lib/queue_bus/config.rb', line 39

def context
  if Thread.current.thread_variable_get(CONTEXT_VAR).is_a?(Wrap)
    Thread.current.thread_variable_get(CONTEXT_VAR).value
  else
    @context
  end
end

#default_app_keyObject

Returns the value of attribute default_app_key.



100
101
102
# File 'lib/queue_bus/config.rb', line 100

def default_app_key
  @default_app_key
end

#default_queueObject

Returns the value of attribute default_queue.



9
10
11
# File 'lib/queue_bus/config.rb', line 9

def default_queue
  @default_queue
end

#hostnameObject

Returns the value of attribute hostname.



9
10
11
# File 'lib/queue_bus/config.rb', line 9

def hostname
  @hostname
end

#incoming_queueObject

Returns the value of attribute incoming_queue.



9
10
11
# File 'lib/queue_bus/config.rb', line 9

def incoming_queue
  @incoming_queue
end

#local_modeObject

Returns the current local mode of QueueBus



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

def local_mode
  if Thread.current.thread_variable_get(LOCAL_MODE_VAR).is_a?(Wrap)
    Thread.current.thread_variable_get(LOCAL_MODE_VAR).value
  else
    @local_mode
  end
end

#loggerObject

Returns the value of attribute logger.



9
10
11
# File 'lib/queue_bus/config.rb', line 9

def logger
  @logger
end

#worker_middleware_stackObject (readonly)

Returns the value of attribute worker_middleware_stack.



11
12
13
# File 'lib/queue_bus/config.rb', line 11

def worker_middleware_stack
  @worker_middleware_stack
end

Instance Method Details

#adapterObject



84
85
86
87
88
# File 'lib/queue_bus/config.rb', line 84

def adapter
  return @adapter_instance if has_adapter?

  raise 'no adapter has been set'
end

#adapter=(val) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/queue_bus/config.rb', line 70

def adapter=(val)
  raise "Adapter already set to #{@adapter_instance.class.name}" if has_adapter?

  @adapter_instance =
    if val.is_a?(Class)
      val.new
    elsif val.is_a?(::QueueBus::Adapters::Base)
      val
    else
      class_name = ::QueueBus::Util.classify(val)
      ::QueueBus::Util.constantize("::QueueBus::Adapters::#{class_name}").new
    end
end

#before_publish=(callback) ⇒ Object



105
106
107
# File 'lib/queue_bus/config.rb', line 105

def before_publish=(callback)
  @before_publish_callback = callback
end

#before_publish_callback(attributes) ⇒ Object



109
110
111
# File 'lib/queue_bus/config.rb', line 109

def before_publish_callback(attributes)
  @before_publish_callback&.call(attributes)
end

#has_adapter?Boolean

Checks whether an adapter is set and returns true if it is.

Returns:

  • (Boolean)


91
92
93
# File 'lib/queue_bus/config.rb', line 91

def has_adapter? # rubocop:disable Naming/PredicateName
  !@adapter_instance.nil?
end

#in_context(context) ⇒ Object

Overrides the current bus context (if any) for the duration of a block, adding a ‘bus_context` attribute set to this value for all events published in this scope. This is a threadsafe implementation. After, the global setting will be resumed.



62
63
64
65
66
67
68
# File 'lib/queue_bus/config.rb', line 62

def in_context(context)
  previous = Thread.current.thread_variable_get(CONTEXT_VAR)
  Thread.current.thread_variable_set(CONTEXT_VAR, Wrap.new(context))
  yield if block_given?
ensure
  Thread.current.thread_variable_set(CONTEXT_VAR, previous)
end

#log_application(message) ⇒ Object



113
114
115
# File 'lib/queue_bus/config.rb', line 113

def log_application(message)
  logger&.info(message)
end

#log_worker(message) ⇒ Object



117
118
119
# File 'lib/queue_bus/config.rb', line 117

def log_worker(message)
  logger&.debug(message)
end

#redis(&block) ⇒ Object



95
96
97
98
# File 'lib/queue_bus/config.rb', line 95

def redis(&block)
  # TODO: could allow setting for non-redis adapters
  adapter.redis(&block)
end

#with_local_mode(mode) ⇒ Object

Overrides the current local mode for the duration of a block. This is a threadsafe implementation. After, the global setting will be resumed.

Parameters:

  • mode (Symbol)

    the mode to switch to



51
52
53
54
55
56
57
# File 'lib/queue_bus/config.rb', line 51

def with_local_mode(mode)
  previous = Thread.current.thread_variable_get(LOCAL_MODE_VAR)
  Thread.current.thread_variable_set(LOCAL_MODE_VAR, Wrap.new(mode))
  yield if block_given?
ensure
  Thread.current.thread_variable_set(LOCAL_MODE_VAR, previous)
end