2017-11-17 185 views
1

我设置了多个触发器,如:为什么Google Cloud Function在部署后多次运行全局功能?

exports.doSomething = functions.firestore.document('col/{doc}').onCreate(event => {}) 

比我有我想要运行时刻,当我部署一个功能。这看起来是这样的:

now() 
function now(){ 
    console.log("running function") 
} 

而且我得到这个在我的日志:

enter image description here

它为什么跑这么多次,并得到其他函数调用?

的完整代码,只是测试它和运行函数被调用4次,相同数量的触发我已设置:

const functions = require('firebase-functions'); 
const admin = require('firebase-admin'); 
admin.initializeApp(functions.config().firebase); 
const db = admin.firestore() 
var geoip = require('geoip-lite'); 

exports.z = functions.firestore.document('converasdassdtIP/{UID}').onCreate(event => { }) 
exports.x = functions.firestore.document('sads/{UID}').onCreate(event => { }) 
exports.n = functions.firestore.document('asdasasdsa/{UID}').onCreate(event => { }) 
exports.m = functions.firestore.document('converasdasddtIP/{UID}').onCreate(event => { }) 

now() 
function now(){ 
    console.log("running function") 
} 
+0

你的代码在哪里调用'now()'?你可以添加你的一些'function/index.js'到这个问题吗? – Callam

+0

@Callam看到我的编辑 –

+0

每次触发函数时,都会调用'now()'的调用,因为它位于'index.js'文件的根范围内,因此每个运行的云功能都会看到登录。你想用这个日志实现什么?你想什么时候打印? – Callam

回答

4

云功能,不提供了一种在运行一段代码部署时间。您的功能显然会多次运行,并且您应该预计它将运行甚至更​​多次,因为新的服务器实例将与您的项目负载一起分配(和销毁)。这就是云端功能如何扩展。绝对不只有一个服务器实例处理您的所有请求。您应该期望任何全局代码重复运行。

如果您想在部署完成后只运行一次代码,请创建导出功能(可能是HTTPS)并在部署后触发它。也许你可以编写一个脚本来部署你的代码,然后使用curl或你选择的其他机制来触发这个函数。