ConversationQueue\<TInput, TOutput>
Manages planned conversations.
A conversation may be enqueued, that is, planned to execute in a specific time frame.
When the conversation is to be executed, a "ready" event is fired on this object,
that gets passed a Conversation object to populate its input and run.
The queue is stored on the Dasha platform. To associate an enqueued conversation with its data, each conversation is assigned a key.
Examples
running enqueued conversations
application.queue.on("ready", async (key, conversation) => { conversation.input = getInput(key); const result = await conversation.execute(); });
Sometimes, the conversation can be removed from the queue by the Dasha platform itself.
In that case, a "rejected" event is fired.
conversation rejection
application.queue.on("rejected", (key, error) => { console.log(`conversation with key ${key} was rejected`, error); });
There is an important caveat with the incoming SIP conversations.
They get placed in the queue automatically, by the Dasha platform itself.
To discern them from the manually-enqueued conversations, and to get the additional
SIP info (e.g. the SIP user and domain of the conversation partner),
the "ready" event has an extra argument of type QueuedConversationInfo.
using SIP data in a conversation
application.queue.on("ready", (key, conversation, info) => { assert(info.sip !== undefined); conversation.input.from = `${info.fromUser}@${info.fromDomain}`; await conversation.execute(); });
Extends
EventEmitter<{error: [Error];ready: [string,QueuedConversation<TInput,TOutput>,QueuedConversationInfo];rejected: [string,ConversationRejectedError,string];timeout: [string,string]; }>
Type Parameters
TInput
TInput extends Record<string, unknown>
TOutput
TOutput extends Record<string, unknown>
Methods
length()
length():
Promise<number>
Get current queue length at the server side.
Returns
Promise<number>
push()
push(
key,options?):Promise<{jobId:string; }>
Push a new conversation into the queue.
Parameters
key
string
a conversation key that can be used to associate data
options?
after?
Date
the earliest point in time the conversation can run at
before?
Date
the latest point in time the conversation can run at
input?
TInput
priority?
number
Returns
Promise<{ jobId: string; }>
Object with internal backend jobId
Example
enqueue a conversation to within an hour
application.queue.push("key", { after: new Date(), before: new Date(Date.now() + 60 * 60 * 1000) });
waitUntilCurrentProcessed()
waitUntilCurrentProcessed(
options?):Promise<boolean>
Parameters
options?
cancelationToken?
checkPeriodInMs?
number
Returns
Promise<boolean>