Skip to content

Commit

Permalink
Re-fix slackapi#395 by adjusting the processBeforeResponse option
Browse files Browse the repository at this point in the history
  • Loading branch information
seratch committed Apr 1, 2020
1 parent 0d662d0 commit c2bb022
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
14 changes: 12 additions & 2 deletions src/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ export default class App {
/** Receiver - ingests events from the Slack platform */
private receiver: Receiver;

/** Always call ack() after event listener completion if true */
private processBeforeResponse: boolean = false;

/** Logger */
private logger: Logger;

Expand Down Expand Up @@ -256,6 +259,7 @@ export default class App {
});
}
}
this.processBeforeResponse = processBeforeResponse;

// Conditionally use a global middleware that ignores events (including messages) that are sent from this app
if (ignoreSelf) {
Expand Down Expand Up @@ -580,8 +584,11 @@ export default class App {
if (type !== IncomingEventType.Event) {
listenerArgs.ack = ack;
} else {
// Events API requests are acknowledged right away, since there's no data expected
await ack();
// If processBeforeResponse is true, ack() call should be after the listener completion
if (!this.processBeforeResponse) {
// Events API requests are acknowledged right away, since there's no data expected
await ack();
}
}

// Get the client arg
Expand Down Expand Up @@ -635,6 +642,9 @@ export default class App {
} else if (rejectedListenerResults.length > 1) {
throw new MultipleListenerError(rejectedListenerResults.map(rlr => rlr.reason));
}
if (this.processBeforeResponse && type === IncomingEventType.Event) {
await ack();
}
},
);
} catch (error) {
Expand Down
22 changes: 17 additions & 5 deletions src/ExpressReceiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,24 @@ export default class ExpressReceiver implements Receiver {

try {
await this.bolt?.processEvent(event);
if (storedResponse !== undefined) {
if (typeof storedResponse === 'string') {
res.send(storedResponse);
} else {
res.json(storedResponse);

if (this.processBeforeResponse) {
let spentMillis = 0;
const millisForTimeout = 10;
// Wait here until the isAcknowledged is marked as true or 3 seconds have passed
while (!isAcknowledged && spentMillis < 3000) {
await new Promise(resolve => setTimeout(resolve, millisForTimeout));
spentMillis += millisForTimeout;
}
if (isAcknowledged) {
this.logger.debug(`The listener execution completed in ${spentMillis} millis`);
if (typeof storedResponse === 'string') {
res.send(storedResponse);
} else {
res.json(storedResponse);
}
this.logger.debug('Acknowledged after the listener completion');
} // Otherwise, this Bolt app never responds to this request and above setTimeout outputs an error message
}
} catch (err) {
res.send(500);
Expand Down

0 comments on commit c2bb022

Please sign in to comment.