Queue
Queue client class for interacting with Azure Queue Storage.
Table of Contents
-
Methods
- authorize(methodpathqueryheader)
- clearMessages(queue)
- createQueue(queuemetadata)
- deleteMessage(queuemessageIdpopReceipt)
- deleteQueue(queue)
- getMessages(queueoptions)
- getMetadata(queue)
- listQueues(options)
- peekMessages(queueoptions)
- putMessage(queuetextoptions)
- request(methodpathqueryheaderdata)
- sas(queueoptions)
- setMetadata(queuemetadata)
- updateMessage(queuetextmessageIdpopReceiptoptions)
Constructor
Queue
-
options
Parameters:
-
optionsObject- options on the form:
{ // Value for the x-ms-version header fixing the API version version: SERVICE_VERSION, // Value for the x-ms-client-request-id header identifying the client clientId: 'fast-azure-storage', // Server-side request timeout timeout: 30 * 1000, // Delay between client- and server-side timeout clientTimeoutDelay: 500, // HTTP Agent to use (defaults to a global azure.Agent instance) agent: azure.Agent.globalAgent, // Max number of request retries retries: 5, // Multiplier for computation of retry delay: 2 ^ retry * delayFactor delayFactor: 100, // Randomization factor added as: // delay = delay * random([1 - randomizationFactor; 1 + randomizationFactor]) randomizationFactor: 0.25, // Maximum retry delay in ms (defaults to 30 seconds) maxDelay: 30 * 1000, // Error codes for which we should retry transientErrorCodes: TRANSIENT_ERROR_CODES, // Azure storage accountId (required) accountId: undefined, // Azure shared accessKey, required unless options.sas is given accessKey: undefined, // Function that returns SAS string or promise for SAS string, in which // case we will refresh SAS when a request occurs less than // minSASAuthExpiry from signature expiry. This property may also be a // SAS string. sas: undefined, // Minimum SAS expiry before refreshing SAS credentials, if a function for // refreshing SAS credentials is given as options.sas minSASAuthExpiry: 15 * 60 * 1000 }
Methods
clearMessages
-
queue
Clear all messages from queue, note this may timeout if there is a lot of
messages in the queue, in this case you'll get a error with the code:
OperationTimedOut, and you should retry until the operation is successful.
See Azure Queue Storage REST API documentation for details.
Parameters:
-
queueString- Name of queue to clear all messages from.
Returns:
A promise that messages have been cleared.
createQueue
-
queue -
metadata
Create queue with given name, returns promise that resolves to true, if
the queue didn't already exist. Do not rely on this behavior unless you
disable the retry logic. Note, if queue exists with different
meta-data an error will be thrown.
Parameters:
-
queueString- Name of queue to create.
-
metadataObject- Mapping from metadata keys to values.
Returns:
A promise that queue has been created.
deleteMessage
-
queue -
messageId -
popReceipt
Delete a message from queue using messageId and popReceipt
Parameters:
-
queueString- Name of queue to delete message from
-
messageIdString- Message identifier for message to delete, this
identifier is given when you call
getMessages.
- Message identifier for message to delete, this
identifier is given when you call
-
popReceiptString- Opaque token
popReceiptthat was given bygetMessageswhen you received the message.
- Opaque token
Returns:
A promise that the message has been deleted.
deleteQueue
-
queue
Delete queue, return promise queue is deleted. Note, Azure may take a while to garbage collect the queue, see documentation for relevant details, if you plan to recreate the queue again.
Parameters:
-
queueString- Name of queue to delete.
Returns:
A promise that the queue has been marked for deletion.
getMessages
-
queue -
options
Get messages from queue, returns up to options.numberOfMessages of
messages, note, that Azure Queue Storage only allows up to 32 messages per
request.
See, deleteMessage for how to delete messages once you have processed them.
Note, Azure may return zero messages giving you an empty array. This is not necessarily proof the that the queue is empty. See REST documentation for consistency levels.
Parameters:
-
queueString- Name of queue to get messages from.
-
optionsObjectoptionson the following form:
{ numberOfMessages: 1, // Max number of messages to claim (max 32) visibilityTimeout: 30 // Seconds to messages becomes visible again }
Returns:
A promise for an array of messages on the following form:
[
{
messageId: '...', // Message id as string
insertionTime: new Date(), // Insertion time as Date object
expirationTime: new Date(), // Expiration time as Date object
dequeueCount: 1, // Message dequeue count
messageText: '...', // Message text (however, you encoded it)
popReceipt: '...', // Opaque string for deleting the message
timeNextVisible: new Date() // Next time visible as Date object
},
...
]
getMetadata
-
queue
Get meta-data for given queue. This includes approximate message count,
note that the approximate message is an upper-bound on the number of messages
in the queue.
Warning, this is a HEAD request, so if the queue is missing you get an
error with err.statusCode = 404, but err.code property will be
ErrorWithoutCode. The same goes for all other error codes.
Parameters:
-
queueString- Name of queue to get meta-data from.
Returns:
A promise for an object on the form:
{
messageCount: 50, // Upper-bound on message count
metadata: { // Mapping from meta-data keys to values
'<key>': '<value>', // Meta-data key/value pair
...
}
}
listQueues
-
options
List queues under the storage account.
Parameters:
-
optionsObjectoptionson the following form:
{ prefix: '', // Prefix of queues to list marker: '', // Marker to list queues from maxResults: 5000, // Max number of results metadata: false // Whether or not to include metadata }
Returns:
A promise for an object on the form:
{
queues: [
{
name: '...', // Name of queue
metadata: {} // Meta-data dictionary if requested
}
],
prefix: '...', // prefix given in options (if given)
marker: '...', // marker given in options (if given)
maxResults: 5000, // maxResults given in options (if given)
nextMarker: '...' // Next marker if not at end of list
}
peekMessages
-
queue -
options
Peek messages from queue, returns up to options.numberOfMessages, note,
that Azure Queue Storage only allows up to 32 messages at once.
Note, Azure may return zero messages giving you an empty array. This is not necessarily proof the that the queue is empty. See REST documentation for consistency levels.
Parameters:
-
queueString- Name of queue to peek messages from.
-
optionsObjectoptionson the following form:
{ numberOfMessages: 1 // Max number of messages to peek }
Returns:
A promise for an array of messages on the following form:
[
{
messageId: '...', // Message id as string
insertionTime: new Date(), // Insertion time as Date object
expirationTime: new Date(), // Expiration time as Date object
dequeueCount: 1, // Message dequeue count
messageText: '...' // Message text (however, you encoded it)
},
...
]
putMessage
-
queue -
text -
options
Put a message with XML-safe text into queue with TTL and visibility-
timeout, as given in options.
Notice that the text must be XML-safe, for JSON it's useful to base64
encode the message. This is what many other libraries does, so make sense for
interoperability. Encoding this way is trivial in node.js:
var text = new Buffer(JSON.stringify(jsonMessage)).toString('base64');
Parameters:
-
queueString- Name of queue to put message into.
-
textString- XML-safe string to send.
-
optionsObject- options on the following form:
{ visibilityTimeout: 7 * 24 * 60 * 60, // Visibility timeout in seconds messageTTL: 7 * 24 * 60 * 60 // Message Time-To-Live in seconds }
Returns:
A promise that the messages was inserted in the queue.
request
-
method -
path -
query -
header -
data
Make a signed request to path using method in upper-case and all query
parameters and headers keys in lower-case. The request will carry data
as payload and will be retried using the configured retry policy,
Parameters:
-
methodString- HTTP verb in upper case, e.g.
GET.
- HTTP verb in upper case, e.g.
-
pathString- Path on queue resource for storage account.
-
queryObject- Query-string parameters.
-
headerObject- Mapping from header key in lowercase to value.
-
dataString- String data to send as UTF-8 payload.
Returns:
A promise for HTTPS response with payload property as
string containing the response payload.
sas
-
queue -
options
Generate a SAS string on the form 'key1=val1&key2=val2&...'.
Parameters:
-
queueString- Name of queue that this SAS string applies to.
-
optionsObject- Options for the following form:
{ start: new Date(), // Time from which signature is valid expiry: new Date(), // Expiration of signature (required) permissions: { // Set of permissions delegated (required) read: false, // Read meta-data and peek messages add: false, // Add new messages update: false, // Update messages (after get messages) process: false // Process messages (get and delete messages) }, accessPolicy: '...' // Reference to stored access policy }
Returns:
Shared-Access-Signature on string form.
setMetadata
-
queue -
metadata
Set meta-data for given queue, note that this overwrites all existing
meta-data key/value pairs.
Parameters:
-
queueString- Name of queue to set meta-data on.
-
metadataObject- Mapping from meta-data keys to values.
Returns:
A promise that the meta-data was set.
updateMessage
-
queue -
text -
messageId -
popReceipt -
options
Update a message from queue with XML-safe text and visibility-timeout,
as given in options.
Notice that the text must be XML-safe, for JSON it's useful to base64
encode the message. This is what many other libraries does, so make sense for
interoperability. Encoding this way is trivial in node.js:
var text = new Buffer(JSON.stringify(jsonMessage)).toString('base64');
Parameters:
-
queueString- Name of queue in which you wish to update a message.
-
textString- XML-safe UTF-8 text to set on the message.
-
messageIdString- MessageId as received from
getMessages.
- MessageId as received from
-
popReceiptString- Opaque token as given by
getMessages.
- Opaque token as given by
-
optionsObject- Options on the following form:
{ visibilityTimeout: 7 * 24 * 60 * 60, // Visibility timeout in seconds }
Returns:
A promise that the message has been updated.