Class: Wurk::Worker::Setter

Inherits:
Object
  • Object
show all
Includes:
JobUtil
Defined in:
lib/wurk/worker/setter.rb

Overview

Aliased as Sidekiq::Job::Setter. Per-call option carrier returned by Worker.set(opts). Holds string-keyed overrides and exposes the same perform_* surface as the worker class itself.

set(sync: true) makes perform_async invoke perform_inline — required for testing-mode parity.

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

Instance Method Summary collapse

Constructor Details

#initialize(klass, opts) ⇒ Setter

Returns a new instance of Setter.



20
21
22
23
# File 'lib/wurk/worker/setter.rb', line 20

def initialize(klass, opts)
  @klass = klass
  @opts = normalize_opts(opts)
end

Instance Method Details

#normalize_item(item) ⇒ Object Originally defined in module JobUtil

Validate → merge class/default options → stringify → assign jid & created_at → strip transient keys. Returns the canonical payload.

#now_in_millis ⇒ Object Originally defined in module JobUtil

#perform_async(*args) ⇒ Object



30
31
32
33
34
# File 'lib/wurk/worker/setter.rb', line 30

def perform_async(*args)
  return perform_inline(*args) if @opts['sync']

  @klass.client_push(@opts.merge('class' => @klass, 'args' => args))
end

#perform_bulk(args, **opts) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/wurk/worker/setter.rb', line 65

def perform_bulk(args, **opts)
  merged = @opts.merge(opts.transform_keys(&:to_s)).merge(
    'class' => @klass,
    'args' => args
  )
  # Mirror client_push: a per-call `set(pool:)` selects the Redis pool and
  # is removed so it never persists (normalize_item strips the class-level
  # pool re-merged into each payload).
  pool = merged.delete('pool') || @klass.get_sidekiq_options['pool']
  @klass.build_client(pool).push_bulk(merged)
end

#perform_in(interval, *args) ⇒ Object Also known as: perform_at



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

def perform_in(interval, *args)
  ts = absolute_at(interval)
  item = @opts.merge('class' => @klass, 'args' => args)
  item['at'] = ts if ts && ts > now_seconds
  @klass.client_push(item)
end

#perform_inline(*args) ⇒ Object Also known as: perform_sync

Explicit synchronous execution. Runs the payload through the real client AND server middleware chains rather than calling perform directly: unique-job locks are taken by the client chain and released only by the server chain (bypassing both leaked every lock until its TTL), and batch callbacks fire from server middleware. Mirrors Sidekiq's Setter#perform_inline, including its return contract — nil when middleware halts the job, else true.



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/wurk/worker/setter.rb', line 42

def perform_inline(*args)
  config = Wurk.configuration
  item = normalize_item(@opts.merge('class' => @klass, 'args' => args))
  pushed = config.client_middleware.invoke(item['class'], item, item['queue'], config.redis_pool) do
    verify_json(item)
    item
  end
  return nil unless pushed

  # Round-trip through JSON so the job sees exactly the arguments a real
  # worker would after the payload has crossed Redis.
  run_job(Wurk.load_json(Wurk.dump_json(item)), config)
end

#set(options) ⇒ Object



25
26
27
28
# File 'lib/wurk/worker/setter.rb', line 25

def set(options)
  @opts.merge!(normalize_opts(options))
  self
end

#validate(item) ⇒ Object Originally defined in module JobUtil

Raises:

  • (ArgumentError) —

    if the payload is structurally invalid.

#verify_json(item) ⇒ Object Originally defined in module JobUtil

Walk args; report the first non-JSON-native value according to the configured strict mode. Hash keys must be Strings.