Method: Hookshot#enqueue

Defined in:
lib/hookshot.rb

#enqueue(url:, headers:, context:, payload:, activate_at: nil) ⇒ Object

enqueue takes a URL, a hash of headers, a payload (request body), and a context value. It submits these values to hookshot for processing.

The context value can be any string, and will be returned to you via get_next_failure if the job can’t be successfully completed after about 48 hours of retries.

In Shopify, we pass our WebhookSubscription object ID for the context value, so that we can notify merchants when their webhooks are failing, and delete subscriptions that fail consistently.

activate_at is an optional parameter that specifies a number of seconds to wait before making this job active in hookshot. You should normally call enqueue_in rather than pass activate_at explicitly.

Example:

hookshot.enqueue(
  url: 'http://localhost:8080/post',
  headers: {"X-My-Header" => "value"},
  context: "42",
  payload: "request body")


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/hookshot.rb', line 63

def enqueue(url:, headers:, context:, payload:, activate_at: nil)
  uuid = SecureRandom.uuid
  redis.pipelined do
    redis.hmset(
      job_key(uuid),
      "url", url,
      "headers", serialize_headers(headers),
      "context", context,
      "payload", payload,
      "failures", 0)
    redis.expire(job_key(uuid), JOB_KEY_LIFETIME)
    if activate_at
      redis.zadd(DELAYED_SET, activate_at, uuid)
    else
      redis.lpush(NEW_JOBS_LIST, uuid)
    end
  end

  uuid
end