执行Firebase功能时出现ECONNRESET错误

时间:2018-11-14 07:56:44

标签: node.js firebase google-cloud-functions axios

我做了一些功能,使用Firebase功能每5分钟向20多个经理的电子邮件地址发送通知电子邮件。

我在cron作业中使用了计划触发(调用checkNotices函数)。没问题。

当cron作业引擎调用checkNotices函数时,有时会出现如下错误。

{ Error: read ECONNRESET
    at exports._errnoException (util.js:1020:11)
    at TLSWrap.onread (net.js:580:26)
  code: 'ECONNRESET',
  errno: 'ECONNRESET',
  syscall: 'read',
  config: 
   { adapter: [Function: httpAdapter],
     transformRequest: { '0': [Function: transformRequest] },
     transformResponse: { '0': [Function: transformResponse] },
     timeout: 0,
     xsrfCookieName: 'XSRF-TOKEN',
     xsrfHeaderName: 'X-XSRF-TOKEN',
     maxContentLength: -1,
     validateStatus: [Function: validateStatus],
     headers: 
      { Accept: 'application/json, text/plain, */*',
        'Content-Type': 'application/x-www-form-urlencoded',
        'User-Agent': 'axios/0.16.2',
        'Content-Length': 41877 },
     method: 'post',
     url: 'https://us-central1-firebase-sending-mail-t3Bd3.cloudfunctions.net/sendingMail/',
     data: 
     ...
     (something something..)
     }    

但是有时候效果很好, 这让我更加困惑。

这是与代码片段相关的功能的一部分。

const axios = require('axios');
const functions = require('firebase-functions');
const admin = require('firebase-admin');

 exports.checkNotices = functions.https.onRequest((request, response) => {
    const database = admin.database();
    const date = new Date();
    // const hours = date.getHours();
    const timestamp = date.getTime();
    const oneDay = 1 * 1000 * 60 * 60 * 23;
    const timeZone = commonHelpers.getTimeZoneForNotifications();   // it returns like +2, -7, etc.
    database.ref('clubs').orderByChild('timeZone').equalTo(timeZone).once('value')
        .then((clubsData) => {
            let clubs = clubsData ? clubsData.val() : null;
            if (clubs) {
                clubs = commonHelpers.idConvertor(clubs);
                clubs = clubs.filter((club) => {
                    return (!club.doNotSendDailySummary && (!club.lastCheckDate || (timestamp > (club.lastCheckDate + oneDay))));
                });
                const processClubPromises = clubs.map((club) => {
                    return processClubPromise(club, database);
                });
                return Promise.all(processClubPromises);
            } else {
                console.log(`empty clubs for timezone ${timeZone}`);
                Promise.resolve();
            }
        })
        .then(() => {
            response.send('ok');
        })
        .catch((err) => {
            console.error('error:', err);
            response.send(err);
        });
});



function processClubPromise(club, database) {
    let liveMessages = [];
    let mgr = null;
    return database.ref(`mgrs/${club.mgrId}`).once('value')
        .then((mgrData) => {
            mgr = mgrData ? mgrData.val() : null;
            if (mgr) {
                return database.ref('messages').orderByChild('statusFilter').equalTo(`${club._id}_active`).once('value');
            } else {
                Promise.resolve();
            }
        })
        .then((liveMessagesData) => {
            liveMessages = liveMessagesData ? liveMessagesData.val() : null;
            if (liveMessages) {
                const getPausedMessagesPromises = liveMessages.map((activemessage) => {
                    const clubNotice = commonHelpers.clubNoticeConvertor(club, mgr, activemessage);
                    return processGetPausedMessages(clubNotice, club);
                });
                return Promise.all(getPausedMessagesPromises);
            } else {
                Promise.resolve();
            }
        })
        .then((listOfUnreadedmessages) => {
            if (listOfUnreadedmessages) {
                const content = emailHelpers.getclubNoticeEmailContent(listOfUnreadedmessages);
                if (content && content.contentLength > 0) {
                    const params = {
                        fromEmail: EMAIL_SENDER,
                        fromEmailName: 'Statistics email',
                        replyTo: EMAIL_SENDER,
                        toEmail: club.email,
                        subject: `Here is notification`,
                        content: content.contentText,
                    };
                    const url = `${HOST}/sendingMail/`;
                    return axios.post(url, qs.stringify(params)).catch((err) => {
                        console.error('err in axios', err, url);
                    });
                } else {
                    Promise.resolve();
                }
            } else {
                Promise.resolve();
            }
        })
        .then(() => {
            return database.ref().update({
                [`clubs/${club._id}/lastCheckDate`]: new Date().getTime(),
            });
        });
}


function processGetPausedMessages(clubEmailNotice, club) {
    const {
        userId,
        userName,
    } = clubEmailNotice;
    return new Promise((resolve, reject) => {
        return smoochAccount.appUsers.getMessages(club.appId, userId)
            .then((message) => {
                const messages = commonHelpers.parseMessages(message, userName);
                const userMessages = messages.filter((message) => {
                    return message.role === 'appUser';
                });
                const message = userMessages[userMessages.length - 1];
                if (message) {
                    resolve({notice: clubEmailNotice, message});
                } else {
                    resolve();
                }
            })
            .catch((err) => {
                reject(err);
            });
    });
}


exports.sendingMail = functions.https.onRequest((request, response) => {
    cors(request, response, () => {
        const {replyToName} = request.body;
        const fromEmail = new sendgridMailHelper.Email(request.body.fromEmail, request.body.fromEmailName);
        const replyTo = replyToName ? new sendgridMailHelper.Email(request.body.replyTo, replyToName) : new sendgridMailHelper.Email(request.body.replyTo);
        const toEmail = new sendgridMailHelper.Email(request.body.toEmail);
        const ccEmail = request.body.ccEmail;
        const subject = request.body.subject;
        const content = new sendgridMailHelper.Content('text/html', request.body.content);
        const mail = new sendgridMailHelper.Mail(fromEmail, subject, toEmail, content);
        mail.setReplyTo(replyTo);

        const sendgridRequest = sg.emptyRequest({
            method: 'POST',
            path: '/v3/mail/send',
            body: mail.toJSON(),
        });

        return sg.API(sendgridRequest).then(() => {
            response.send('ok');
        }, (err) => {
            response.send(err);
        });
    });
});

有人说诺言没有正确兑现,但是如果确实如此,为什么有时会奏效?

谢谢您的任何建议。我花了很多时间。 请救我的命。 :/

0 个答案:

没有答案