Kantoor.Maintenance (Kantoor v1.0.0)

Scheduled maintenance: a supervised GenServer that runs idempotent jobs on configurable intervals.

Jobs (see Kantoor.Maintenance.Jobs for what each does):

  • :imap_tombstones, :dav_tombstones - forget old sync tombstones;
  • :pending_fetch - re-fetch bodies of notify-only mail, degrade hopeless ones;
  • :app_passwords, :known_login_ips, :rate_limiter, :staged_attachments, :orphan_bodies, :chat_retention - housekeeping;
  • :quota_release - release held over-quota mail that now fits;
  • :dns_sync - repair the Salt DNS records of every published mail domain (Kantoor.DnsRecords.Publishing.sync_all/0).

Safety

  • Every job is idempotent, so two nodes (or a retry) can never do harm.
  • A job holds a Postgres session advisory lock (pg_try_advisory_lock) while it runs, so only one node runs a given job at a time; the other skips it. Behind a transaction pooler (KANTOOR_DATABASE_POOLER) a session lock cannot be held, so the lock is a transaction-scoped one on a connection of its own (Kantoor.Maintenance.Lock); the key and the behaviour are the same.
  • A job is wrapped: a crash is logged and reported, never propagated, and the other jobs keep their schedule.
  • Each run emits [:kantoor, :maintenance, :job, :stop | :exception] with measurements %{duration: native_time, count: n} and metadata %{job: name} (the exception event adds kind and the exception module). No personal data.

Configuration (config :kantoor, Kantoor.Maintenance)

  • :enabled (true), :schedule (true; false = only run_now/2)
  • :jobs - [name: [enabled: bool, interval_s: n]]
  • :tombstone_days (90), :app_password_grace_days (30), :known_login_ip_ttl_days (30), :chat_deleted_days (30), :pending_fetch_min_gap_s (1800), :pending_fetch_max_gap_s (21600), :pending_fetch_max_age_days (7), :batch (500), :first_run_ms (30_000), :jitter_ms (300_000)

start_link/1 options override the application config; :clock is a zero-arity function returning a DateTime (tests inject it) and :name.

Summary

Functions

Returns a specification to start this module under a supervisor.

All job names.

The {namespace, job_id} advisory-lock key of job (for tests and operators).

Runs job now, in the server process, and returns its result: {:ok, count}, {:skipped, :disabled}, {:skipped, :locked} (another node holds the job's lock) or {:error, reason} (the job crashed; it was logged).

Per-job status for the admin dashboard, read from memory of this node: [%{job:, enabled:, interval_s:, last: nil | %{at:, duration_ms:, count:, status:}}].

Types

job()

@type job() :: atom()

result()

@type result() ::
  {:ok, non_neg_integer()} | {:skipped, :disabled | :locked} | {:error, term()}

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

jobs()

@spec jobs() :: [job()]

All job names.

lock_key(job)

@spec lock_key(job()) :: {integer(), integer()}

The {namespace, job_id} advisory-lock key of job (for tests and operators).

run_now(job, server \\ __MODULE__)

@spec run_now(job(), GenServer.server()) :: result() | {:error, :unknown_job}

Runs job now, in the server process, and returns its result: {:ok, count}, {:skipped, :disabled}, {:skipped, :locked} (another node holds the job's lock) or {:error, reason} (the job crashed; it was logged).

start_link(opts)

status(server \\ __MODULE__)

@spec status(atom()) :: [map()]

Per-job status for the admin dashboard, read from memory of this node: [%{job:, enabled:, interval_s:, last: nil | %{at:, duration_ms:, count:, status:}}].