Kantoor.Feeds.Refresh (Kantoor v1.0.0)

One refresh of one subscription: claim it, fetch the feed, replace the events, record what happened and when to look again.

Safe with any number of replicas

Every replica's scheduler (Kantoor.Feeds.Scheduler) looks for due subscriptions and calls run/2 for each, so several replicas will often start the same one. Only one gets to run it: run/2 first claims the subscription with a single conditional UPDATE (claimed_until is empty or past), and a caller that loses the claim returns {:skipped, :claimed} having done nothing, not even a request to the feed. The claim is a lease: a replica that dies mid-run is not waited for longer than 300 seconds. This is the same guarantee a unique job queue gives, with the facts in Postgres.

What a run records

  • success: status ok, the time, event_count, the feed's ETag and Last-Modified for the next conditional request, the error cleared;
  • 304 Not Modified, or a body identical to the last one: the same, and the events are left as they are;
  • failure (network, size, status, not iCalendar, ...): status error, a short error code (never the address), error_count up by one, and the last good events stay. The next try is later each time (backoff/3).

The address is decrypted here and handed to Kantoor.Feeds.Fetcher; it is never logged or stored in plain text.

Summary

Functions

Seconds to wait after the errors-th failure in a row: the refresh interval doubled for each, at most 21600 seconds (never less than the interval), and no sooner than the server's Retry-After.

Refreshes the subscription id. Options: :force (ignore the schedule and a pause; a manual refresh), :now (a DateTime, for tests) and :jitter (a function from seconds to extra seconds, for tests).

Types

outcome()

@type outcome() ::
  {:ok, :refreshed | :unchanged}
  | {:error, String.t()}
  | {:skipped, :claimed | :not_due | :not_found}

Functions

backoff(interval, errors, retry_after \\ nil)

@spec backoff(pos_integer(), pos_integer(), non_neg_integer() | nil) :: pos_integer()

Seconds to wait after the errors-th failure in a row: the refresh interval doubled for each, at most 21600 seconds (never less than the interval), and no sooner than the server's Retry-After.

run(id, opts \\ [])

@spec run(Ecto.UUID.t(), keyword()) :: outcome()

Refreshes the subscription id. Options: :force (ignore the schedule and a pause; a manual refresh), :now (a DateTime, for tests) and :jitter (a function from seconds to extra seconds, for tests).