Class: Sidekiq::Processor

Inherits:
Object
  • Object
show all
Includes:
Component
Defined in:
lib/sidekiq/processor.rb

Overview

The Processor is a standalone thread which:

  1. fetches a job from Redis

  2. executes the job

a. instantiate the job class
b. run the middleware chain
c. call #perform

A Processor can exit due to shutdown or due to an error during job execution.

If an error occurs in the job execution, the Processor calls the Manager to create a new one to replace itself and exits.

Defined Under Namespace

Classes: Counter, SharedWorkState

Instance Attribute Summary collapse

Attributes included from Component

#config

Instance Method Summary collapse

Methods included from Component

#default_tag, #fire_event, #handle_exception, #hostname, #identity, #inspect, #logger, #mono_ms, #process_nonce, #real_ms, #redis, #safe_thread, #tid, #watchdog

Constructor Details

#initialize(capsule, &block) ⇒ Processor

Returns a new instance of Processor.



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/sidekiq/processor.rb', line 32

def initialize(capsule, &block)
  @config = @capsule = capsule
  @callback = block
  @down = false
  @done = false
  @job = nil
  @thread = nil
  @reloader = Sidekiq.default_configuration[:reloader]
  @job_logger = (capsule.config[:job_logger] || Sidekiq::JobLogger).new(capsule.config)
  @retrier = Sidekiq::JobRetry.new(capsule)
end

Instance Attribute Details

#capsuleObject (readonly)

Returns the value of attribute capsule.



30
31
32
# File 'lib/sidekiq/processor.rb', line 30

def capsule
  @capsule
end

#jobObject (readonly)

Returns the value of attribute job.



29
30
31
# File 'lib/sidekiq/processor.rb', line 29

def job
  @job
end

#threadObject (readonly)

Returns the value of attribute thread.



28
29
30
# File 'lib/sidekiq/processor.rb', line 28

def thread
  @thread
end

Instance Method Details

#kill(wait = false) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/sidekiq/processor.rb', line 50

def kill(wait = false)
  @done = true
  return unless @thread
  # unlike the other actors, terminate does not wait
  # for the thread to finish because we don't know how
  # long the job will take to finish.  Instead we
  # provide a `kill` method to call after the shutdown
  # timeout passes.
  @thread.raise ::Sidekiq::Shutdown
  @thread.value if wait
end

#startObject



66
67
68
# File 'lib/sidekiq/processor.rb', line 66

def start
  @thread ||= safe_thread("#{config.name}/processor", &method(:run))
end

#stopping?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/sidekiq/processor.rb', line 62

def stopping?
  @done
end

#terminate(wait = false) ⇒ Object



44
45
46
47
48
# File 'lib/sidekiq/processor.rb', line 44

def terminate(wait = false)
  @done = true
  return unless @thread
  @thread.value if wait
end