Handle failed polling

This commit is contained in:
Eric Bailey
2024-05-03 14:50:45 -05:00
parent 6903b68557
commit 9054ba6979
2 changed files with 83 additions and 55 deletions
@@ -22,6 +22,9 @@ export function MessageListError({
[ConvoItemError.ResumeFailed]: _( [ConvoItemError.ResumeFailed]: _(
msg`There was an issue connecting to the chat.`, msg`There was an issue connecting to the chat.`,
), ),
[ConvoItemError.PollFailed]: _(
msg`This chat was disconnected due to a network error.`,
),
}[item.code] }[item.code]
}, [_, item.code]) }, [_, item.code])
+25
View File
@@ -28,6 +28,7 @@ export enum ConvoStatus {
export enum ConvoItemError { export enum ConvoItemError {
HistoryFailed = 'historyFailed', HistoryFailed = 'historyFailed',
ResumeFailed = 'resumeFailed', ResumeFailed = 'resumeFailed',
PollFailed = 'pollFailed',
} }
export enum ConvoError { export enum ConvoError {
@@ -194,6 +195,7 @@ export class Convo {
private historyCursor: string | undefined | null = undefined private historyCursor: string | undefined | null = undefined
private isFetchingHistory = false private isFetchingHistory = false
private eventsCursor: string | undefined = undefined private eventsCursor: string | undefined = undefined
private pollingFailure = false
private pastMessages: Map< private pastMessages: Map<
string, string,
@@ -527,6 +529,11 @@ export class Convo {
) { ) {
if (this.pendingEventIngestion) return if (this.pendingEventIngestion) return
/*
* Represents a failed state, which is retryable.
*/
if (this.pollingFailure) return
setTimeout(async () => { setTimeout(async () => {
this.pendingEventIngestion = this.ingestLatestEvents() this.pendingEventIngestion = this.ingestLatestEvents()
await this.pendingEventIngestion await this.pendingEventIngestion
@@ -537,6 +544,8 @@ export class Convo {
} }
async ingestLatestEvents() { async ingestLatestEvents() {
try {
// throw new Error('UNCOMMENT TO TEST POLL FAILURE')
const response = await this.agent.api.chat.bsky.convo.getLog( const response = await this.agent.api.chat.bsky.convo.getLog(
{ {
cursor: this.eventsCursor, cursor: this.eventsCursor,
@@ -609,6 +618,22 @@ export class Convo {
if (needsCommit) { if (needsCommit) {
this.commit() this.commit()
} }
} catch (e: any) {
logger.error('Convo: failed to poll events')
this.pollingFailure = true
this.footerItems.set(ConvoItemError.PollFailed, {
type: 'error-recoverable',
key: ConvoItemError.PollFailed,
code: ConvoItemError.PollFailed,
retry: () => {
this.footerItems.delete(ConvoItemError.PollFailed)
this.pollingFailure = false
this.commit()
this.pollEvents()
},
})
this.commit()
}
} }
async sendMessage(message: ChatBskyConvoSendMessage.InputSchema['message']) { async sendMessage(message: ChatBskyConvoSendMessage.InputSchema['message']) {