Class: Qwirk::Adapter::Base::WorkerConfig

Inherits:
Object
  • Object
show all
Includes:
Rumx::Bean
Defined in:
lib/qwirk/adapter/base/worker_config.rb

Direct Known Subclasses

ExpandingWorkerConfig, Inline::WorkerConfig

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter_factory, name, manager, worker_class, default_options, options) ⇒ WorkerConfig

Create new WorkerConfig to manage workers of a common class



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
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/qwirk/adapter/base/worker_config.rb', line 32

def initialize(adapter_factory, name, manager, worker_class, default_options, options)
  @adapter_factory  = adapter_factory
  @name             = name
  @manager          = manager
  @worker_class     = worker_class
  @default_options  = default_options.dup
  @options          = options.dup
  # First option is getting the queue or topic from the given options
  @queue_name       = options.delete(:queue_name)
  @topic_name       = !@queue_name && options.delete(:queue_name)
  # Second option is getting them from the default options
  @queue_name     ||= !@topic_name && default_options.delete(:queue_name)
  @topic_name     ||= !@queue_name && default_options.delete(:topic_name)
  # Third (and most likely) option is getting it based on the class topic or queue DSL or defaulting to the default queue
  @queue_name     ||= !@topic_name && worker_class.queue_name(@name)
  @topic_name     ||= !@queue_name && worker_class.topic_name

  if @queue_name
    more_queue_options = (default_options.delete(:queue_options) || {}).merge(options.delete(:queue_options) || {})
  else
    more_queue_options = (default_options.delete(:topic_options) || {}).merge(options.delete(:topic_options) || {})
  end
  @queue_options    = worker_class.queue_options.merge(more_queue_options)
  @response_options = @queue_options[:response] || {}

  # Defines how we will marshal the response
  marshal_sym       = (response_options[:marshal] || self.class.default_marshal_sym)
  @marshaler        = MarshalStrategy.find(marshal_sym)
  @log_times        = adapter_factory.log_times

  init

  #Qwirk.logger.debug { "options=#{options.inspect}" }
  default_options.each do |key, value|
    begin
      send(key.to_s+'=', value)
    rescue Exception => e
      # Let config_reader's set a default value
      begin
        instance_variable_set("@#{key}", value)
      rescue Exception => e
        Qwirk.logger.debug "DEBUG: During initialization of #{worker_class.name} config=#{@name}, default assignment of #{key}=#{value} was ignored"
      end
    end
  end
  # Run the specified options after the default options, so that codependant options don't get overwritten (like min_count/max_count)
  options.each do |key, value|
    begin
      send(key.to_s+'=', value)
    rescue Exception => e
      Qwirk.logger.debug "DEBUG: During initialization of #{worker_class.name} config=#{@name}, assignment of #{key}=#{value} was ignored"
    end
  end
end

Instance Attribute Details

#adapter_factoryObject (readonly)

Make explicit the instance variables available to the derived adapter classes



10
11
12
# File 'lib/qwirk/adapter/base/worker_config.rb', line 10

def adapter_factory
  @adapter_factory
end

#default_optionsObject (readonly)

Make explicit the instance variables available to the derived adapter classes



10
11
12
# File 'lib/qwirk/adapter/base/worker_config.rb', line 10

def default_options
  @default_options
end

#managerObject (readonly)

Make explicit the instance variables available to the derived adapter classes



10
11
12
# File 'lib/qwirk/adapter/base/worker_config.rb', line 10

def manager
  @manager
end

#marshalerObject (readonly)

Make explicit the instance variables available to the derived adapter classes



10
11
12
# File 'lib/qwirk/adapter/base/worker_config.rb', line 10

def marshaler
  @marshaler
end

#nameObject (readonly)

Make explicit the instance variables available to the derived adapter classes



10
11
12
# File 'lib/qwirk/adapter/base/worker_config.rb', line 10

def name
  @name
end

#optionsObject (readonly)

Make explicit the instance variables available to the derived adapter classes



10
11
12
# File 'lib/qwirk/adapter/base/worker_config.rb', line 10

def options
  @options
end

#queue_nameObject

Returns the value of attribute queue_name.



12
13
14
# File 'lib/qwirk/adapter/base/worker_config.rb', line 12

def queue_name
  @queue_name
end

#queue_optionsObject (readonly)

Make explicit the instance variables available to the derived adapter classes



10
11
12
# File 'lib/qwirk/adapter/base/worker_config.rb', line 10

def queue_options
  @queue_options
end

#response_optionsObject (readonly)

Make explicit the instance variables available to the derived adapter classes



10
11
12
# File 'lib/qwirk/adapter/base/worker_config.rb', line 10

def response_options
  @response_options
end

#topic_nameObject

Returns the value of attribute topic_name.



12
13
14
# File 'lib/qwirk/adapter/base/worker_config.rb', line 12

def topic_name
  @topic_name
end

#worker_classObject (readonly)

Make explicit the instance variables available to the derived adapter classes



10
11
12
# File 'lib/qwirk/adapter/base/worker_config.rb', line 10

def worker_class
  @worker_class
end

Class Method Details

.default_marshal_symObject



22
23
24
# File 'lib/qwirk/adapter/base/worker_config.rb', line 22

def self.default_marshal_sym
  :ruby
end

.in_process?(config) ⇒ Boolean

By default, workers do not run in the same process as the publishers

Returns:

  • (Boolean)


27
28
29
# File 'lib/qwirk/adapter/base/worker_config.rb', line 27

def self.in_process?(config)
  false
end

.initial_default_configObject

Define the default config values for the attributes all workers will share. These will be sent as options to the constructor



18
19
20
# File 'lib/qwirk/adapter/base/worker_config.rb', line 18

def self.initial_default_config
  {}
end

Instance Method Details

#bean_attributes_changedObject

Override rumx bean method



105
106
107
108
# File 'lib/qwirk/adapter/base/worker_config.rb', line 105

def bean_attributes_changed
  super
  @manager.save_persist_state
end

#initObject

Allow extensions to initialize before setting the attributes



88
89
# File 'lib/qwirk/adapter/base/worker_config.rb', line 88

def init
end

#join(timeout = nil) ⇒ Object



94
95
# File 'lib/qwirk/adapter/base/worker_config.rb', line 94

def join(timeout=nil)
end

#marshal_response(object) ⇒ Object



110
111
112
# File 'lib/qwirk/adapter/base/worker_config.rb', line 110

def marshal_response(object)
  @marshaler.marshal(object)
end

#periodic_call(poll_time) ⇒ Object



118
119
# File 'lib/qwirk/adapter/base/worker_config.rb', line 118

def periodic_call(poll_time)
end

#stopObject



91
92
# File 'lib/qwirk/adapter/base/worker_config.rb', line 91

def stop
end

#stopped?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/qwirk/adapter/base/worker_config.rb', line 97

def stopped?
  self.manager.stopped?
end

#to_sObject



121
122
123
# File 'lib/qwirk/adapter/base/worker_config.rb', line 121

def to_s
  @name
end

#unmarshal_response(marshaled_object) ⇒ Object



114
115
116
# File 'lib/qwirk/adapter/base/worker_config.rb', line 114

def unmarshal_response(marshaled_object)
  @marshaler.unmarshal(marshaled_object)
end

#worker_stopped(worker) ⇒ Object



101
102
# File 'lib/qwirk/adapter/base/worker_config.rb', line 101

def worker_stopped(worker)
end