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);
Name | Type | Description |
---|---|---|
props | QueueProps | No description. |
props
Optional
- Type: QueueProps
Methods
Preflight Methods
Name | Description |
---|---|
setConsumer | Create a function to consume messages from this queue. |
Inflight Methods
Name | Description |
---|---|
approxSize | Retrieve the approximate number of messages in the queue. |
pop | Pop a message from the queue. |
purge | Purge all of the messages in the queue. |
push | Push one or more messages to the queue. |
setConsumer
setConsumer(handler: IQueueSetConsumerHandler, props?: QueueSetConsumerProps): Function
Create a function to consume messages from this queue.
handler
Required
- Type: IQueueSetConsumerHandler
props
Optional
- Type: QueueSetConsumerProps
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.
messages
Required
- Type: str
Payload to send to the queue.
Properties
Name | Type | Description |
---|---|---|
node | constructs.Node | The tree node. |
node
Required
node: Node;
- Type: constructs.Node
The tree node.
Structs
QueueProps
Options for Queue
.
Initializer
bring cloud;
let QueueProps = cloud.QueueProps{ ... };
Properties
Name | Type | Description |
---|---|---|
retentionPeriod | duration | How long a queue retains a message. |
timeout | duration | How long a queue's consumers have to process a message. |
retentionPeriod
Optional
retentionPeriod: duration;
- Type: duration
- Default: undefined
How long a queue retains a message.
timeout
Optional
timeout: duration;
- Type: duration
- Default: undefined
How long a queue's consumers have to process a message.
QueueSetConsumerProps
Options for Queue.setConsumer.
Initializer
bring cloud;
let QueueSetConsumerProps = cloud.QueueSetConsumerProps{ ... };
Properties
Name | Type | Description |
---|---|---|
env | MutMap<str> | Environment variables to pass to the function. |
memory | num | The amount of memory to allocate to the function, in MB. |
timeout | duration | The maximum amount of time the function can run. |
batchSize | num | The maximum number of messages to send to subscribers at once. |
env
Optional
env: MutMap<str>;
- Type: MutMap<str>
- Default: No environment variables.
Environment variables to pass to the function.
memory
Optional
memory: num;
- Type: num
- Default: 128
The amount of memory to allocate to the function, in MB.
timeout
Optional
timeout: duration;
- Type: duration
- Default: 1m
The maximum amount of time the function can run.
batchSize
Optional
batchSize: num;
- Type: num
- Default: 1
The maximum number of messages to send to subscribers at once.
Protocols
IQueueSetConsumerHandler
Extends: IResource
Implemented By: IQueueSetConsumerHandler
Inflight client: @winglang/sdk.cloud.IQueueSetConsumerHandlerClient
A resource with an inflight "handle" method that can be passed to Queue.setConsumer
.
Properties
Name | Type | Description |
---|---|---|
node | constructs.Node | The tree node. |
node
Required
node: Node;
- Type: constructs.Node
The tree node.
IQueueSetConsumerHandlerClient
- Implemented By: IQueueSetConsumerHandlerClient
Inflight client for IQueueSetConsumerHandler
.
Methods
Name | Description |
---|---|
handle | Function 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.
message
Required
- Type: str