Module: Zizq::Job::ClassMethods

Includes:
Zizq::JobConfig
Defined in:
lib/zizq/job.rb,
sig/generated/zizq/job.rbs

Instance Method Summary collapse

Methods included from Zizq::JobConfig

#name, #normalize_payload, #zizq_backoff, #zizq_enqueue_request, #zizq_priority, #zizq_queue, #zizq_retention, #zizq_retry_limit, #zizq_unique, #zizq_unique_key, #zizq_unique_scope

Instance Method Details

#zizq_deserialize(payload) ⇒ Object

Deserialize a payload hash back into positional and keyword arguments.

The payload is a JSON decoded Hash.

The default implementation receives a Hash of the form:

{ "args" => [ 42, "Hello" ], "kwargs" => { "template": "example" } }

And returns an array for args and kwargs of the form:

[ [ 42, "Hello" ], {template: "example"} ]

Because the default implementation uses a JSON decoded Hash, any symbol keys that were present at enqueue-time will be string keys after decoding.

Any failure to deserialize the arguments will cause the job to fail and backoff according to the backoff policy.

Parameters:

  • payload (Object)

Returns:

  • (Object)


108
109
110
111
112
# File 'lib/zizq/job.rb', line 108

def zizq_deserialize(payload) #: (Hash[String, untyped]) -> [Array[untyped], Hash[Symbol, untyped]]
  args   = payload.fetch("args")
  kwargs = payload.fetch("kwargs").transform_keys(&:to_sym)
  [args, kwargs]
end

#zizq_payload_filter(*args, **kwargs) ⇒ Object

Generate a jq expression that exactly matches payloads with the given arguments.

This is used for filtering in Zizq::Query.

Generates an expression of the form:

. == {"args":["a","b","c"],"kwargs":{"example":true,"other":false}}

Parameters:

  • args (Object)
  • kwargs (Object)

Returns:

  • (Object)


122
123
124
125
# File 'lib/zizq/job.rb', line 122

def zizq_payload_filter(*args, **kwargs) #: (*untyped, **untyped) -> String
  payload = zizq_serialize(*args, **kwargs)
  ". == #{JSON.generate(payload)}"
end

#zizq_payload_subset_filter(*args, **kwargs) ⇒ Object

Generate a jq expression that matches jobs whose positional args start with the given values and whose kwargs contain the given key/value pairs.

This is used for filtering in Zizq::Query.

Generates expressions of the form:

(.args[0:2] == ["a","b"]) and (.kwargs | contains({"example":true}))

Parameters:

  • args (Object)
  • kwargs (Object)

Returns:

  • (Object)


136
137
138
139
140
141
142
143
144
145
# File 'lib/zizq/job.rb', line 136

def zizq_payload_subset_filter(*args, **kwargs) #: (*untyped, **untyped) -> String
  payload = zizq_serialize(*args, **kwargs)
  serialized_args = payload.fetch("args")
  serialized_kwargs = payload.fetch("kwargs")

  [
    "(.args[0:#{serialized_args.size}] == #{JSON.generate(serialized_args)})",
    "(.kwargs | contains(#{JSON.generate(serialized_kwargs)}))"
  ].join(" and ")
end

#zizq_serialize(*args, **kwargs) ⇒ Object

Serialize positional and keyword arguments for the #perform method into a payload hash suitable for sending to the server.

The result must be a JSON encodable Hash.

The default implementation generates a hash of the form:

{ "args" => [ 42, "Hello" ], "kwargs" => { "template": "example" } }

If you override this method you almost certainly need to override ::zizq_deserialize, ::zizq_payload_filter and ::zizq_payload_subset_filter too.

Any failure to deserialize the arguments will cause the job to fail and backoff according to the backoff policy.

Parameters:

  • args (Object)
  • kwargs (Object)

Returns:

  • (Object)


86
87
88
# File 'lib/zizq/job.rb', line 86

def zizq_serialize(*args, **kwargs) #: (*untyped, **untyped) -> Hash[String, untyped]
  { "args" => args, "kwargs" => kwargs.transform_keys(&:to_s) }
end