Properly call init-sequence-docs on boot.

This commit is contained in:
zkldi
2021-11-02 18:29:45 +00:00
parent 30a8696a99
commit 4f8682d173
3 changed files with 59 additions and 71 deletions
+2 -71
View File
@@ -1,74 +1,5 @@
import { Command } from "commander";
import db from "external/mongo/db";
import CreateLogCtx from "lib/logger/logger";
const program = new Command();
program.option("-f, --force", "Delete existing sequence documents and recreate them.");
program.parse(process.argv);
const options = program.opts();
const logger = CreateLogCtx(__filename);
import { InitSequenceDocs } from "external/mongo/sequence-docs";
if (require.main === module) {
(async () => {
if (options.force) {
await db.counters.remove({});
} else {
const anyCounters = await db.counters.findOne();
if (anyCounters) {
throw new Error(
`Counters are already in database, cannot create them without --force.`
);
}
}
const userWithLargestID = await db.users.findOne(
{},
{
sort: {
id: -1,
},
}
);
const largestUSCSongID = await db.songs.usc.findOne(
{},
{
sort: {
id: -1,
},
}
);
const largestBMSSongID = await db.songs.bms.findOne(
{},
{
sort: {
id: -1,
},
}
);
const Counters = [
{
counterName: "users",
value: userWithLargestID ? userWithLargestID.id + 1 : 1,
},
{
counterName: "usc-song-id",
value: largestUSCSongID ? largestUSCSongID.id + 1 : 1,
},
{
counterName: "bms-song-id",
value: largestBMSSongID ? largestBMSSongID.id + 1 : 1,
},
];
logger.info(`Inserting ${Counters.map((e) => `${e.counterName}: ${e.value}`)}`);
await db.counters.insert(Counters);
process.exit(0);
})();
InitSequenceDocs().then(() => process.exit(0));
}
+53
View File
@@ -0,0 +1,53 @@
import CreateLogCtx from "lib/logger/logger";
import db from "./db";
const logger = CreateLogCtx(__filename);
export async function InitSequenceDocs() {
await db.counters.remove({});
const userWithLargestID = await db.users.findOne(
{},
{
sort: {
id: -1,
},
}
);
const largestUSCSongID = await db.songs.usc.findOne(
{},
{
sort: {
id: -1,
},
}
);
const largestBMSSongID = await db.songs.bms.findOne(
{},
{
sort: {
id: -1,
},
}
);
const Counters = [
{
counterName: "users",
value: userWithLargestID ? userWithLargestID.id + 1 : 1,
},
{
counterName: "usc-song-id",
value: largestUSCSongID ? largestUSCSongID.id + 1 : 1,
},
{
counterName: "bms-song-id",
value: largestBMSSongID ? largestBMSSongID.id + 1 : 1,
},
];
logger.info(`Inserting ${Counters.map((e) => `${e.counterName}: ${e.value}`)}`);
await db.counters.insert(Counters);
}
+4
View File
@@ -6,12 +6,16 @@ import fs from "fs";
import { FormatVersion } from "./lib/constants/version";
import { spawn } from "child_process";
import path from "path";
import { InitSequenceDocs } from "external/mongo/sequence-docs";
const logger = CreateLogCtx(__filename);
logger.info(`Booting ${ServerTypeInfo.name} - ${FormatVersion()} [ENV: ${process.env.NODE_ENV}]`);
logger.info(`Log level is set to ${ServerConfig.LOG_LEVEL}.`);
logger.info(`Loading sequence documents...`);
InitSequenceDocs();
if (ServerConfig.ENABLE_SERVER_HTTPS) {
logger.warn(
"HTTPS Mode is enabled. This should not be used in production, and you should instead run behind a reverse proxy."