Queue
Aerostack Queue provides background job processing backed by Cloudflare Queues. Jobs are automatically retried on failure with configurable backoff.
Quick start
import { sdk } from '@aerostack/sdk'
// Enqueue a job
await sdk.queue.send('email:send', {
to: 'user@example.com',
subject: 'Welcome to our app!',
templateId: 'welcome',
userId: '123',
})Enqueue jobs
// Simple job
await sdk.queue.send('job-type', payload)
// With delay (seconds)
await sdk.queue.send('reminder:send', { userId: '123' }, {
delaySeconds: 3600, // send in 1 hour
})
// With custom retry policy
await sdk.queue.send('webhook:deliver', { url, body }, {
maxRetries: 5,
retryDelay: 30,
})Process jobs
Define consumers in your Worker’s queue handler:
// In your Worker (aerostack.toml must declare the queue binding)
export default {
async queue(batch, env) {
for (const message of batch.messages) {
const { type, payload } = message.body
if (type === 'email:send') {
await sendEmail(payload)
message.ack()
}
if (type === 'webhook:deliver') {
const ok = await deliverWebhook(payload.url, payload.body)
if (ok) {
message.ack()
} else {
message.retry({ delaySeconds: 60 })
}
}
}
}
}Fan-out pattern
Send the same job to multiple workers:
await Promise.all([
sdk.queue.send('notification:email', { userId, event }),
sdk.queue.send('notification:push', { userId, event }),
sdk.queue.send('analytics:track', { userId, event }),
])Queue jobs are durable — they survive Worker crashes and restarts. If your Worker fails to process a job, it will be retried automatically.
Use Cases
Transactional email sending
Enqueue emails (welcome messages, order confirmations, password resets) instead of sending them inline. This keeps your API response times fast and lets you retry failed sends without blocking the user. Use the delay option to schedule drip campaigns.
await sdk.queue.send('email:send', {
to: user.email,
templateId: 'order-confirmation',
data: { orderId, items, total },
})PDF generation
Generate invoices, reports, or certificates in the background. The user submits a request, gets an immediate acknowledgment, and receives a notification (or polls a status endpoint) when the PDF is ready and uploaded to storage.
Image processing
Resize, crop, or watermark uploaded images asynchronously. When a user uploads a photo, enqueue a processing job that generates multiple sizes (thumbnail, medium, full) and stores them in storage. This avoids blocking the upload response while heavy image work runs.
Webhook delivery with retries
Deliver outbound webhooks to your customers’ endpoints with automatic retry and exponential backoff. If the target server is temporarily down, the queue retries the delivery up to your configured limit, so no events are lost.
Batch data imports
Process CSV or JSON uploads row by row in the background. Enqueue one job per row (or per chunk) so that large imports do not time out your API. Each chunk is retried independently if it fails, and you can track progress by counting acknowledged messages.