Queue API reference
The Queue API is the set of methods your code calls on a linked Queue, in the TypeScript, Python, and Rust SDKs. A binding is the connection details Alien injects into each workload linked to the resource, which the SDK client reads.
A handle is bound to one queue, where name is the resource ID from alien.ts:
- TypeScript:
queue(name)from@alienplatform/sdk - Python:
queue(name)fromalienplatform - Rust:
Bindings::from_env()?.queue(name).await?fromalien-sdk
Creating the TypeScript or Python handle does no I/O. The binding resolves on the first call and is cached. If the workload isn't linked to the resource, the first call throws BINDING_NOT_CONFIGURED.
Methods at a glance
| TypeScript | Python | Rust | What it does |
|---|---|---|---|
send(message) | send(message) | send(MessagePayload::Json(..)) | Send one JSON message. |
sendText(text) | send_text(text) | send(MessagePayload::Text(..)) | Send one text message. |
sendBatch(messages) | send_batch(messages) | send_batch(messages) | Send JSON messages with native batching. |
sendBatchText(messages) | send_batch_text(messages) | send_batch(messages) | Send text messages with native batching. |
receive(max) | receive(max_messages=1) | receive(max) | Receive up to 10 messages. |
ack(receipt) | ack(receipt_handle) | ack(receipt) | Acknowledge one message. |
ackBatch(receipts) | ack_batch(receipts) | ack_batch(receipts) | Acknowledge several messages, one outcome each. |
nack(receipt) | nack(receipt_handle) | nack(receipt) | Release a message for immediate redelivery. |
renewLease(receipt, seconds) | renew_lease(receipt_handle, duration_seconds) | renew_lease(receipt, seconds) | Reset a message's remaining lease. |
purge() | purge() | purge() | Delete every message. AWS and Local only. |
send
Sends one message. send serializes the value with JSON.stringify (Python json.dumps); sendText sends the string as is.
await tasks.send({ type: "resize", imageId: "img_123" })
await tasks.sendText("ping")The encoded message must be at most 64 KiB. A larger message fails before any request is sent.
sendBatch
Sends many messages with the backend's own batch API and returns one outcome per message, in input order.
const results = await tasks.sendBatch(images.map(id => ({ type: "resize", imageId: id })))
results.forEach((result, i) => {
if (result.status === "rejected") console.error(images[i], result.code, result.message)
if (result.status === "unknown") {
// The message may or may not have been accepted. Retrying can duplicate it.
}
})Returns QueueSendResult[].
- A message that's empty or over 64 KiB is
rejectedwith codeQUEUE_MESSAGE_SIZE_INVALID, without failing the others. - Alien doesn't retry.
- If the whole call throws, no message was attempted.
Requests are split to fit each backend: SQS 10 messages and 256 KiB per request, Pub/Sub 1,000 messages and 256 KiB.
receive
Receives up to max messages. Each one is leased to you for 30 seconds by default. Until the lease ends, no other consumer receives it.
for (const message of await tasks.receive(10)) {
const body = message.payloadType === "json" ? JSON.parse(message.payload) : message.payload
await handle(body)
await tasks.ack(message.receiptHandle)
}| Parameter | Type | Description |
|---|---|---|
max | number | 1 to 10. Anything else fails before any request is sent. Python defaults to 1. |
Returns QueueMessage[], possibly empty.
receive can wait before returning: SQS long-polls for up to 20 seconds, and Service Bus waits up to 30 seconds for each message.
ack
Acknowledges a message, deleting it from the queue. Acknowledging the same receipt twice is safe.
await tasks.ack(message.receiptHandle)A receipt handle is short-lived and tied to one delivery. After the lease expires and the message is delivered again, the old handle may no longer work.
ackBatch
Acknowledges several messages independently and returns one outcome per receipt, in input order. It isn't atomic.
const results = await tasks.ackBatch(messages.map(m => m.receiptHandle))
for (const result of results) {
if (result.status === "failed") console.warn(result.code, result.message)
}Returns QueueAckResult[]. A failed outcome means completion wasn't confirmed. It may still have succeeded before a network failure.
nack
Releases a message's lease so it's available for redelivery right away, instead of after the lease runs out.
await tasks.nack(message.receiptHandle)renewLease
Sets the remaining lease of a received message to durationSeconds, from 1 to 600.
await tasks.renewLease(message.receiptHandle, 120)| Backend | Effect |
|---|---|
| SQS, Pub/Sub, Local | The remaining lease becomes exactly durationSeconds. A value below the current remainder shortens it. |
| Service Bus | Renews the queue's configured lock duration and ignores durationSeconds. |
Renewing can't revive a lease that already expired or a message that was delivered again.
purge
Deletes every message, visible or leased.
await tasks.purge()Supported on SQS and Local. On Pub/Sub and Service Bus it fails with OPERATION_NOT_SUPPORTED.
SQS allows one purge per queue every 60 seconds, and deletion can take up to 60 seconds to finish.
Types
QueueMessage
interface QueueMessage {
payloadType: "json" | "text"
payload: string // JSON text when payloadType is "json", raw text otherwise
receiptHandle: string // Opaque, for ack, nack, and renewLease
attempt: number // Delivery count, 1 = first delivery
}attempt is real on SQS and Local. Pub/Sub and Service Bus always report 1 through receive.
Python QueueMessage(payload_type, payload, receipt_handle, attempt). Rust QueueMessage { payload: MessagePayload, receipt_handle: String, attempt: u32 }, where MessagePayload is Json(serde_json::Value) or Text(String).
QueueSendResult
type QueueSendResult =
| { status: "sent" }
| { status: "rejected"; code: string; message: string } // not accepted
| { status: "unknown"; code: string; message: string } // may have been acceptedQueueAckResult
type QueueAckResult =
| { status: "acknowledged" }
| { status: "failed"; code: string; message: string }Python returns both as dictionaries with the same keys.
Worker handlers
A Worker with a queue trigger receives messages through handlers from @alienplatform/sdk instead of calling receive:
| Function | Signature | Notes |
|---|---|---|
onQueueMessage<T> | (queue, (message: QueueMessageEvent<T>) => Promise<void>) | One message per call. Returning acknowledges it; throwing leaves it for redelivery. |
onQueueBatch<T> | (queue, (batch: QueueBatchEvent<T>) => Promise<QueueDeliveryOutcome[]>) | Needs a trigger with batch. Return { deliveryId, disposition: "success" | "retry" } per delivery. Retrying one delivery doesn't affect the others. |
QueueMessageEvent has id, source, receiptHandle, payload (parsed), attemptCount, timestamp, and optional attributes. Python has on_queue_message and on_queue_batch. See Worker.
Errors
| Code | When |
|---|---|
BINDING_SETUP_FAILED | On SQS, Pub/Sub, and Service Bus: a message over 64 KiB, max outside 1 to 10, or a failed backend call. The message says which. |
INVALID_INPUT | A lease duration outside 1 to 600, or invalid input on the local queue. |
QUEUE_OPERATION_FAILED | A failed operation on the local queue. |
QUEUE_PROVIDER_RESPONSE_INVALID | The backend returned something Alien couldn't read. |
OPERATION_NOT_SUPPORTED | purge on Pub/Sub or Service Bus. |
BINDING_NOT_CONFIGURED | The workload isn't linked to this resource. |
Cloud queue failures are reported as BINDING_SETUP_FAILED even after the binding is set up, such as a throttled receive. Decide whether to retry from the message, not only from the error's retryable flag.