Adding reconnection logic to email client
This commit is contained in:
parent
fd5966bb8f
commit
d9b62e6c6a
1 changed files with 22 additions and 19 deletions
|
|
@ -47,10 +47,23 @@ export class EmailClient {
|
||||||
this.status = status
|
this.status = status
|
||||||
}
|
}
|
||||||
|
|
||||||
public connect() {
|
private connect() {
|
||||||
this.imapClient.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(
|
private attempt(
|
||||||
label: string,
|
label: string,
|
||||||
current: number,
|
current: number,
|
||||||
|
|
@ -106,16 +119,6 @@ export class EmailClient {
|
||||||
throw new Error('Not ready to listen')
|
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))
|
this.imapClient.on('mail', (n: number) => this.handleNewMail(n, callback))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -212,19 +215,19 @@ export class EmailClient {
|
||||||
private setupDefaultListeners() {
|
private setupDefaultListeners() {
|
||||||
this.imapClient.on('ready', () => {
|
this.imapClient.on('ready', () => {
|
||||||
this.loggerService.debug('email client ready')
|
this.loggerService.debug('email client ready')
|
||||||
this.setStatus(EmailClientStatus.Ready)
|
if (this.status === EmailClientStatus.NotReady) {
|
||||||
|
this.setStatus(EmailClientStatus.Ready)
|
||||||
|
this.openBox()
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
this.imapClient.on('close', () => {
|
this.imapClient.on('close', () => {
|
||||||
this.loggerService.debug('email client close')
|
this.loggerService.debug('email client close')
|
||||||
if (this.status !== EmailClientStatus.Error)
|
if (this.status !== EmailClientStatus.Error) {
|
||||||
this.setStatus(EmailClientStatus.NotReady)
|
this.loggerService.debug('email client reconnecting')
|
||||||
})
|
|
||||||
|
|
||||||
this.imapClient.on('end', () => {
|
|
||||||
this.loggerService.debug('email client end')
|
|
||||||
if (this.status !== EmailClientStatus.Error)
|
|
||||||
this.setStatus(EmailClientStatus.NotReady)
|
this.setStatus(EmailClientStatus.NotReady)
|
||||||
|
this.connect()
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
this.imapClient.on('error', (error: Error) => {
|
this.imapClient.on('error', (error: Error) => {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue