Class: Sidekiq::BackgroundManager

Inherits:
Object
  • Object
show all
Includes:
Singleton, Util
Defined in:
lib/trinidad_sidekiq_extension/background_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBackgroundManager

Returns a new instance of BackgroundManager.



24
25
26
27
28
# File 'lib/trinidad_sidekiq_extension/background_manager.rb', line 24

def initialize
  @code            = nil
  @interrupt_mutex = Mutex.new
  @interrupted     = false
end

Instance Attribute Details

#codeObject

Used for BM testing



20
21
22
# File 'lib/trinidad_sidekiq_extension/background_manager.rb', line 20

def code
  @code
end

#interruptedObject (readonly)

Returns the value of attribute interrupted.



22
23
24
# File 'lib/trinidad_sidekiq_extension/background_manager.rb', line 22

def interrupted
  @interrupted
end

#managerObject

Returns the value of attribute manager.



21
22
23
# File 'lib/trinidad_sidekiq_extension/background_manager.rb', line 21

def manager
  @manager
end

Instance Method Details

#configure(given_options = { }) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/trinidad_sidekiq_extension/background_manager.rb', line 30

def configure(given_options = { })
  @code = nil
  Sidekiq.logger

  logger.debug "BM configure : options : #{options.inspect}" if options[:verbose]
  logger.debug "BM configure : given_options : #{given_options.inspect}" if options[:verbose]
  config_options = parse_config given_options
  logger.debug "BM configure : config_options : #{config_options.inspect}" if options[:verbose]
  options.merge! config_options
  logger.debug "BM configure : options : #{options.inspect}" if options[:verbose]

  Sidekiq.logger.level = Logger::DEBUG if options[:verbose]
  Celluloid.logger = nil unless options[:verbose]

  validate!
  boot_system
end

#interruptObject



72
73
74
75
76
77
78
79
80
# File 'lib/trinidad_sidekiq_extension/background_manager.rb', line 72

def interrupt
  @interrupt_mutex.synchronize do
    unless @interrupted
      logger.debug "BM interrupting ..."
      @interrupted = true
      Thread.main.raise Interrupt
    end
  end
end

#runObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/trinidad_sidekiq_extension/background_manager.rb', line 48

def run
  logger.debug "Booting Sidekiq BM #{Sidekiq::VERSION} with Redis at #{redis { |x| x.client.id }}"
  logger.debug "Running in #{RUBY_DESCRIPTION}"
  logger.debug "Dir.pwd : #{Dir.pwd}"
  logger.debug Sidekiq::LICENSE

  @manager = Sidekiq::Manager.new(options)
  poller   = Sidekiq::Scheduled::Poller.new
  begin
    logger.debug 'BM Starting processing ...'
    @manager.start!
    poller.poll!(true)
    sleep
  rescue Interrupt
    logger.debug 'BM Shutting down ...'
    poller.terminate! if poller.alive?
    @manager.stop!(:shutdown => true, :timeout => options[:timeout])
    @manager.wait(:shutdown)
    # WILL NOT explicitly exit because Trinidad will take care of exiting
    # in its own sweet time.
    # exit(0)
  end
end