OPC Stack supports Cloudflare Queues and Cron Triggers.
Queue
Define queues
Define queue names in .env.dev or .env.prod:
QUEUES=task-check,email-send
pre-build.mjs auto creates queues and generates bindings:
task-check->Q_TASK_CHECKemail-send->Q_EMAIL_SEND
Send messages
// src/api/handler/tasks.ts
await ctx.env.Q_TASK_CHECK.send({
taskId: 'task-123',
userId: 'user-456',
timestamp: Date.now()
})
// Batch send
await ctx.env.Q_TASK_CHECK.sendBatch([
{ body: { taskId: 'task-1' } },
{ body: { taskId: 'task-2' } },
{ body: { taskId: 'task-3' } }
])
Consume messages
Handle queue messages in src/consumers/index.ts and dispatch to different handlers by queue name.
Error handling
Use message.ack() for success and message.retry() for retry on failure.
Cron
Define cron expressions
Define cron expressions in .env.dev or .env.prod:
CRONS=0 0 * * *,0 */6 * * *
pre-build.mjs auto generates wrangler.jsonc config.
Handle cron jobs
Handle scheduled jobs in src/jobs/index.ts and dispatch by cron expression.
Cron format
* * * * *
│ │ │ │ │
│ │ │ │ └─ day of week (0-7 and both 0 and 7 mean Sunday)
│ │ │ └─── month (1-12)
│ │ └───── day of month (1-31)
│ └─────── hour (0-23)
└───────── minute (0-59)
Common examples:
0 0 * * *every day at 00:000 */6 * * *every 6 hours*/15 * * * *every 15 minutes0 9 * * 1every Monday at 09:00
Local testing
Test queue
# Send test message
curl -X POST http://localhost:8787/api/test-queue
Test cron
# Trigger cron manually
wrangler dev --test-scheduled
Monitoring
Queue status
# Show queue list
wrangler queues list
Cron logs
# Show logs
wrangler tail
Best practices
Queue
- Idempotency: make message handling idempotent to avoid duplicates
- Timeout control: keep single message handling within 30 seconds
- Batch processing: use
sendBatchfor efficiency - Retry errors: use
message.retry()instead ofmessage.ack()for failures
Cron
- Avoid long running task: keep one run within 30 seconds
- Use queue: send long tasks to queue for async processing
- Handle errors: catch errors to avoid impacting next run
- Idempotency: repeated execution should not create side effects
FAQ
Q: What if queue messages are lost
Check whether message.ack() is called correctly and inspect queue metrics in Cloudflare dashboard.
Q: Why cron does not run
Check cron expression and verify wrangler.jsonc was generated correctly.
Q: How to implement delayed queue
Cloudflare Queues does not support delay directly. Put timestamp in message and check due time during consumption.