#[job]Expand description
Marks a function as a background job.
Jobs are durable background tasks that survive server restarts and automatically retry on failure.
§Authentication
By default, jobs require an authenticated user to dispatch. Override with:
public- Can be dispatched without authenticationrequire_role("admin")- Requires specific role to dispatch
§Attributes
timeout = "30m"- Job timeout (supports s, m, h suffixes)priority = "normal"- background, low, normal, high, criticalmax_attempts = 3- Maximum retry attemptsbackoff = "exponential"- fixed, linear, or exponentialmax_backoff = "5m"- Maximum backoff durationretry(max_attempts = 3, backoff = "exponential", max_backoff = "5m")- Grouped retry configworker_capability = "media"- Required worker capabilityidempotent- Mark job as idempotentidempotent(key = "input.id")- Idempotent with custom keyname = "custom_name"- Override job name
§Example
ⓘ
#[forge::job(timeout = "30m", priority = "high")] // Requires authenticated user (default)
pub async fn send_welcome_email(ctx: &JobContext, input: SendEmailInput) -> Result<()> {
// ...
}
#[forge::job(public)] // Can be dispatched without auth
pub async fn process_webhook(ctx: &JobContext, input: WebhookInput) -> Result<()> {
// ...
}
#[forge::job(retry(max_attempts = 5, backoff = "exponential"), require_role("admin"))]
pub async fn process_payment(ctx: &JobContext, input: PaymentInput) -> Result<()> {
// Requires admin role to dispatch
}