🐛 Fix attempt to reconnect and make IMAP node stable (#1667)

This commit is contained in:
Omar Ajoue 2021-04-17 17:01:27 +02:00 committed by GitHub
parent 3fe52c84d5
commit 73da7998cf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -354,8 +354,7 @@ export class EmailReadImap implements INodeType {
return newEmails;
};
let connection: ImapSimple;
const establishConnection = (): Promise<ImapSimple> => {
const config: ImapSimpleOptions = {
imap: {
user: credentials.user as string,
@ -366,11 +365,13 @@ export class EmailReadImap implements INodeType {
authTimeout: 20000,
},
onmail: async () => {
if (connection) {
const returnData = await getNewEmails(connection, searchCriteria);
if (returnData.length) {
this.emit([returnData]);
}
}
},
};
@ -382,7 +383,19 @@ export class EmailReadImap implements INodeType {
// Connect to the IMAP server and open the mailbox
// that we get informed whenever a new email arrives
connection = await imapConnect(config);
return imapConnect(config).then(async conn => {
conn.on('error', async err => {
if (err.code.toUpperCase() === 'ECONNRESET') {
connection = await establishConnection();
}
throw err;
});
return conn;
});
};
let connection: ImapSimple = await establishConnection();
await connection.openBox(mailbox);
// When workflow and so node gets set to inactive close the connectoin