0

I have a firebase application that uses Cloud Functions to talk to a Google Cloud SQL instance. These cloud functions are used to perform CRUD actions. I would like to ensure that the database reflects the CRUD operations, as such, run migration code every time I push new function code to ensure the database is always up to date.

I do this in a global function

const functions = require('firebase-functions')
const pg = require('pg')    

// Create if not exists database
(function() {
    console.log('create db...')
})()

exports.helloWorld = functions.https.onRequest((request, response) => {
    console.log('Hello from Firebase function log!')
    response.send('Hello from Firebase!')
})

exports.helloWorld2 = functions.https.onRequest((request, response) => {
    console.log('Hello from Firebase function log 2!')
    response.send('Hello from Firebase 2!')
})

This console log then runs twice when I deploy.

Now I understand that there is no way of knowing how many instances Cloud Functions will spin up for the functions, as stated in their docs:

The global scope in the function file, which is expected to contain the function definition, is executed on every cold start, but not if the instance has already been initialized.`

If I add a third function, this console log is now shown 3 times in the logs, instead of 2, one for each function. Would it be correct in saying that there's a new instance for every single function uploaded? I am trying to understand what happens under the hood when I upload a set of cloud functions.

If so - is there no reliable way to run migration code inside a global function in cloud functions?

1 Answer 1

1

What you're doing isn't a supported use case for Cloud Functions. Cloud Functions code runs in response to events that occur in your project. There is no "one time" function invocations that happen on deployment. If you need to run code a single time, just run that from your desktop or some other server you control.

You should also strive to minimize the amount of work that happens in the global scope of your functions. Globals will be instantiated and run once for each allocated server instance running a function in your app, as each function runs in full isolation of each other, and each has its own copy of everything. Watch my video about function scaling and isolation to better understand this behavior.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Doug, this answers my question perfectly. I understand my approach is certainly a hacky way, though it now explains the behaviour I was getting. Looks like the answer to my question in bold is 'Yes'. Great video by the way, will check out more.
Globals will not be instantiated once for each function in your app. See that for more information. medium.com/@DazWilkin/cloud-functions-global-scope-7450a5f08038
There must be a way to run initializtion code in a cloud function otherwise cloud functions have little user. For example, how is a db connection pool initialized?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.