Module: Wurk::JobUtil

Included in:
Client, Worker::Setter
Defined in:
lib/wurk/job_util.rb

Overview

Mixin shared by Wurk::Client (and Wurk::Job::Setter) to validate, normalize, and JSON-verify job payloads before they hit Redis.

Spec: docs/target/sidekiq-free.md §9 (Sidekiq::JobUtil).

Constant Summary collapse

TRANSIENT_ATTRIBUTES =

Top-level keys consumed at enqueue time but stripped from every payload before raw_push — they must never reach the wire (spec §2.2):

`pool`         selects the Redis pool (resolved in client_push/build_client)
`client_class` swaps the enqueue client (Wurk.transactional_push!)

Both carry non-JSON values (a pool / a Class). Baked into the literal rather than appended at load: a load-time ‘<<` is fragile under the parallel test runner (a test that add/deletes the same key clobbers it for later suites). Still mutable so other extensions can append without monkey-patching.

%w[pool client_class]
RETRY_FOR_MAX =

rubocop:disable Style/MutableConstant

1_000_000_000

Instance Method Summary collapse

Instance Method Details

#normalize_item(item) ⇒ Object

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



50
51
52
53
54
55
56
# File 'lib/wurk/job_util.rb', line 50

def normalize_item(item)
  validate(item)
  normalized = class_defaults_for(item['class']).merge(item)
  normalized = wrap_options(normalized)
  stringify_identity!(normalized, item['class'])
  finalize(normalized)
end

#now_in_millisObject



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

def now_in_millis
  ::Process.clock_gettime(::Process::CLOCK_REALTIME, :millisecond)
end

#validate(item) ⇒ Object

Raises:

  • (ArgumentError)

    if the payload is structurally invalid.



25
26
27
28
29
30
31
32
33
34
# File 'lib/wurk/job_util.rb', line 25

def validate(item)
  raise(ArgumentError, "Job must be a Hash with 'class' and 'args' keys: `#{item}`") unless valid_shape?(item)
  raise(ArgumentError, "Job args must be an Array: `#{item}`") unless item['args'].is_a?(Array)
  raise(ArgumentError, "Job class must be a Class or String: `#{item}`") unless valid_class?(item['class'])
  raise(ArgumentError, "Job 'at' must be a Numeric timestamp: `#{item}`") unless valid_at?(item)
  raise(ArgumentError, "Job tags must be an Array: `#{item}`") unless valid_tags?(item)
  return if valid_retry_for?(item)

  raise(ArgumentError, "Job retry_for over #{RETRY_FOR_MAX} is unreasonable: `#{item}`")
end

#verify_json(item) ⇒ Object

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



38
39
40
41
42
43
44
45
46
# File 'lib/wurk/job_util.rb', line 38

def verify_json(item)
  mode = Wurk.strict_args_mode
  return if mode == false

  offender = json_unsafe(item['args'])
  return if offender.nil?

  report_unsafe(item, offender, mode)
end