diff --git a/server/src/lib/jobs/backsync-kamai-data.ts b/server/src/lib/jobs/backsync-kamai-data.ts new file mode 100644 index 000000000..105a9bfa7 --- /dev/null +++ b/server/src/lib/jobs/backsync-kamai-data.ts @@ -0,0 +1,57 @@ +/* eslint-disable no-await-in-loop */ +import db from "external/mongo/db"; +import { VERSION_INFO } from "lib/constants/version"; +import { PullDatabaseSeeds } from "lib/database-seeds/repo"; +import CreateLogCtx from "lib/logger/logger"; + +const logger = CreateLogCtx(__filename); + +/** + * The tachi-server may have its BMS or PMS database update. It needs to sync this + * information back with the seeds. + */ +export async function BacksyncKamaiSongsAndCharts() { + for (const branch of ["staging", `release/${VERSION_INFO.major}.${VERSION_INFO.minor}`]) { + const repo = await PullDatabaseSeeds(undefined, branch); + + for (const game of ["iidx"] as const) { + logger.info(`Fetching ${game} songs from DB.`); + + // did you know, this code is liable to blow up in my face and OOM one day? + let songs = await db.songs[game].find({}); + + logger.info(`Found ${songs.length} ${game} songs.`); + + await repo.WriteCollection(`songs-${game}`, songs); + + // @ts-expect-error This is obviously making something nullable when it shouldn't be. + // but if we don't *force* node to free this damn memory, it kills itself when it + // tries to read even more stuff. + songs = null; + + logger.info(`Fetching ${game} charts from DB.`); + let charts = await db.charts[game].find({}); + + logger.info(`Found ${charts.length} bms charts.`); + + await repo.WriteCollection(`charts-${game}`, charts); + + // @ts-expect-error See previous expect-error. + charts = null; + } + + await repo.CommitChangesBack(`Backsync Kamai Songs/Charts ${new Date().toISOString()}`); + + await repo.Destroy(); + } +} + +if (require.main === module) { + BacksyncKamaiSongsAndCharts() + .then(() => process.exit(0)) + .catch((err: unknown) => { + logger.error(`Failed to backsync bms/pms songs and charts.`, { err }, () => { + process.exit(1); + }); + }); +} diff --git a/server/src/scripts/iidx-tierlist/get-sp12-data.ts b/server/src/lib/jobs/get-sp12-data.ts similarity index 100% rename from server/src/scripts/iidx-tierlist/get-sp12-data.ts rename to server/src/lib/jobs/get-sp12-data.ts