问题
I have set multiple triggers, like:
exports.doSomething = functions.firestore.document('col/{doc}').onCreate(event => {})
Than I have a function that I want to run instant when I deployed. That looks something like this:
now()
function now(){
console.log("running function")
}
And I get this in my logs:
Why does it run so many times and gets called by other functions?
Full code, just tested it and running function gets called 4 times, the same amount as triggers I have set:
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")
}
回答1:
Cloud Functions doesn't provide a way to run a bit of code at the time of deploy. Your function is obviously being run multiple times, and you should expect it to be run even more times as new server instances are allocated (and destroyed) in tandem with the load on your project. This is how Cloud Functions scales. There is definitely not just one server instance handling all your requests. You should expect any global code to be run repeatedly.
If you want to run some code exactly once after deploy, make an exported funciton (maybe HTTPS) and trigger it after you deploy. Maybe you could write a script that both deploys your code then triggers the function with curl or some other mechanism you choose.
来源:https://stackoverflow.com/questions/47360570/why-does-google-cloud-function-run-a-global-function-multiple-times-after-deploy