Skip to main content

Queue

The cloud.Queue resource represents a data structure for holding a list of messages. Queues are typically used to decouple producers of data and the consumers of said data in distributed systems. Queues by default are not FIFO (first in, first out) - so the order of messages is not guaranteed.

Usage

Setting a Queue Consumer

bring cloud;

let q = new cloud.Queue();

q.setConsumer(inflight (m: str) => {
log("message ${m} consumed");
});

new cloud.Function(inflight () => {
q.push("message a");
q.push("message b");
});

Using Queue inflight api

Pusing messages, popping them, and purge

bring cloud;

let q = new cloud.Queue();

new cloud.Function(inflight () => {
q.push("message a");
q.push("message b", "message c", "message d");
log("approxSize is ${q.approxSize()}");
log("popping message ${q.pop()}");
log("popping message ${q.pop()}");
log("approxSize is ${q.approxSize()}");
q.purge();
log("approxSize is ${q.approxSize()}");
});

Target-specific details

Simulator (sim)

The sim implementation of cloud.Queue uses JavaScript's Array

AWS (tf-aws and awscdk)

The AWS implementation of cloud.Queue uses Amazon Simple Queue Service.

Azure (tf-azure)

🚧 Not supported yet (tracking issue: #617)

GCP (tf-gcp)

🚧 Not supported yet (tracking issue: #616)

API Reference

Queue

A queue.

Initializers

bring cloud;

new cloud.Queue(props?: QueueProps);
NameTypeDescription
propsQueuePropsNo description.

propsOptional

Methods

Preflight Methods
NameDescription
setConsumerCreate a function to consume messages from this queue.
Inflight Methods
NameDescription
approxSizeRetrieve the approximate number of messages in the queue.
popPop a message from the queue.
purgePurge all of the messages in the queue.
pushPush one or more messages to the queue.

setConsumer
setConsumer(handler: IQueueSetConsumerHandler, props?: QueueSetConsumerProps): Function

Create a function to consume messages from this queue.

handlerRequired

propsOptional

approxSize
inflight approxSize(): num

Retrieve the approximate number of messages in the queue.

pop
inflight pop(): str

Pop a message from the queue.

purge
inflight purge(): void

Purge all of the messages in the queue.

push
inflight push(...messages: Array<str>): void

Push one or more messages to the queue.

messagesRequired
  • Type: str

Payload to send to the queue.


Properties

NameTypeDescription
nodeconstructs.NodeThe tree node.

nodeRequired
node: Node;
  • Type: constructs.Node

The tree node.


Structs

QueueProps

Options for Queue.

Initializer

bring cloud;

let QueueProps = cloud.QueueProps{ ... };

Properties

NameTypeDescription
retentionPerioddurationHow long a queue retains a message.
timeoutdurationHow long a queue's consumers have to process a message.

retentionPeriodOptional
retentionPeriod: duration;

How long a queue retains a message.


timeoutOptional
timeout: duration;

How long a queue's consumers have to process a message.


QueueSetConsumerProps

Options for Queue.setConsumer.

Initializer

bring cloud;

let QueueSetConsumerProps = cloud.QueueSetConsumerProps{ ... };

Properties

NameTypeDescription
envMutMap<str>Environment variables to pass to the function.
memorynumThe amount of memory to allocate to the function, in MB.
timeoutdurationThe maximum amount of time the function can run.
batchSizenumThe maximum number of messages to send to subscribers at once.

envOptional
env: MutMap<str>;
  • Type: MutMap<str>
  • Default: No environment variables.

Environment variables to pass to the function.


memoryOptional
memory: num;
  • Type: num
  • Default: 128

The amount of memory to allocate to the function, in MB.


timeoutOptional
timeout: duration;

The maximum amount of time the function can run.


batchSizeOptional
batchSize: num;
  • Type: num
  • Default: 1

The maximum number of messages to send to subscribers at once.


Protocols

IQueueSetConsumerHandler

Inflight client: @winglang/sdk.cloud.IQueueSetConsumerHandlerClient

A resource with an inflight "handle" method that can be passed to Queue.setConsumer.

Properties

NameTypeDescription
nodeconstructs.NodeThe tree node.

nodeRequired
node: Node;
  • Type: constructs.Node

The tree node.


IQueueSetConsumerHandlerClient

Inflight client for IQueueSetConsumerHandler.

Methods

NameDescription
handleFunction that will be called when a message is received from the queue.

handle
inflight handle(message: str): void

Function that will be called when a message is received from the queue.

messageRequired
  • Type: str