2 Commits

Author SHA1 Message Date
0f1c101acc cronjob stuff
All checks were successful
Deploy Backend / deploy (push) Successful in 3m52s
2024-10-10 01:22:09 +01:00
bca3732f1c remove the logs for this - it spams alot 2024-10-10 01:17:55 +01:00
2 changed files with 19 additions and 41 deletions

View File

@ -14,6 +14,9 @@ import { Config } from "./common/config";
import { setLogLevel } from "@typegoose/typegoose";
import PlayerController from "./controller/player.controller";
import { PlayerService } from "./service/player.service";
import { cron } from "@elysiajs/cron";
import { PlayerDocument, PlayerModel } from "./model/player";
import { getMidnightAlignedDate } from "@ssr/common/utils/time-utils";
// Load .env file
dotenv.config({
@ -26,6 +29,22 @@ await mongoose.connect(Config.mongoUri!); // Connect to MongoDB
setLogLevel("DEBUG");
export const app = new Elysia();
app.use(
cron({
name: "player-statistics-tracker-cron",
pattern: "0 1 * * *", // Every day at 00:01 (midnight)
timezone: "Europe/London",
run: async () => {
console.log("Tracking player statistics...");
const players: PlayerDocument[] = await PlayerModel.find({});
for (const player of players) {
await PlayerService.trackScoreSaberPlayer(getMidnightAlignedDate(new Date()), player);
}
console.log("Finished tracking player statistics.");
},
})
);
/**
* Custom error handler
*/
@ -99,11 +118,6 @@ app.use(
})
);
/**
* Start cronjobs
*/
PlayerService.initCronjobs();
/**
* Swagger Documentation
*/

View File

@ -1,42 +1,11 @@
import { PlayerDocument, PlayerModel } from "../model/player";
import { NotFoundError } from "../error/not-found-error";
import { cron } from "@elysiajs/cron";
import { app } from "../index";
import { getDaysAgoDate, getMidnightAlignedDate } from "@ssr/common/utils/time-utils";
import { scoresaberService } from "@ssr/common/service/impl/scoresaber";
import ScoreSaberPlayerToken from "@ssr/common/types/token/scoresaber/score-saber-player-token";
import { InternalServerError } from "../error/internal-server-error";
export class PlayerService {
/**
* Initialize the cron jobs
*/
public static initCronjobs() {
(async () => {
console.log("Tracking player statistics...");
const players: PlayerDocument[] = await PlayerModel.find({});
for (const player of players) {
await PlayerService.trackScoreSaberPlayer(getMidnightAlignedDate(new Date()), player);
}
console.log("Finished tracking player statistics.");
})();
app.use(
cron({
name: "player-statistics-tracker-cron",
pattern: "0 1 * * *", // Every day at 00:01 (midnight)
run: async () => {
console.log("Tracking player statistics...");
const players: PlayerDocument[] = await PlayerModel.find({});
for (const player of players) {
await PlayerService.trackScoreSaberPlayer(getMidnightAlignedDate(new Date()), player);
}
console.log("Finished tracking player statistics.");
},
})
);
}
/**
* Get a player from the database.
*
@ -46,10 +15,8 @@ export class PlayerService {
* @throws NotFoundError if the player is not found
*/
public static async getPlayer(id: string, create: boolean = false): Promise<PlayerDocument> {
console.log(`Fetching player "${id}"...`);
let player: PlayerDocument | null = await PlayerModel.findById(id);
if (player === null && !create) {
console.log(`Player "${id}" not found.`);
throw new NotFoundError(`Player "${id}" not found`);
}
if (player === null) {
@ -65,9 +32,6 @@ export class PlayerService {
}
player.trackedSince = new Date();
await this.seedPlayerHistory(player, playerToken);
console.log(`Created player "${id}".`);
} else {
console.log(`Found player "${id}".`);
}
return player;
}