Class: SidekiqRunner::SidekiqConfiguration

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/sidekiq-runner/sidekiq_configuration.rb

Constant Summary collapse

RUNNER_ATTRIBUTES =
[:config_file]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSidekiqConfiguration

Returns a new instance of SidekiqConfiguration.


12
13
14
15
16
17
18
19
20
21
# File 'lib/sidekiq-runner/sidekiq_configuration.rb', line 12

def initialize
  @config_file =
    if defined?(Rails)
      File.join(Rails.root, 'config', 'sidekiq.yml')
    else
      File.join(Dir.pwd, 'config', 'sidekiq.yml')
    end

  @sidekiqs    = {}
end

Instance Attribute Details

#sidekiqsObject (readonly)

Returns the value of attribute sidekiqs


10
11
12
# File 'lib/sidekiq-runner/sidekiq_configuration.rb', line 10

def sidekiqs
  @sidekiqs
end

Class Method Details

.defaultObject


23
24
25
# File 'lib/sidekiq-runner/sidekiq_configuration.rb', line 23

def self.default
  @default ||= SidekiqConfiguration.new
end

.getObject


27
28
29
30
31
32
# File 'lib/sidekiq-runner/sidekiq_configuration.rb', line 27

def self.get
  config = default.dup
  config.merge_config_file!
  config.sane?
  config
end

Instance Method Details

#add_instance(name) {|@sidekiqs[name]| ... } ⇒ Object

Yields:


46
47
48
49
50
51
# File 'lib/sidekiq-runner/sidekiq_configuration.rb', line 46

def add_instance(name)
  fail "Sidekiq instance with the name '#{name}' already exists! No duplicates please." if @sidekiqs.key?(name)

  @sidekiqs[name] = SidekiqInstance.new(name)
  yield @sidekiqs[name] if block_given?
end

#each(&block) ⇒ Object


34
35
36
# File 'lib/sidekiq-runner/sidekiq_configuration.rb', line 34

def each(&block)
  @sidekiqs.each(&block)
end

#each_key(&block) ⇒ Object


38
39
40
# File 'lib/sidekiq-runner/sidekiq_configuration.rb', line 38

def each_key(&block)
  @sidekiqs.each_key(&block)
end

#empty?Boolean

Returns:

  • (Boolean)

42
43
44
# File 'lib/sidekiq-runner/sidekiq_configuration.rb', line 42

def empty?
  @sidekiqs.empty?
end

#ensure_default_sidekiq!Object


84
85
86
# File 'lib/sidekiq-runner/sidekiq_configuration.rb', line 84

def ensure_default_sidekiq!
  add_instance('sidekiq_default') if empty?
end

#merge_config_file!Object


88
89
90
91
92
93
# File 'lib/sidekiq-runner/sidekiq_configuration.rb', line 88

def merge_config_file!
  yml = File.exist?(config_file) ? YAML.load_file(config_file) : {}
  yml = Hash[yml.map { |k, v| [k.to_sym, v] }]

  @sidekiqs.each_value { |skiq| skiq.merge_config_file!(yml) }
end

#queue(name, weight = 1) ⇒ Object Also known as: add_queue


62
63
64
65
66
67
# File 'lib/sidekiq-runner/sidekiq_configuration.rb', line 62

def queue(name, weight = 1)
  fail 'Multiple Sidekiq instances defined and queue() outside of instance block called.' if @sidekiqs.size > 1

  ensure_default_sidekiq!
  @sidekiqs.values.first.add_queue(name, weight)
end

#sane?Boolean

Returns:

  • (Boolean)

95
96
97
98
99
# File 'lib/sidekiq-runner/sidekiq_configuration.rb', line 95

def sane?
  fail 'No sidekiq instances defined. Nothing to run.' if @sidekiqs.empty?

  @sidekiqs.each_value { |skiq| skiq.sane? }
end