Skip to main content

job

Attribute Macro job 

Source
#[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 authentication
  • require_role("admin") - Requires specific role to dispatch

§Attributes

  • timeout = "30m" - Job timeout (supports s, m, h suffixes)
  • priority = "normal" - background, low, normal, high, critical
  • max_attempts = 3 - Maximum retry attempts
  • backoff = "exponential" - fixed, linear, or exponential
  • max_backoff = "5m" - Maximum backoff duration
  • retry(max_attempts = 3, backoff = "exponential", max_backoff = "5m") - Grouped retry config
  • worker_capability = "media" - Required worker capability
  • idempotent - Mark job as idempotent
  • idempotent(key = "input.id") - Idempotent with custom key
  • name = "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
}