feat: make jobs exit correctly (#1037)

This commit is contained in:
zkldi
2024-04-16 00:59:38 +01:00
committed by GitHub
parent 3b037c2dde
commit 1a30c8f130
9 changed files with 22 additions and 54 deletions
+2 -7
View File
@@ -3,6 +3,7 @@ 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";
import { WrapScriptPromise } from "utils/misc";
const logger = CreateLogCtx(__filename);
@@ -47,11 +48,5 @@ export async function BacksyncBMSPMSSongsAndCharts() {
}
if (require.main === module) {
BacksyncBMSPMSSongsAndCharts()
.then(() => process.exit(0))
.catch((err: unknown) => {
logger.error(`Failed to backsync bms/pms songs and charts.`, { err }, () => {
process.exit(1);
});
});
WrapScriptPromise(BacksyncBMSPMSSongsAndCharts(), logger);
}
+2 -7
View File
@@ -2,6 +2,7 @@
import { PullDatabaseSeeds } from "lib/database-seeds/repo";
import CreateLogCtx from "lib/logger/logger";
import fetch from "node-fetch";
import { WrapScriptPromise } from "utils/misc";
import type { ChartDocument } from "tachi-common";
const AI_URL = "https://bms.hexlataia.xyz/tables/json/ai.json";
@@ -32,11 +33,5 @@ export async function UpdateAILevels() {
}
if (require.main === module) {
UpdateAILevels()
.then(() => process.exit(0))
.catch((err: unknown) => {
logger.error(`Failed to sync BMS AI Table.`, { err }, () => {
process.exit(1);
});
});
WrapScriptPromise(UpdateAILevels(), logger);
}
+2 -8
View File
@@ -5,7 +5,7 @@ import CreateLogCtx from "lib/logger/logger";
import { DeorphanIfInQueue } from "lib/orphan-queue/orphan-queue";
import { BMS_TABLES } from "tachi-common";
import { InitaliseFolderChartLookup } from "utils/folder";
import { FormatBMSTables } from "utils/misc";
import { FormatBMSTables, WrapScriptPromise } from "utils/misc";
import type { BMSTableEntry } from "bms-table-loader";
import type { FilterQuery } from "mongodb";
import type { BMSTableInfo, ChartDocument } from "tachi-common";
@@ -218,11 +218,5 @@ export async function SyncBMSTables() {
}
if (require.main === module) {
SyncBMSTables()
.then(() => process.exit(0))
.catch((err: unknown) => {
logger.error(`Failed to sync BMS Tables.`, { err }, () => {
process.exit(1);
});
});
WrapScriptPromise(SyncBMSTables(), logger);
}
+2 -7
View File
@@ -3,6 +3,7 @@
import db from "external/mongo/db";
import CreateLogCtx from "lib/logger/logger";
import { ReprocessOrphan } from "lib/score-import/framework/orphans/orphans";
import { WrapScriptPromise } from "utils/misc";
const logger = CreateLogCtx(__dirname);
@@ -42,11 +43,5 @@ export async function DeoprhanScores() {
}
if (require.main === module) {
DeoprhanScores()
.then(() => process.exit(0))
.catch((err: unknown) => {
logger.error(`Failed to de-orphan scores.`, { err }, () => {
process.exit(1);
});
});
WrapScriptPromise(DeoprhanScores(), logger);
}
+3 -1
View File
@@ -102,6 +102,8 @@ if (require.main === module) {
// This is a severe error, not an error. Running the UGS snapshot every day is necessary.
logger.severe(`Failed to snapshot user game stats.`, { err });
process.exit(1);
setTimeout(() => {
process.exit(1);
}, 1000);
});
}
+2 -9
View File
@@ -3,6 +3,7 @@ import { PullDatabaseSeeds } from "lib/database-seeds/repo";
import CreateLogCtx from "lib/logger/logger";
import { RecalcAllScores } from "utils/calculations/recalc-scores";
import fetch from "utils/fetch";
import { WrapScriptPromise } from "utils/misc";
import type {
ChartDocument,
Difficulties,
@@ -178,13 +179,5 @@ export async function UpdatePoyashiData() {
}
if (require.main === module) {
UpdatePoyashiData()
.then(() => {
process.exit(0);
})
.catch((err: unknown) => {
logger.error("Failed to update poyashi data.", { err }, () => {
process.exit(1);
});
});
WrapScriptPromise(UpdatePoyashiData(), logger);
}
+2 -9
View File
@@ -6,6 +6,7 @@ import CreateLogCtx from "lib/logger/logger";
import { parse } from "node-html-parser";
import { RecalcAllScores } from "utils/calculations/recalc-scores";
import fetch from "utils/fetch";
import { WrapScriptPromise } from "utils/misc";
import { FindSongOnTitle } from "utils/queries/songs";
const logger = CreateLogCtx(__filename);
@@ -120,13 +121,5 @@ function ParseTierStr(tierStr: string) {
}
if (require.main === module) {
UpdateDPTiers()
.then(() => {
process.exit(0);
})
.catch((err: unknown) => {
logger.error(`Failed to update DP Tiers.`, { err }, () => {
process.exit(1);
});
});
WrapScriptPromise(UpdateDPTiers(), logger);
}
+1 -5
View File
@@ -205,11 +205,7 @@ async function FetchSP12Data() {
logger.info(`Finished recalcing scores.`);
await BacksyncCollection(
"charts-iidx",
db.charts.iidx,
"Update SP12 Tierlist"
);
await BacksyncCollection("charts-iidx", db.charts.iidx, "Update SP12 Tierlist");
}
process.exit(0);
+6 -1
View File
@@ -1,5 +1,5 @@
import { ONE_MEGABYTE } from "lib/constants/filesize";
import { ONE_HOUR } from "lib/constants/time";
import { ONE_HOUR, ONE_SECOND } from "lib/constants/time";
import { TachiConfig } from "lib/setup/config";
import { GetGameConfig, GetGamePTConfig } from "tachi-common";
import { exec } from "child_process";
@@ -322,6 +322,11 @@ export function WrapScriptPromise(promise: Promise<unknown>, logger: KtLogger) {
code = 1;
})
.finally(() => {
// die in 10 seconds or when the logger ends, whatever ends earlier.
setTimeout(() => {
process.exit(1);
}, ONE_SECOND * 10);
logger.end(() => {
process.exit(code);
});