Skip to content

📬 Queueing System

The queueing system handles asynchronous file processing in Curiositi. When a user uploads a file, the platform immediately acknowledges the upload and queues a background job to process the file. This keeps the user experience fast and responsive.

flowchart LR
    Platform["Platform"] -->|"3. Enqueue job"| Queue["Queue<br/>(QStash/bunqueue)"]
    Queue -->|"4. Invoke worker"| Worker["Worker<br/>(extract + embed)"]
    Worker -->|"5. Process file"| Complete["Process Complete"]

Flow:

  1. User uploads a file via the Platform
  2. Platform saves the file to S3 storage and creates a database record with status pending
  3. Platform enqueues a processing job via the configured queue provider
  4. Queue invokes the Worker at POST /process-file
  5. Worker downloads the file, extracts content, creates embeddings, and updates the database
FeatureQStashbunqueue
HostingCloud (Upstash)Self-hosted (local)
CostFree tier availableFree (open source)
SetupCreate account, get tokenRun local server
PersistenceUpstash managedSQLite database
Use CaseProductionLocal development
ScalingAuto-scalingSingle instance

QStash (Production)

  • Deploying to production
  • Need reliable, managed infrastructure
  • Want automatic scaling
  • Have Upstash account configured

bunqueue (Development)

  • Local development without cloud services
  • Quick prototyping
  • Testing queue behavior locally
  • No internet connection required

QStash is the default queue provider and recommended for production deployments.

Sign up at upstash.com and create a new QStash project.

In the QStash dashboard, copy your authentication token.

Add to your .env file:

Terminal window
QUEUE_PROVIDER=qstash
QSTASH_TOKEN="your-qstash-token-here"
QSTASH_URL="https://your-qstash-url.upstash.io"
WORKER_URL="https://your-worker-url.com"

QStash is ready when:

  • QUEUE_PROVIDER=qstash
  • QSTASH_TOKEN is set
  • WORKER_URL points to your worker deployment

bunqueue is a lightweight queue built on Bun, ideal for local development.

Terminal window
# Using the queue package dev script
cd packages/queue && bun run dev
# Or run directly with bunx
bunx bunqueue start --port 6789 --data-path ./queue.db

The server runs on localhost:6789 by default.

Add to your .env file:

Terminal window
QUEUE_PROVIDER=local
BUNQUEUE_HOST=localhost
BUNQUEUE_PORT=6789

Add to .gitignore:

queue.db
queue.db-shm
queue.db-wal

The SQLite database stores jobs locally. Each developer has their own queue state.

To switch between providers, update QUEUE_PROVIDER in your .env:

Terminal window
# For QStash (production)
QUEUE_PROVIDER=qstash
QSTASH_TOKEN="your-token"
# For bunqueue (local)
QUEUE_PROVIDER=local
BUNQUEUE_HOST=localhost
BUNQUEUE_PORT=6789

Restart both platform and worker after changing the provider.

VariableProviderRequiredDefaultDescription
QUEUE_PROVIDERBothNoqstashQueue provider: qstash or local
QSTASH_TOKENqstashConditional-Upstash QStash authentication token
QSTASH_URLqstashNoUpstash defaultCustom QStash endpoint URL
WORKER_URLqstashYes-Public URL of the worker service
BUNQUEUE_HOSTlocalConditionallocalhostbunqueue server host
BUNQUEUE_PORTlocalConditional6789bunqueue server port

Symptoms: Files remain in “pending” status indefinitely.

Solutions:

  1. Check queue provider is running:

    Terminal window
    # For bunqueue, use the HTTP /health endpoint which runs on a different port than TCP, which is used for enqueueing jobs
    curl http://localhost:6790/health
  2. Verify environment variables are set correctly:

    Terminal window
    echo $QUEUE_PROVIDER
    echo $QSTASH_TOKEN # or $BUNQUEUE_HOST and $BUNQUEUE_PORT
  3. Check worker logs for processing errors:

    Terminal window
    bun --filter @curiositi/worker dev
  4. For QStash, verify the token is valid in the Upstash dashboard

Symptoms: ECONNREFUSED error when enqueueing jobs.

Solutions:

  1. Ensure bunqueue server is running:

    Terminal window
    cd packages/queue && bun run dev
  2. Verify the port matches:

    Terminal window
    # Check BUNQUEUE_URL matches the server port
    echo $BUNQUEUE_HOST:$BUNQUEUE_PORT
  3. Try a different port if 6789 is in use:

    Terminal window
    bunx bunqueue start --port 6790
    # Update BUNQUEUE_PORT=6790

Symptoms: 401 Unauthorized or invalid token errors.

Solutions:

  1. Regenerate token in Upstash dashboard
  2. Update QSTASH_TOKEN in your .env
  3. Restart the platform application

Symptoms: Jobs appear in queue but worker never processes them.

Solutions:

  1. Verify WORKER_URL is publicly accessible (for QStash)
  2. For local testing with QStash, use a tunnel:
    Terminal window
    npx cloudflared tunnel --url http://localhost:3040
    # Use the tunnel URL as WORKER_URL
  3. Check worker is running:
    Terminal window
    curl http://localhost:3040/process-file
    # Should return 400 (bad request) not 404

Enable verbose logging to debug queue operations:

Terminal window
# In your .env
DEBUG=*

Check platform logs for:

  • Enqueued job processFile — job was sent to queue
  • Failed to enqueue job — error occurred

For a deeper understanding of how the queue fits into the system, see: