Class: Sidecloq::Runner

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/sidecloq/runner.rb

Overview

Runner encapsulates a locker and a scheduler, running scheduler when “elected” leader

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

included, #logger, #redis, #will_never_run

Constructor Details

#initialize(options = {}) ⇒ Runner

Returns a new instance of Runner.



9
10
11
12
# File 'lib/sidecloq/runner.rb', line 9

def initialize(options = {})
  @locker = extract_locker(options)
  @scheduler = extract_scheduler(options)
end

Instance Attribute Details

#lockerObject (readonly)

Returns the value of attribute locker.



7
8
9
# File 'lib/sidecloq/runner.rb', line 7

def locker
  @locker
end

#schedulerObject (readonly)

Returns the value of attribute scheduler.



7
8
9
# File 'lib/sidecloq/runner.rb', line 7

def scheduler
  @scheduler
end

Instance Method Details

#extract_locker(options) ⇒ Object



36
37
38
39
# File 'lib/sidecloq/runner.rb', line 36

def extract_locker(options)
  return options[:locker] if options[:locker]
  Locker.new(options[:locker_options] || {})
end

#extract_scheduler(options) ⇒ Object



41
42
43
44
# File 'lib/sidecloq/runner.rb', line 41

def extract_scheduler(options)
  return options[:scheduler] if options[:scheduler]
  Scheduler.new(options[:schedule], options[:scheduler_options] || {})
end

#runObject



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/sidecloq/runner.rb', line 14

def run
  @thread = Thread.new do
    logger.info('Runner starting')
    @locker.with_lock do
      # i am the leader
      logger.info('Obtained leader lock')
      @scheduler.run
    end
    logger.info('Runner ending')
  end
end

#stop(timeout = nil) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/sidecloq/runner.rb', line 26

def stop(timeout = nil)
  logger.debug('Stopping runner')
  @scheduler.stop(timeout)
  @locker.stop(timeout)
  @thread.join(timeout) if @thread
  logger.debug('Stopped runner')
end