Aurinko.Sync.Orchestrator (Aurinko v0.2.1)

Copy Markdown View Source

High-level sync orchestrator for email, calendar, and contacts.

Manages the full lifecycle of Aurinko's delta-sync model:

  1. Start or resume a sync session
  2. Load all updated records (paging automatically)
  3. Load all deleted record IDs
  4. Persist the delta tokens for the next run
  5. Emit telemetry events per batch

Usage

# Full or incremental email sync
{:ok, result} = Aurinko.Sync.Orchestrator.sync_email(token,
  days_within: 30,
  on_updated: fn records -> MyApp.Mailbox.upsert_many(records) end,
  on_deleted: fn ids -> MyApp.Mailbox.delete_many(ids) end,
  get_tokens: fn -> MyApp.Store.get_delta_tokens("email") end,
  save_tokens: fn tokens -> MyApp.Store.save_delta_tokens("email", tokens) end
)

# Calendar sync
{:ok, result} = Aurinko.Sync.Orchestrator.sync_calendar(token, "primary",
  time_min: ~U[2024-01-01 00:00:00Z],
  time_max: ~U[2024-12-31 23:59:59Z],
  on_updated: fn records -> MyApp.Calendar.upsert_events(records) end,
  on_deleted: fn ids -> MyApp.Calendar.delete_events(ids) end,
  get_tokens: fn -> MyApp.Store.get_delta_tokens("calendar:primary") end,
  save_tokens: fn tokens -> MyApp.Store.save_delta_tokens("calendar:primary", tokens) end
)

Summary

Functions

Run a full or incremental calendar sync for a given calendar.

Run a full or incremental contacts sync.

Run a full or incremental email sync.

Types

sync_opts()

@type sync_opts() :: [
  days_within: pos_integer(),
  on_updated: (list() -> any()),
  on_deleted: (list() -> any()),
  get_tokens: (-> map() | nil),
  save_tokens: (map() -> any()),
  batch_size: pos_integer()
]

sync_result()

@type sync_result() :: %{
  updated: non_neg_integer(),
  deleted: non_neg_integer(),
  duration_ms: non_neg_integer(),
  sync_updated_token: String.t() | nil,
  sync_deleted_token: String.t() | nil
}

Functions

sync_calendar(token, calendar_id, opts)

@spec sync_calendar(String.t(), String.t(), sync_opts()) ::
  {:ok, sync_result()} | {:error, Aurinko.Error.t()}

Run a full or incremental calendar sync for a given calendar.

sync_contacts(token, opts)

@spec sync_contacts(String.t(), sync_opts()) ::
  {:ok, sync_result()} | {:error, Aurinko.Error.t()}

Run a full or incremental contacts sync.

sync_email(token, opts)

@spec sync_email(String.t(), sync_opts()) ::
  {:ok, sync_result()} | {:error, Aurinko.Error.t()}

Run a full or incremental email sync.