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]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ SidekiqInstance

Returns a new instance of SidekiqInstance.



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

def initialize(name)
  fail "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
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#queuesObject (readonly)

Returns the value of attribute queues.



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

def queues
  @queues
end

Instance Method Details

#add_queue(queue_name, weight = 1) ⇒ Object



29
30
31
32
33
34
# File 'lib/sidekiq-runner/sidekiq_instance.rb', line 29

def add_queue(queue_name, weight = 1)
  fail "Cannot add the queue. The name is empty!" if queue_name.empty?
  fail "Cannot add the queue. The weight is not an integer!" unless weight.is_a? Integer
  fail "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



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/sidekiq-runner/sidekiq_instance.rb', line 57

def build_start_command
  create_directories!

  cmd = []
  cmd << 'bundle exec' if bundle_env
  cmd << (rbtrace ? File.expand_path('../../../script/sidekiq_rbtrace', __FILE__) : 'sidekiq')
  cmd << '-d'
  cmd << "-c #{concurrency}"
  cmd << '-v' if verbose
  cmd << "-L #{logfile}"
  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.to_s}"
  end

  cmd.join(' ')
end

#build_stop_command(timeout) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'lib/sidekiq-runner/sidekiq_instance.rb', line 79

def build_stop_command(timeout)
  cmd = []
  cmd << (bundle_env ? 'bundle exec sidekiqctl' : 'sidekiqctl')
  cmd << 'stop'
  cmd << pidfile
  cmd << timeout

  cmd.join(' ')
end

#merge_config_file!(yml) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/sidekiq-runner/sidekiq_instance.rb', line 36

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)


52
53
54
55
# File 'lib/sidekiq-runner/sidekiq_instance.rb', line 52

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