Skip to main content
The CGIAR Risk Intelligence Tool uses an asynchronous job pattern for long-running AI operations like prompt previews, document parsing, gap detection, risk analysis, and report generation.

Architecture

The async processing flow works as follows:

Implementation Pattern

Step 1: Initiate Job

Call an endpoint that creates an async job (e.g. POST /api/admin/prompts/preview). The endpoint returns immediately with:
  • 202 Accepted status
  • jobId (UUID) to poll
  • status: "PROCESSING"

Step 2: Poll Job Status

Poll GET /api/jobs/:id every 3 seconds until status is COMPLETED or FAILED.

Step 3: Handle Results

Once the job status is COMPLETED, extract the result field:

Retry Logic

Jobs automatically retry up to 3 times on failure:
  • attempts field tracks retry count
  • maxAttempts is set to 3 by default
  • After 3 failed attempts, status becomes FAILED

Timeout Recommendations

Client-Side Timeout

Implement a maximum polling duration to avoid infinite loops:

Expected Durations

Best Practices

1. Exponential Backoff (Optional)

For production use, consider exponential backoff if the job is taking longer than expected:

2. User Feedback

Show progress indicators while polling:

3. Error Handling

Handle different failure scenarios:

4. Cancellation

Allow users to cancel polling (jobs continue running in background):

React Hook Example

Job Scoping

Jobs are scoped to the user who created them:
  • Job records include createdById field
  • GET /api/jobs/:id only returns jobs created by the authenticated user
  • Attempting to access another user’s job returns 404

Database Schema

Jobs are stored in the jobs table with the following fields:

See Also