firebase - 具有多个触发器的云函数 - 计划和 onCall
问题描述
我有一个云功能,每当用户在网络应用程序上执行一组操作并且每天在指定时间执行一组操作时,我都想运行它。为了不重复代码和未来的功能/错误修复,我想从一个函数/文件中运行两者。
对此流程的任何建议/参考将不胜感激!
解决方案
您可以在一个函数中编写业务逻辑,从两个云函数调用该函数。大致如下,具有异步业务逻辑和使用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;
}
推荐阅读
- javascript - TypeError: _this.props.editAp 不是函数
- azure - Export-AzureRmAutomationDscConfiguration 无法反序列化响应
- algorithm - 更复杂的散列函数会导致更快的构建表吗?
- regex - 从末尾开始匹配正则表达式组
- javascript - 在 React 应用程序中,JSON.Stringify 仅显示第一个项目和一个长度项目,而不是其他项目
- vue.js - 突变 vuex 存储外部处理程序问题
- command-line - 从命令行对文件执行 emacs 命令
- javascript - Charts.js - 如何为每个数据集设置自定义工具提示文本
- azure-data-explorer - 加入两个不带相等运算符的数据集
- javascript - 我需要一个用于搜索 . 字符串中的(点)后跟 2 位数字