Adding reconnection logic to email client

This commit is contained in:
Collin Duncan 2023-09-26 15:22:05 +02:00
parent fd5966bb8f
commit d9b62e6c6a
No known key found for this signature in database

View file

@ -47,10 +47,23 @@ export class EmailClient {
this.status = status
}
public connect() {
private connect() {
this.imapClient.connect()
}
private openBox() {
this.imapClient.openBox(this.mailboxName, (error, mailbox) => {
if (error) {
this.loggerService.error('Error opening mailbox', {
...error,
})
return
}
this.loggerService.debug('Mailbox opened', { mailbox })
this.mailbox = mailbox
})
}
private attempt(
label: string,
current: number,
@ -106,16 +119,6 @@ export class EmailClient {
throw new Error('Not ready to listen')
}
this.imapClient.openBox(this.mailboxName, (error, mailbox) => {
if (error) {
this.loggerService.error('Error opening mailbox', {
...error,
})
return
}
this.mailbox = mailbox
})
this.imapClient.on('mail', (n: number) => this.handleNewMail(n, callback))
}
@ -212,19 +215,19 @@ export class EmailClient {
private setupDefaultListeners() {
this.imapClient.on('ready', () => {
this.loggerService.debug('email client ready')
if (this.status === EmailClientStatus.NotReady) {
this.setStatus(EmailClientStatus.Ready)
this.openBox()
}
})
this.imapClient.on('close', () => {
this.loggerService.debug('email client close')
if (this.status !== EmailClientStatus.Error)
this.setStatus(EmailClientStatus.NotReady)
})
this.imapClient.on('end', () => {
this.loggerService.debug('email client end')
if (this.status !== EmailClientStatus.Error)
if (this.status !== EmailClientStatus.Error) {
this.loggerService.debug('email client reconnecting')
this.setStatus(EmailClientStatus.NotReady)
this.connect()
}
})
this.imapClient.on('error', (error: Error) => {