Skip to content

Commit

Permalink
fix(core): session state not persisted during before_session_timeout …
Browse files Browse the repository at this point in the history
…hook (#5702)

* fix(core): state not persisted during

* save state in the janitor

* PR comments

* remove

Co-authored-by: Samuel Massé <samuelmasse4@gmail.com>
Co-authored-by: Laurent Leclerc-Poulin <laurentleclercpoulin@gmail.com>
  • Loading branch information
3 people committed Feb 2, 2022
1 parent b6febcd commit 28221d2
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions packages/bp/src/core/dialog/janitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ import { TimeoutNodeNotFound } from './errors'
const debug = DEBUG('janitor')
const dialogDebug = debug.sub('dialog')

interface updateArgs {
resetSession: boolean
event?: IO.IncomingEvent
}

@injectable()
export class DialogJanitor extends Janitor {
constructor(
Expand Down Expand Up @@ -66,7 +71,7 @@ export class DialogJanitor extends Janitor {

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

try {
const { channel, target, threadId } = SessionIdFactory.extractDestinationFromId(sessionId)
Expand All @@ -90,15 +95,16 @@ export class DialogJanitor extends Janitor {
fakeEvent.state.user = user.attributes
fakeEvent.state.temp = session.temp_data

const after = await this.dialogEngine.processTimeout(botId, sessionId, fakeEvent)
if (_.get(after, 'state.context.queue.instructions.length', 0) > 0) {
update.event = await this.dialogEngine.processTimeout(botId, sessionId, fakeEvent)

if (_.get(update.event, '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
update.resetSession = false
}
} catch (error) {
this._handleError(error, botId)
} finally {
await this._resetContext(botId, botConfig, sessionId, resetSession)
await this._updateState(botId, botConfig, sessionId, update)
}
}

Expand All @@ -110,18 +116,19 @@ export class DialogJanitor extends Janitor {
}
}

private async _resetContext(botId: string, botConfig: BotConfig, sessionId: string, resetContext: boolean) {
private async _updateState(botId: string, botConfig: BotConfig, sessionId: string, update: updateArgs) {
const botpressConfig = await this.getBotpressConfig()
const expiry = createExpiry(botConfig!, botpressConfig)
const session = await this.sessionRepo.get(sessionId)

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

session.context_expiry = expiry.context
session.session_expiry = expiry.session
session.session_data = update.event?.state.session || session.session_data

await this.sessionRepo.update(session)

Expand Down

0 comments on commit 28221d2

Please sign in to comment.