📬 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.
How It Works
Section titled “How It Works”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:
- User uploads a file via the Platform
- Platform saves the file to S3 storage and creates a database record with status
pending - Platform enqueues a processing job via the configured queue provider
- Queue invokes the Worker at
POST /process-file - Worker downloads the file, extracts content, creates embeddings, and updates the database
Provider Comparison
Section titled “Provider Comparison”| Feature | QStash | bunqueue |
|---|---|---|
| Hosting | Cloud (Upstash) | Self-hosted (local) |
| Cost | Free tier available | Free (open source) |
| Setup | Create account, get token | Run local server |
| Persistence | Upstash managed | SQLite database |
| Use Case | Production | Local development |
| Scaling | Auto-scaling | Single instance |
When to Use Each
Section titled “When to Use Each”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
Setting Up QStash
Section titled “Setting Up QStash”QStash is the default queue provider and recommended for production deployments.
1. Create Upstash Account
Section titled “1. Create Upstash Account”Sign up at upstash.com and create a new QStash project.
2. Get Your Token
Section titled “2. Get Your Token”In the QStash dashboard, copy your authentication token.
3. Configure Environment
Section titled “3. Configure Environment”Add to your .env file:
QUEUE_PROVIDER=qstashQSTASH_TOKEN="your-qstash-token-here"QSTASH_URL="https://your-qstash-url.upstash.io"WORKER_URL="https://your-worker-url.com"4. Verify Configuration
Section titled “4. Verify Configuration”QStash is ready when:
QUEUE_PROVIDER=qstashQSTASH_TOKENis setWORKER_URLpoints to your worker deployment
Setting Up bunqueue
Section titled “Setting Up bunqueue”bunqueue is a lightweight queue built on Bun, ideal for local development.
1. Start the bunqueue Server
Section titled “1. Start the bunqueue Server”# Using the queue package dev scriptcd packages/queue && bun run dev
# Or run directly with bunxbunx bunqueue start --port 6789 --data-path ./queue.dbThe server runs on localhost:6789 by default.
2. Configure Environment
Section titled “2. Configure Environment”Add to your .env file:
QUEUE_PROVIDER=localBUNQUEUE_HOST=localhostBUNQUEUE_PORT=67893. Ignore Database Files
Section titled “3. Ignore Database Files”Add to .gitignore:
queue.dbqueue.db-shmqueue.db-walThe SQLite database stores jobs locally. Each developer has their own queue state.
Switching Providers
Section titled “Switching Providers”To switch between providers, update QUEUE_PROVIDER in your .env:
# For QStash (production)QUEUE_PROVIDER=qstashQSTASH_TOKEN="your-token"
# For bunqueue (local)QUEUE_PROVIDER=localBUNQUEUE_HOST=localhostBUNQUEUE_PORT=6789Restart both platform and worker after changing the provider.
Configuration Reference
Section titled “Configuration Reference”| Variable | Provider | Required | Default | Description |
|---|---|---|---|---|
QUEUE_PROVIDER | Both | No | qstash | Queue provider: qstash or local |
QSTASH_TOKEN | qstash | Conditional | - | Upstash QStash authentication token |
QSTASH_URL | qstash | No | Upstash default | Custom QStash endpoint URL |
WORKER_URL | qstash | Yes | - | Public URL of the worker service |
BUNQUEUE_HOST | local | Conditional | localhost | bunqueue server host |
BUNQUEUE_PORT | local | Conditional | 6789 | bunqueue server port |
Troubleshooting
Section titled “Troubleshooting”Jobs Not Processing
Section titled “Jobs Not Processing”Symptoms: Files remain in “pending” status indefinitely.
Solutions:
-
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 jobscurl http://localhost:6790/health -
Verify environment variables are set correctly:
Terminal window echo $QUEUE_PROVIDERecho $QSTASH_TOKEN # or $BUNQUEUE_HOST and $BUNQUEUE_PORT -
Check worker logs for processing errors:
Terminal window bun --filter @curiositi/worker dev -
For QStash, verify the token is valid in the Upstash dashboard
Connection Refused (bunqueue)
Section titled “Connection Refused (bunqueue)”Symptoms: ECONNREFUSED error when enqueueing jobs.
Solutions:
-
Ensure bunqueue server is running:
Terminal window cd packages/queue && bun run dev -
Verify the port matches:
Terminal window # Check BUNQUEUE_URL matches the server portecho $BUNQUEUE_HOST:$BUNQUEUE_PORT -
Try a different port if 6789 is in use:
Terminal window bunx bunqueue start --port 6790# Update BUNQUEUE_PORT=6790
Invalid Token (QStash)
Section titled “Invalid Token (QStash)”Symptoms: 401 Unauthorized or invalid token errors.
Solutions:
- Regenerate token in Upstash dashboard
- Update
QSTASH_TOKENin your.env - Restart the platform application
Worker Not Receiving Jobs
Section titled “Worker Not Receiving Jobs”Symptoms: Jobs appear in queue but worker never processes them.
Solutions:
- Verify
WORKER_URLis publicly accessible (for QStash) - For local testing with QStash, use a tunnel:
Terminal window npx cloudflared tunnel --url http://localhost:3040# Use the tunnel URL as WORKER_URL - Check worker is running:
Terminal window curl http://localhost:3040/process-file# Should return 400 (bad request) not 404
Debug Mode
Section titled “Debug Mode”Enable verbose logging to debug queue operations:
# In your .envDEBUG=*Check platform logs for:
Enqueued job processFile— job was sent to queueFailed to enqueue job— error occurred
Architecture
Section titled “Architecture”For a deeper understanding of how the queue fits into the system, see:
- Architecture Overview — High-level system design
- Configuration — Environment variable reference