-
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-load-balancing): update game move logic
- Loading branch information
1 parent
fd73bc3
commit 72da50c
Showing
9 changed files
with
141 additions
and
32 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
36 changes: 36 additions & 0 deletions
36
packages/functions/src/load-balancing/scheduledMoveGame.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,36 @@ | ||
import * as functions from 'firebase-functions'; | ||
import * as admin from "firebase-admin"; | ||
import {FirebaseCollection} from '@pipeline/common'; | ||
import {getDatabase} from "../utils/rtdb"; | ||
import {Game} from "../models/Game"; | ||
import {moveGameFromRTDBToFirestore} from "./utils"; | ||
const db = admin.firestore(); | ||
const logger = functions.logger; | ||
|
||
const DAY_IN_MILLIS = 24 * 60 * 60 * 1000; | ||
|
||
const moveGamesJob = async () => { | ||
const lastActiveGamesDate = new Date(Date.now() - DAY_IN_MILLIS); | ||
|
||
const oldActiveGamesQuery = db.collection(FirebaseCollection.Games) | ||
.where(`lastPlayerDisconnectedAt`, '<=', lastActiveGamesDate); | ||
|
||
const oldActiveGamesSnap = await oldActiveGamesQuery.get(); | ||
logger.log(`Found ${oldActiveGamesSnap.docs.length} old active games`); | ||
|
||
for (const gameDoc of oldActiveGamesSnap.docs) { | ||
const game = gameDoc.data() as Game; | ||
await moveGameFromRTDBToFirestore(gameDoc.id, db, getDatabase(`https://${game.rtdbInstance!}.firebasedatabase.app`)) | ||
} | ||
|
||
logger.log('moveGames job ended'); | ||
} | ||
|
||
export const scheduledMoveGamesJob = functions.region( | ||
'europe-west1' | ||
).runWith({ | ||
memory: '2GB', | ||
timeoutSeconds: 540, | ||
}).pubsub.schedule(`every 60 minutes`).onRun(async () => { | ||
await moveGamesJob(); | ||
}); |
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