【问题标题】:Cloud Function with Multiple Triggers - Scheduled and onCall具有多个触发器的云函数 - 计划和 onCall
【发布时间】:2021-03-01 13:18:14
【问题描述】:

我有一个云功能,只要用户在网络应用程序上执行一组操作并且每天在指定时间执行一组操作,我就想运行它。为了不重复代码和未来的功能/错误修复,我想从一个函数/文件中运行两者。

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

【问题讨论】:

    标签: firebase google-cloud-functions


    【解决方案1】:

    您可以在一个函数中编写业务逻辑,从两个云函数调用该函数。大致如下,具有异步业务逻辑并使用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;
        
    }
    

    【讨论】:

    • 啊,是的。极好的。然后我想我将调用用户验证/权限检查移动到 myFunctionCalledFromTheApp?在 asyncBusineesLogic 中包含上下文方面会很好......
    • 您可以很好地将context 对象传递给asyncBusinessLogic() 函数,但请注意auth 对象只会为可调用函数填充,而不是为预定函数填充(参见@ 987654321@)。由您来管理这两种不同的情况。
    • 非常感谢!那么如何在不暴露 onCall 的情况下验证计划函数是否具有“管理员”权限?只是!context.auth 似乎不安全?
    • 您可以通过使用参数来识别调用者函数,例如async function asyncBusinessLogic(context, caller);await asyncBusinessLogic(context, 'callable');await asyncBusinessLogic(context, 'scheduled');
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-06
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多