具有多个触发器的云功能-计划和onCall [英] Cloud Function with Multiple Triggers - Scheduled and onCall

查看:56
本文介绍了具有多个触发器的云功能-计划和onCall的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个云功能,我希望每当用户在指定时间每天在Web应用程序上执行一组操作时运行一次.为了不重复代码和将来的功能/错误修复,我想从一个功能/文件中同时运行.

I have a cloud function that I would like to run whenever a user performs a set of actions on the web app AND daily at a specified time. In the interest of not duplicating code and future features/bug fixes, I'd like to run both from one function/file.

对此流程的任何建议/参考将不胜感激!

Any suggestions/references on this flow would be greatly appreciated!

推荐答案

您可以在一个函数中编写业务逻辑,该函数是从两个Cloud Functions中调用的.遵循以下方式,具有异步业务逻辑并使用 async/await :

You can write your business logic in one function, that you call from the two Cloud Functions. Something along the following lines, with an asynchronous business logic and the use of async/await:

exports.myFunctionCalledFromTheApp = functions.https.onCall(async (data, context) => {
    try {
        const result = await asyncBusinessLogic();
        return { result: result }
    } catch (error) {
        // ...
    }
});

exports.myFunctionCalledByScheduler = functions.pubsub.schedule('every 24 hours').onRun(async (context) => {
    try {
        await asyncBusinessLogic();
        return null;
    } catch (error) {
        // ...
        return null;
    }
});


async function asyncBusinessLogic() {
    
    const result = await anAsynchronousJob();
    return result;
    
}

这篇关于具有多个触发器的云功能-计划和onCall的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆