# `Countersign.Workers.Certificate`
[🔗](https://github.com/mkreyman/countersign/blob/v0.1.0/lib/countersign/workers/certificate.ex#L1)

The Oban worker that renders and stores one signed request's certificate
(`Countersign.Certificate`). A host `use`s it and names it as the config's
`:certificate_worker`; `Countersign.Requests.sign/3` then inserts the job in
the sign transaction.

    defmodule MyApp.Signing.CertificateWorker do
      use Countersign.Workers.Certificate,
        queue: :exports,
        max_attempts: 10,
        time_zone: "America/Denver"

      @impl Countersign.Workers.Certificate
      def config, do: MyApp.Signing.config()
    end

The job's args are the request's id and its scope; the request is loaded
in that scope, so a job naming another scope finds nothing and cancels.
One job per request (unique on the request id).

When the job runs:

  * a request not found in the job's scope, or not signed, cancels;
  * a request whose `certificate_ref` is set stores nothing and completes;
  * the certificate is rendered (`Countersign.Certificate.render/3`)
    outside any transaction, then, with the request row locked
    `FOR UPDATE`, checked for a `certificate_ref` again, stored through
    `c:Countersign.Signable.store_document/4` and pinned; a rerun after a
    crash finds it pinned and stores nothing;
  * `{:error, :busy}` or `{:error, :timeout}` from rendering or storing
    snoozes the job 30 seconds, `{:cancel, reason}` from storing cancels
    it, and any other error retries it.

## Options

  * `:queue` - default `:default`.
  * `:max_attempts` - default 10.
  * `:time_zone` - passed to `Countersign.Certificate` (default UTC only).

# `config`

```elixir
@callback config() :: Countersign.Config.t()
```

The config the worker renders and stores with.

# `args`

```elixir
@spec args(Countersign.Request.t()) :: map()
```

A certificate job's args: the request and its scope.

# `perform`

```elixir
@spec perform(Countersign.Config.t(), Oban.Job.t(), keyword()) ::
  :ok | {:snooze, pos_integer()} | {:cancel, term()} | {:error, term()}
```

Runs a certificate job (see the moduledoc).

---

*Consult [api-reference.md](api-reference.md) for complete listing*
