Karta.Webhooks.Sender (Karta v1.0.0)

The job (Oban worker, queue webhooks) that sends one delivery: one event to one webhook.

A run:

  1. Loads the delivery with its event and webhook. A delivery that is gone (its webhook was removed, or it was pruned) or no longer :pending is done. A delivery whose webhook is switched off fails ("the webhook is switched off"): it is a dead letter, and can be sent again once the webhook is on (Karta.Webhooks.replay/2).
  2. Waits ({:snooze, seconds}) until the delivery is due (next_attempt_at). Then it takes the delivery itself with one conditional update (Karta.Claim): sending_until moves forward (:claim seconds, by default the request's time plus 30), and only the run whose update took the row sends it. A second job for the same delivery takes nothing and waits. The same field counts the concurrency requests (default 4) to the same webhook that are under way on any node; the count and the claim are taken under a lock on the webhook's row. A request that never finished frees its slot when the time runs out.
  3. Sends POST <url> through Karta.NetGuard.HTTP (no private or special addresses unless the operator allows them; the checked address is connected to; no redirects), within timeout (default 15 seconds), with the body and headers of the webhook's format (Karta.Webhooks.Format): in Karta's own, the body of Karta.Webhooks.Payload.body/1, Content-Type: application/json, and the Standard Webhooks headers (Karta.Webhooks.Signature): the event's id, the time of the request, and one signature per valid secret; and the URL's user name and password, if it had them, as Basic authentication.
  4. Records the attempt (Karta.Webhooks.Attempt) and the delivery's new state in one transaction, and announces it on the server's topic (:webhook_deliveries, Karta.Changes). A 2xx answer is a success; anything else (another status, a redirect, a refused address, no connection, no answer in time) is a failure.

Retries. A failed delivery is tried again after 1, 5, 15 and 30 minutes, 1, 2, 4, 8, 12 and 16 hours, and a day: 12 attempts over about three days, each wait 10% shorter or longer at random so that many deliveries that failed together do not come back together. After the last one it is :failed: a dead letter. Deliveries are independent: they are sent in no guaranteed order, and an event may arrive more than once (receivers drop duplicates by webhook-id, and order by the payload's timestamp).

Switched off when it keeps failing. When a webhook's requests have failed without a single success for disable_after (default 5 days), Karta switches it off (disabled_reason: "failing"), logs a warning and announces it (:webhooks); the delivery that failed last and the webhook's other pending deliveries become dead letters. Its owners see it in the API and switch it on again when the receiver works.

A job that crashes is run again by Oban (at most 5 times). The delivery it was sending is still :pending, and its claim runs out after the request's time plus a margin, so the attempt may be made twice — never lost — and the wait before the retry is bounded by the claim.

Configuration (config :karta, Karta.Webhooks.Sender): timeout, concurrency, disable_after (seconds). run/2 takes them as options, with :claim (seconds), :now and :http (options for Karta.NetGuard.HTTP) for tests.

Summary

Functions

A job for a delivery.

The number of attempts a delivery gets.

One run for the delivery id (see the module doc).

Functions

job(delivery_id)

@spec job(Ecto.UUID.t()) :: Oban.Job.changeset()

A job for a delivery.

max_attempts()

@spec max_attempts() :: pos_integer()

The number of attempts a delivery gets.

run(id, opts \\ [])

@spec run(Ecto.UUID.t(), keyword()) :: :ok | {:snooze, pos_integer()}

One run for the delivery id (see the module doc).