Skip to content

Commit 8456e86

Browse files
committed
docs(functions-general): improve documentation
1 parent 6ba5ab3 commit 8456e86

File tree

5 files changed

+12
-39
lines changed

5 files changed

+12
-39
lines changed

packages/functions/src/load-balancing/onConnectionDelete.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ const logger = functions.logger;
1212
*
1313
* The proper document of Firestore, representing that RTDB instance, is updated incrementing by -1
1414
*
15-
* Next, an RTDB query is performed to look for the online users for those games.
16-
* If more than zero were found, it means there is still someone in the game
17-
* Otherwise, we can move the game from RTDB back to Firestore, for each game.
15+
* Cards that are locked by the disconnected user are unlocked
1816
*/
1917

2018
export async function handler(snapshot: functions.database.DataSnapshot, context: functions.EventContext, rtdbId:string, rtdbUrl:string) {

packages/functions/src/load-balancing/scheduledMoveGame.ts

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ const logger = functions.logger;
88

99
export const DAY_IN_MILLIS = 24 * 60 * 60 * 1000;
1010

11+
/**
12+
* Scheduled job to move stale games from the real time database to Firestore.
13+
*
14+
* A game is stale if no one connects to it for a day.
15+
*/
1116
export const moveGamesJob = async () => {
1217
const lastActiveGamesDate = new Date(Date.now() - DAY_IN_MILLIS);
1318

packages/functions/src/load-balancing/selectBestRTDBInstance.ts

-33
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,6 @@ import {getDatabase, PROJECT_ID} from "../utils/rtdb";
66
import {Game} from "../models/Game";
77
const logger = functions.logger;
88

9-
/*const getNextRTDBInstanceNum = async (): Promise<number> => {
10-
const res = await axios.get(`https://firebasedatabase.googleapis.com/v1beta/projects/${PROJECT_ID}/locations/-/instances?pageSize=100`, {
11-
headers: {
12-
'Authorization': `Bearer `,
13-
},
14-
})
15-
const instances = res.data.instances as any[];
16-
return instances.length;
17-
}
18-
19-
const createRTDBInstance = async (databaseId: string) => {
20-
await axios.get(`https://firebasedatabase.googleapis.com/v1beta/projects/${PROJECT_ID}/locations/${RTDB_LOCATION}/instances?database_id=${databaseId}`, {
21-
headers: {
22-
'Authorization': `Bearer `,
23-
},
24-
})
25-
}
26-
27-
const createNewRTDBInstance = async () => {
28-
const nextNum = await getNextRTDBInstanceNum();
29-
const newRTDBInstanceName = getRTDBInstanceName(nextNum);
30-
await createRTDBInstance(newRTDBInstanceName);
31-
await db.collection(FirebaseCollection.RTDBInstances).doc(newRTDBInstanceName).set({
32-
createdAt: FieldValue.serverTimestamp(),
33-
connectionsCount: 0,
34-
} as RTDBInstance)
35-
admin.app().database(`https://secondary_db_url.firebaseio.com`)
36-
37-
return newRTDBInstanceName;
38-
}
39-
40-
*/
41-
429
/**
4310
* API to effectively balance the game load by selected the best RTDB instance, in terms of minimum number connections (tabs, browsers, devices, ...).
4411
* It needs a "gameId" as url parameter and can be called only by authenticated users.

packages/functions/src/load-balancing/utils.ts

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ const logger = functions.logger;
1010

1111
type CardStateEntity = CardState & {id: string};
1212

13-
// TODO refactor this
1413
function unlockAndZIndexNormalize(cardsSnap: admin.database.DataSnapshot) {
1514
let newCards: {[key: string]: CardState} | null;
1615

packages/functions/src/utils/rtdb.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@ import * as admin from "firebase-admin";
22

33
export const PROJECT_ID = JSON.parse(process.env.FIREBASE_CONFIG!).projectId;
44

5-
6-
export function getDatabase( url: string) {
5+
/**
6+
* Returns the database management object given the url.
7+
*
8+
* @param url the database complete url
9+
*/
10+
export function getDatabase( url: string): admin.database.Database {
711
const app = admin.apps.find(a => a?.name === url);
812

913
if (app) {

0 commit comments

Comments
 (0)