AuditTrail.Task (audit_trail v0.1.2)

Copy Markdown

Drop-in wrappers for Task that propagate the actor context to child processes.

Use instead of Task.async/Task.start when spawning work that should carry the current user's audit context.

AuditTrail.Task.async(fn -> Repo.update(changeset) end)
AuditTrail.Task.start(fn -> send_notification(user) end)

Summary

Functions

Same as Task.async/1, but the spawned process starts with the caller's current actor (AuditTrail.set_actor/1) and tenant (AuditTrail.set_tenant/1) already set — so any emit/monitor/schema-tracked Repo call inside fun is attributed to whoever kicked off the parent request, not "anonymous". Returns a Task struct — await it with await/2 (or plain Task.await/2).

Same as Task.await/2 (5s default timeout) — provided so callers don't need to alias both Task and AuditTrail.Task just to await a task started with async/1

Same as Task.start/1 (fire-and-forget, not linked, no way to await a result), with actor/tenant context propagated the same way as async/1.

Same as Task.start_link/1 (linked to the caller — a crash in fun crashes the caller too), with actor/tenant context propagated the same way as async/1. Prefer async/1 unless you specifically want that link.

Functions

async(fun)

Same as Task.async/1, but the spawned process starts with the caller's current actor (AuditTrail.set_actor/1) and tenant (AuditTrail.set_tenant/1) already set — so any emit/monitor/schema-tracked Repo call inside fun is attributed to whoever kicked off the parent request, not "anonymous". Returns a Task struct — await it with await/2 (or plain Task.await/2).

task = AuditTrail.Task.async(fn -> Repo.update(changeset) end)
AuditTrail.Task.await(task)

await(task, timeout \\ 5000)

Same as Task.await/2 (5s default timeout) — provided so callers don't need to alias both Task and AuditTrail.Task just to await a task started with async/1:

task = AuditTrail.Task.async(fn -> Repo.update(changeset) end)
AuditTrail.Task.await(task)

start(fun)

Same as Task.start/1 (fire-and-forget, not linked, no way to await a result), with actor/tenant context propagated the same way as async/1.

AuditTrail.Task.start(fn -> Notifications.send_receipt(order) end)

start_link(fun)

Same as Task.start_link/1 (linked to the caller — a crash in fun crashes the caller too), with actor/tenant context propagated the same way as async/1. Prefer async/1 unless you specifically want that link.