Firebase云功能的超时效率

时间:2018-10-15 15:49:59

标签: javascript typescript firebase google-cloud-functions

我有一个函数,每次向数据库添加新元素时都会发送电子邮件,就像这样:

export const onWorkCreation = functions.database.ref('/Works/{workId}').onCreate(async (snapshot, context) => {
    const work = snapshot.val();
    // const emails = ['email1@email.com', 'email2@email.com', 'email3@email.com'];
    // TODO sprawdz z jakiej kategorii zadanie, wyslij do uzytkownikow ktorzy maja te kategorie + link do deaktywacji emaili.
    let calls = [];
    const persons = admin.database().ref('Users').orderByChild('userType').equalTo('person').once('value').then(r => r.val()).catch(err => console.log(1, err));
    const companies = admin.database().ref('Users').orderByChild('userType').equalTo('company').once('value').then(r => r.val()).catch(err => console.log(2, err));
    const undefineds = admin.database().ref('Users').orderByChild('userType').equalTo('undefined').once('value').then(r => r.val()).catch(err => console.log(3, err));
    calls.push(persons, companies, undefineds);

    let users = await Promise.all(calls).catch(err => console.log(4, err));
    users = [...arrayFromObject(users[0]), ...arrayFromObject(users[1]), ...arrayFromObject(users[2])];
    users.filter(u => u.receivesNotifications === undefined || u.receivesNotifications === true);
    const usersIds = [];
    for (const i in users) {
        const user = users[i];
        if (user.testInfo[work.category] !== undefined && user.testInfo[work.category.toLowerCase()].status.toLowerCase() === 'approved' && user.receivesNotifications !== false) {
            usersIds.push(user.id);
        } else {
            // console.log(work);
            // console.log(user.testInfo[work.category]);
            // console.log(work.category);
            // console.log(2, user.testInfo[work.category] !== undefined, 3, user.testInfo[work.category] !== undefined && user.testInfo[work.category.toLowerCase()].status.toLowerCase() === 'approved', 4, user.receivesNotifications !== false)
        }
    }
    calls = [];
    for (const i in usersIds) {
        calls.push(0);
        try {
            calls[i] = await admin.auth().getUser(usersIds[i]).then(r => r).catch(err => console.log(5, err, usersIds[i]));
        } catch (e) {
            console.log('user', usersIds[i]);
        }
    }
    users = await Promise.all(calls).catch(err => console.log(6, err));
    users = arrayFromObject(users);
    console.log('users', users);
    const usersDetails = [];
    for (const i in users) {
        const user = {
            email: users[i].email,
            id: users[i].uid,
        };
        usersDetails.push(user);
    }
    calls = [];
    const mailTransport = nodemailer.createTransport({
        service: 'gmail',
        auth: {
            user: 'USER',
            pass: 'PASS'
        },
    });
    for (const i in usersDetails) {
        const user = usersDetails[i];
        calls.push(mailTransport.sendMail({
            from: `ZdajTo <noreply@zdajto.com>`,
            to: user.email,
            subject: `Dostepne sa nowe zadania!`,
            html: `<p>Hej! Sprawdz aplikacje ZdajTo! Dostepne sa nowe zadania z kategorii ${work.category}! Aby zrezygnowac z otrzymywania emaili kliknij w <a href="https://us-central1-uczsieapp.cloudfunctions.net/adminHelper/cancelEmailSubscription/${user.id}" target="_blank">ten link</a></p>`,
        }).then(() => null).catch(err => console.log(7, err, user.email)));
    }
    return Promise.all(calls).then(() => console.log('Emails sent')).catch(err => console.log(8, err));
});

这是很多代码,但总而言之,它只是为某些用户抓取电子邮件并将电子邮件发送到这些地址。

现在。每当创建一个新的工作孩子时,我都会将其激发。是否可以检查孩子是否在db中挂了5分钟以上?

我想要实现的目标:

如果work的属性(available)在5分钟内没有更改,我想再次发送电子邮件。我可以通过触发超时循环来实现,但是我希望会有更好的方法。

1 个答案:

答案 0 :(得分:1)

为此,我将使用CRON函数根据状态和createTime查询工作。 (添加工作元素时,您将要填充createTime值。)执行CRON函数的最简单方法是使用Azure函数,但是,您可能还会查看GCP / firebase固有的其他选项。