Skip to content

Commit

Permalink
Merge pull request #2395 from botpress/sp_dialog-timeout
Browse files Browse the repository at this point in the history
fix(dialog): timeout handling
  • Loading branch information
slvnperron committed Sep 17, 2019
2 parents 70d29d3 + 1b2fa60 commit 077f33c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/bp/core/sdk/impl.ts
Expand Up @@ -61,7 +61,7 @@ export class IOEvent implements sdk.IO.Event {
this.id = args.id || (Date.now() * 100000 + ((Math.random() * 100000) | 0)).toString()
this.preview = args.preview || this.constructPreview()
this.flags = {}
this.state = {}
this.state = { __stacktrace: [] }
args.nlu = args.nlu || {}

if (this.direction === 'incoming') {
Expand Down
2 changes: 1 addition & 1 deletion src/bp/core/services/dialog/dialog-engine.ts
Expand Up @@ -144,7 +144,7 @@ export class DialogEngine {

// Check for a timeout node in the current flow
if (!timeoutNode) {
timeoutNode = findNodeWithoutError(timeoutFlow, 'timeout')
timeoutNode = findNodeWithoutError(currentFlow, 'timeout')
}

// Check for a timeout property in the current flow
Expand Down
18 changes: 13 additions & 5 deletions src/bp/core/services/dialog/janitor.ts
Expand Up @@ -69,6 +69,7 @@ export class DialogJanitor extends Janitor {

private async _processSessionTimeout(sessionId: string, botId: string, botConfig: BotConfig) {
dialogDebug.forBot(botId, 'Processing timeout', sessionId)
let resetSession = true

try {
const channel = SessionIdFactory.createChannelFromId(sessionId)
Expand Down Expand Up @@ -97,11 +98,15 @@ export class DialogJanitor extends Janitor {
fakeEvent.state.context = session.context as IO.DialogContext
fakeEvent.state.session = session.session_data as IO.CurrentSession

await this.dialogEngine.processTimeout(botId, sessionId, fakeEvent)
const after = await this.dialogEngine.processTimeout(botId, sessionId, fakeEvent)
if (_.get(after, 'state.context.queue.instructions.length', 0) > 0) {
// if after processing the timeout handling we still have instructions queued, we're not clearing the context
resetSession = false
}
} catch (error) {
this._handleError(error, botId)
} finally {
await this._resetContext(botId, botConfig, sessionId)
await this._resetContext(botId, botConfig, sessionId, resetSession)
}
}

Expand All @@ -113,13 +118,16 @@ export class DialogJanitor extends Janitor {
}
}

private async _resetContext(botId, botConfig, sessionId) {
private async _resetContext(botId, botConfig, sessionId, resetContext: boolean) {
const botpressConfig = await this.getBotpresConfig()
const expiry = createExpiry(botConfig!, botpressConfig)
const session = await this.sessionRepo.get(sessionId)

session.context = {}
session.temp_data = {}
if (resetContext) {
session.context = {}
session.temp_data = {}
}

session.context_expiry = expiry.context
session.session_expiry = expiry.session

Expand Down

0 comments on commit 077f33c

Please sign in to comment.