Jobs and scheduler
Background jobs on Oban, queued in the caller's transaction, and scheduled entries in app.exs.
A worker lives in lib/workers/<name>.worker.ex and uses CardinalJobs.Worker. It runs as the site and user that queued it, on any node.
A worker
defmodule Crm.Workers.InvitationExpiryWorker do
use CardinalJobs.Worker, queue: :default
@impl CardinalJobs.Worker
def perform(ctx, _args) do
with {:ok, names, fx} <- Crm.Controllers.CRMInvitation.expire(ctx),
do: {:after_commit, length(names), fx}
end
endperform/2 runs inside a transaction and returns what a TxnService.run/2 function returns. {:error, reason} or a raise rolls back, and Oban retries with backoff. A raise also writes an Error Log on the site.
| Option | Effect |
|---|---|
queue: :long | For slow jobs |
queue: :apps | For an app's job that waits on another system |
txn: false | perform/2 opens its own transactions, for jobs that commit in steps |
unique: ... | Oban's unique option. A duplicate returns the existing job |
Queue a job
Cardinal.Jobs.enqueue(ctx, Crm.Workers.LeadSyncWorker, %{"source" => name})The job is inserted in the caller's transaction, so it exists only if that transaction commits. Args use string keys. The keys t, u, rid and c are reserved: the platform fills them from the ctx.
Scheduled jobs
List each scheduled worker in app.exs:
scheduler: [
{"*/5 * * * *", Crm.Workers.LeadSyncFiveMinutesWorker},
{:hourly_long, Crm.Workers.LeadSyncHourlyWorker},
{:daily, Crm.Workers.InvitationExpiryWorker}
]A frequency is :all, :hourly, :daily, :weekly, :monthly, :yearly, a _long variant of the middle four, or a five-field cron string. The installer keeps one Scheduled Job Type per entry, and a cron string that does not parse fails the install.
Run and inspect
cardinal jobs list -t mysite.localhost --state retryable
cardinal jobs trigger -t mysite.localhost JOB_TYPE
cardinal jobs retry -t mysite.localhost ID
cardinal scheduler status -t mysite.localhost