Bedrock.JobQueue.Lease (bedrock_job_queue v0.2.0)

View Source

A lease on a job item.

Per QuiCK paper: Leasing works by updating vesting_time to make items "invisible" rather than removing them from the queue. If a worker fails, the lease expires and the item becomes visible again automatically.

Fields

  • id - Unique lease identifier (derived from item_id, holder, and time)
  • item_id - The ID of the leased job item
  • queue_id - The queue this item belongs to
  • holder - Identifier of the consumer holding the lease
  • obtained_at - When the lease was obtained (ms since epoch)
  • expires_at - When the lease expires (ms since epoch)
  • item_key - The item's storage key tuple {priority, vesting_time, id} for O(1) lookup on complete/requeue. Stored because vesting_time changes when leased.

Summary

Functions

Derives a deterministic lease ID from inputs.

Returns true if the lease has expired.

Creates a new lease for an item.

Returns the remaining time on the lease in milliseconds. Returns 0 if expired.

Types

t()

@type t() :: %Bedrock.JobQueue.Lease{
  expires_at: non_neg_integer(),
  holder: binary(),
  id: binary(),
  item_id: binary(),
  item_key: tuple() | nil,
  obtained_at: non_neg_integer(),
  queue_id: String.t()
}

Functions

derive_id(item_id, holder, now)

@spec derive_id(binary(), binary(), non_neg_integer()) :: binary()

Derives a deterministic lease ID from inputs.

This makes lease IDs predictable for testing while still being unique per item/holder/time combination.

expired?(lease, opts \\ [])

@spec expired?(
  t(),
  keyword()
) :: boolean()

Returns true if the lease has expired.

Options

  • :now - Current time in milliseconds (default: System.system_time(:millisecond))

new(item, holder, opts \\ [])

@spec new(Bedrock.JobQueue.Item.t(), binary(), keyword()) :: t()

Creates a new lease for an item.

Options

  • :duration_ms - Lease duration in milliseconds (default: 30_000)
  • :now - Current time in milliseconds (default: System.system_time(:millisecond))

remaining_ms(lease, opts \\ [])

@spec remaining_ms(
  t(),
  keyword()
) :: non_neg_integer()

Returns the remaining time on the lease in milliseconds. Returns 0 if expired.

Options

  • :now - Current time in milliseconds (default: System.system_time(:millisecond))