Module: Wurk::Worker

Defined in:
lib/wurk/worker.rb,
lib/wurk/worker/setter.rb

Overview

The user-facing job DSL. include Wurk::Worker — or its modern alias Sidekiq::Job / Sidekiq::Worker — onto a class to make it a background job. The class gains sidekiq_options, the perform_* enqueue methods, set, and the retry-hook DSL; each instance gains jid, logger, interrupted?, and the batch helpers.

Spec: docs/target/sidekiq-free.md §6 (Sidekiq::Job).

Examples:

A minimal job

class HardJob
  include Sidekiq::Job
  sidekiq_options queue: "critical", retry: 5

  def perform(user_id, opts = {})
    # ... your work ...
  end
end

HardJob.perform_async(42, "fast" => true)   # enqueue now
HardJob.perform_in(5.minutes, 42)           # enqueue later

See Also:

Defined Under Namespace

Modules: ClassMethods Classes: Setter

Constant Summary collapse

SCHEDULED_THRESHOLD =

Interval values below this threshold are interpreted as seconds-from-now; values at or above are treated as absolute epoch timestamps. Threshold matches Sidekiq exactly — wire-compat sacred.

1_000_000_000

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bidObject

Batch helpers (Pro). Available on every worker — return nil when the current job did not originate from a batch.

Spec: docs/target/sidekiq-pro.md §2.6.



67
68
69
# File 'lib/wurk/worker.rb', line 67

def bid
  @bid
end

#statusObject

Progress handle for a class that opted into tracking with sidekiq_options track: truestatus.at(row, total, 'importing'). nil for every other class, so callers that report progress conditionally write status&.at(...).

Defined as a plain reader rather than pushed onto the including class the way jid is: status is a name a job may well already own, and a method in this module loses to one in the class body, which is the right way round for a Wurk extra.

See Also:



91
92
93
# File 'lib/wurk/worker.rb', line 91

def status
  @status
end

Class Method Details

.clear_allObject



49
# File 'lib/wurk/worker.rb', line 49

def self.clear_all  = ::Wurk::Queues.clear_all

.drain_allObject



50
# File 'lib/wurk/worker.rb', line 50

def self.drain_all  = ::Wurk::Testing.drain_all

.included(base) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/wurk/worker.rb', line 37

def self.included(base)
  base.extend(ClassMethods)
  base.module_eval { attr_accessor :jid, :_context }
  base.singleton_class.module_eval do
    attr_accessor :sidekiq_retry_in_block, :sidekiq_retries_exhausted_block
  end
end

.jobsObject

Module-level test helpers: Sidekiq::Worker.jobs / clear_all / drain_all operate across every job class (spec §24.3). Resolved lazily so the testing constants need not be loaded when Worker is.



48
# File 'lib/wurk/worker.rb', line 48

def self.jobs       = ::Wurk::Queues.jobs

Instance Method Details

#batchObject



74
75
76
77
78
# File 'lib/wurk/worker.rb', line 74

def batch
  return nil if @bid.nil?

  Wurk::Batch.new(@bid)
end

#interrupted?Boolean

Cooperative cancellation flag for IterableJob and long-running jobs. Returns false when no processor context has been attached.

Returns:

  • (Boolean)


58
59
60
61
# File 'lib/wurk/worker.rb', line 58

def interrupted?
  ctx = @_context
  ctx.respond_to?(:stopping?) && ctx.stopping?
end

#loggerObject



52
53
54
# File 'lib/wurk/worker.rb', line 52

def logger
  Wurk.logger
end

#valid_within_batch?Boolean

False if the batch was invalidated. Workers should return unless valid_within_batch? to short-circuit work for cancelled batches.

Returns:

  • (Boolean)


100
101
102
103
104
# File 'lib/wurk/worker.rb', line 100

def valid_within_batch?
  return true if @bid.nil?

  batch.valid?
end