I have a variety of functions for Cloud Functions that use things derived from firebase-admin/app
's initializeApp()
. Since multiple functions use this, I had the idea of calling initializeApp()
close to the beginning of the Typescript/Javascript. Like this:
import { initializeApp } from 'firebase-admin/app';
...
const app = initializeApp();
...
export const activeUser = https.onCall(
async (email: string, ctx: CallableContext) => {
...
const auth = getAuth(app);
const firestore = getFirestore(app);
try {
const user = await auth.getUserByEmail(email);
const snpsht = await firestore.collection('users').doc(user.uid).get();
...
export const userExists = https.onCall(
async (email: string, ctx: CallableContext) => {
if (!emulating() && forbiddenOrigin(ctx)) {
return `${ERRORFLAG}: forbidden`;
}
const auth = getAuth(app);
...
A testing headache showed me that I forgot about calling deleteApp(app)
.
Is deleteApp()
necessary, or will the app be deleted as a side effect of Cloud tearing down the function?
Should initializeApp()
/deleteApp()
be called within each function where an app is needed, ie within activeUser
and userExists
in my above example?
Is there any way to tell Cloud Functions to run setup/teardown code before running a given function?
CodePudding user response:
A call to deleteApp is not necessary. When a Cloud Functions instance terminates, all memory gets shut down with it (which is what deleteApp would do). The entire server instance is completely gone.
You only need to call deleteApp if you are controlling a process that needs to continue after the call to deleteApp with the Firebase resources freed from memory. Cloud Functions does not meet this criteria since you don't control the process startup or shutdown.