Class: SidekiqRunner::SidekiqInstance

Inherits:
Object
  • Object
show all
Defined in:
lib/sidekiq-runner/sidekiq_instance.rb

Constant Summary collapse

RUNNER_ATTRIBUTES =
[:bundle_env, :chdir, :requirefile]
CONFIG_FILE_ATTRIBUTES =
[:concurrency, :verbose, :pidfile, :logfile, :tag, :rbtrace, :uid, :gid]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ SidekiqInstance

Returns a new instance of SidekiqInstance.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/sidekiq-runner/sidekiq_instance.rb', line 11

def initialize(name)
  raise "No sidekiq instance name given!" if name.empty?

  @name = name
  @queues = []

  @bundle_env   = true
  @chdir        = nil
  @requirefile  = nil
  @pidfile      = File.join(Dir.pwd, 'tmp', 'pids', "#{@name}.pid")
  @logfile      = File.join(Dir.pwd, 'log', "#{@name}.log")
  @concurrency  = 4
  @verbose      = false
  @tag          = name
  @rbtrace      = false
  @uid          = nil
  @gid          = nil
  @config_blocks = []
end

Instance Attribute Details

#config_blocksObject (readonly)

Returns the value of attribute config_blocks.



9
10
11
# File 'lib/sidekiq-runner/sidekiq_instance.rb', line 9

def config_blocks
  @config_blocks
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/sidekiq-runner/sidekiq_instance.rb', line 9

def name
  @name
end

#queuesObject (readonly)

Returns the value of attribute queues.



9
10
11
# File 'lib/sidekiq-runner/sidekiq_instance.rb', line 9

def queues
  @queues
end

Instance Method Details

#add_queue(queue_name, weight = 1) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/sidekiq-runner/sidekiq_instance.rb', line 31

def add_queue(queue_name, weight = 1)
  raise "Cannot add the queue. The name is empty!" if queue_name.empty?
  raise "Cannot add the queue. The weight is not an integer!" unless weight.is_a? Integer
  raise "Cannot add the queue. The queue with \"#{queue_name}\" name already exist" if @queues.any? { |q| q.first == queue_name }

  @queues << [queue_name, weight]
end

#build_start_commandObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/sidekiq-runner/sidekiq_instance.rb', line 64

def build_start_command
  create_directories!

  cmd = []
  cmd << 'bundle exec' if bundle_env
  cmd << (rbtrace ? File.expand_path('../../script/sidekiq_rbtrace', __dir__) : 'sidekiq')
  cmd << "-c #{concurrency}"
  cmd << '-v' if verbose
  cmd << "-P #{pidfile}"
  cmd << "-e #{Rails.env}" if defined?(Rails)
  cmd << "-r #{requirefile}" if requirefile
  cmd << "-g '#{tag}'"

  queues.each do |q, w|
    cmd << "-q #{q},#{w}"
  end

  cmd.join(' ')
end

#god_config(&block) ⇒ Object



39
40
41
# File 'lib/sidekiq-runner/sidekiq_instance.rb', line 39

def god_config(&block)
  @config_blocks << block
end

#merge_config_file!(yml) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/sidekiq-runner/sidekiq_instance.rb', line 43

def merge_config_file!(yml)
  # Get global configuration options.
  SidekiqInstance::CONFIG_FILE_ATTRIBUTES.each do |k|
    send("#{k}=", yml[k]) unless yml[k].nil?
  end

  # Override with instance-specific options.
  if (syml = yml[@name.to_sym]) && syml.is_a?(Hash)
    syml = Hash[syml.map { |k, v| [k.to_sym, v] }]

    SidekiqInstance::CONFIG_FILE_ATTRIBUTES.each do |k|
      send("#{k}=", syml[k]) unless syml[k].nil?
    end
  end
end

#sane?Boolean

Returns:

  • (Boolean)


59
60
61
62
# File 'lib/sidekiq-runner/sidekiq_instance.rb', line 59

def sane?
  raise "No queues given for #{@name}!" if @queues.empty?
  raise "No requirefile given for #{@name} and not in Rails environment!" if !defined?(Rails) && !requirefile
end