-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(functions-general): add deploy of functions to all rtdb instances
- Loading branch information
Showing
12 changed files
with
199 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 18 additions & 15 deletions
33
packages/functions/src/load-balancing/onOnlineGameStatusCreate.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,35 @@ | ||
import * as functions from 'firebase-functions'; | ||
import * as admin from "firebase-admin"; | ||
import {FirebaseCollection, RTDBInstance, RTDBPaths} from "@pipeline/common"; | ||
import {PROJECT_ID} from "../utils/rtdb"; | ||
import exportFunctionsOnAllRTDBInstances from "../utils/exportFunctionsOnAllRTDBInstances"; | ||
import FieldValue = admin.firestore.FieldValue; | ||
|
||
const db = admin.firestore(); | ||
const logger = functions.logger; | ||
|
||
const INSTANCE_ID = `${PROJECT_ID}-default-rtdb` | ||
|
||
/** | ||
* It triggers when the path /connections/{gameId}/{userId} of that RTDB instance is created. | ||
* | ||
* The proper document of Firestore, representing that RTDB instance, is updated incrementing by 1 | ||
* | ||
*/ | ||
async function handler(snapshot: functions.database.DataSnapshot, context: functions.EventContext, rtdbId: string) { | ||
const userId = context.params.userId; | ||
const gameId = context.params.gameId; | ||
// TODO snapshot.instance in emulator is always the default one | ||
// const docInstanceId = parseRTDBInstanceId(snapshot.instance); | ||
|
||
export const onOnlineGameStatusCreate = functions.database.instance(INSTANCE_ID).ref(`/${RTDBPaths.Connections}/{gameId}/{userId}`) | ||
.onCreate(async (snapshot, context) => { | ||
logger.log(`User ${userId} just created connection for game ${gameId} in ${rtdbId} snapshotInstance ${snapshot.instance}`); | ||
await db.collection(FirebaseCollection.RTDBInstances).doc(rtdbId) | ||
.update({ | ||
connectionsCount: FieldValue.increment(1) as any, | ||
} as Partial<RTDBInstance>); | ||
} | ||
|
||
const instanceId = INSTANCE_ID; | ||
const userId = context.params.userId; | ||
const gameId = context.params.gameId; | ||
|
||
logger.log(`User ${userId} just created connection for game ${gameId}`); | ||
const docInstanceId = instanceId.split(`${PROJECT_ID}-`)[1]; | ||
await db.collection(FirebaseCollection.RTDBInstances).doc(docInstanceId) | ||
.update({ | ||
connectionsCount: FieldValue.increment(1) as any, | ||
} as Partial<RTDBInstance>); | ||
}); | ||
exportFunctionsOnAllRTDBInstances( | ||
'onOnlineGameStatusCreate', | ||
(builder, dbId) => builder.ref(`/${RTDBPaths.Connections}/{gameId}/{userId}`) | ||
.onCreate((snapshot, context) => handler(snapshot, context, dbId)), | ||
exports | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const rtdbInstancesUrl = [ | ||
{ | ||
url: 'https://pipeline-game-dev-default-rtdb.firebasedatabase.app', | ||
name: 'pipeline-game-dev-default-rtdb', | ||
id: 'default-rtdb', | ||
}, | ||
{ | ||
url: 'https://pipeline-game-dev-instance-1.firebasedatabase.app', | ||
name: 'pipeline-game-dev-instance-1', | ||
id: 'instance-1', | ||
}, | ||
]; | ||
|
||
export default rtdbInstancesUrl; |
This file was deleted.
Oops, something went wrong.
39 changes: 39 additions & 0 deletions
39
packages/functions/src/utils/exportFunctionsOnAllRTDBInstances.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import rtdbInstancesUrl from "../rtdbInstances"; | ||
import * as functions from "firebase-functions"; | ||
|
||
/** | ||
* Exports a function with a trigger on all rtdbInstances defined. | ||
* | ||
* the functions will be exported using the functionName and the suffix `[dbId]` | ||
* | ||
* @param functionName the name of the function to export | ||
* @param functionBuilder receive a database instance and returns a function instance | ||
* @param exportObj the exports object to be filled with the functions to export | ||
*/ | ||
export default function exportFunctionsOnAllRTDBInstances<T>( | ||
functionName: string, | ||
functionBuilder: (build: functions.database.InstanceBuilder, rtdbId:string, rtdbUrl:string) => functions.CloudFunction<T>, | ||
exportObj: any, | ||
) { | ||
for (const db of rtdbInstancesUrl) { | ||
exportObj[`${functionName}[${db.id.replace('-', '_')}]`] = functionBuilder( | ||
functions.database.instance(db.name), | ||
db.id, | ||
db.url | ||
); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* | ||
* Re-export all the elements inside object inside exportsObj | ||
* | ||
* @param exportsObj the export object to fill | ||
* @param object the object containing elements to re-export | ||
*/ | ||
export function exportEverythingFrom(exportsObj: any, object: any) { | ||
for (const key in object) { | ||
exportsObj[key] = object[key]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import {PROJECT_ID} from "./rtdb"; | ||
import * as functions from "firebase-functions"; | ||
|
||
const logger = functions.logger; | ||
|
||
/** | ||
* Returns the instance id from the complete db url | ||
* | ||
* @param instanceUrl | ||
*/ | ||
export default function parseRTDBInstanceId(instanceUrl: string) { | ||
let completeName; | ||
if (instanceUrl.includes('localhost')) { | ||
completeName = instanceUrl.split('ns=')[1]; | ||
} else { | ||
completeName = instanceUrl.replace('https://', '').split('.')[0]; | ||
} | ||
logger.info( | ||
`[parseRTDBInstanceId] instanceUrl ${instanceUrl} | ||
completeName ${completeName} id ${completeName.replace(`${PROJECT_ID}-`, '')}` | ||
); | ||
return completeName.replace(`${PROJECT_ID}-`, ''); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,17 @@ | ||
import * as admin from "firebase-admin"; | ||
|
||
export const PROJECT_ID = JSON.parse(process.env.FIREBASE_CONFIG!).projectId; | ||
|
||
export const getRTDBInstanceName = (num: number) => { | ||
return `${PROJECT_ID}-${num}-rtdb` | ||
}; | ||
|
||
export function getAppForDB(dbId: string, url: string) { | ||
const app = admin.apps.find(a => a?.name === dbId); | ||
|
||
if (app) { | ||
return app; | ||
} else { | ||
return admin.initializeApp({ | ||
databaseURL: url, | ||
}, dbId); | ||
} | ||
|
||
} |