mirror of
https://github.com/zkldi/Tachi.git
synced 2026-09-22 23:18:05 +03:00
feat: automatically free import locks (#1024)
* feat: automatically free import locks * feat: automigration
This commit is contained in:
Vendored
+3
-1
@@ -174,7 +174,9 @@ const db = {
|
||||
"bms-course-lookup": monkDB.get<BMSCourseDocument>("bms-course-lookup"),
|
||||
"api-tokens": monkDB.get<APITokenDocument>("api-tokens"),
|
||||
"orphan-scores": monkDB.get<OrphanScoreDocument>("orphan-scores"),
|
||||
"import-locks": monkDB.get<{ userID: integer; locked: boolean }>("import-locks"),
|
||||
"import-locks": monkDB.get<{ userID: integer; locked: boolean; lockedAt: integer | null }>(
|
||||
"import-locks"
|
||||
),
|
||||
tables: monkDB.get<TableDocument>("tables"),
|
||||
"invite-locks": monkDB.get<{ userID: integer; locked: boolean }>("invite-locks"),
|
||||
"game-settings": monkDB.get<UGPTSettingsDocument>("game-settings"),
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import UserFollowersMigration from "./migrations/add-following-to-users";
|
||||
import AddLockedAt from "./migrations/add-lockedat";
|
||||
import UGPTAddPreferredRanking from "./migrations/add-preferredRanking-to-ugpt";
|
||||
import UGPTRivalsMigration from "./migrations/add-rivals-to-ugpt";
|
||||
import FixUndefinedBMSData from "./migrations/fix-undefined-bms-data";
|
||||
@@ -50,6 +51,7 @@ const REGISTERED_MIGRATIONS: Array<Migration> =
|
||||
SessionsToScoreIDs,
|
||||
V3PropsMigration,
|
||||
V3ScoresMigration,
|
||||
AddLockedAt,
|
||||
];
|
||||
|
||||
// only apply type-specific migrations if we're not in testing
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
import db from "external/mongo/db";
|
||||
import type { Migration } from "utils/types";
|
||||
|
||||
const migration: Migration = {
|
||||
id: "add-lockedat",
|
||||
up: async () => {
|
||||
await db["import-locks"].update(
|
||||
{},
|
||||
{
|
||||
$set: {
|
||||
lockedAt: null,
|
||||
},
|
||||
},
|
||||
{ multi: true }
|
||||
);
|
||||
},
|
||||
down: async () => {
|
||||
await db["import-locks"].update(
|
||||
{},
|
||||
{
|
||||
$unset: {
|
||||
lockedAt: 1,
|
||||
},
|
||||
},
|
||||
{ multi: true }
|
||||
);
|
||||
},
|
||||
};
|
||||
|
||||
export default migration;
|
||||
@@ -1,6 +1,10 @@
|
||||
import db from "external/mongo/db";
|
||||
import { ONE_HOUR } from "lib/constants/time";
|
||||
import CreateLogCtx from "lib/logger/logger";
|
||||
import type { integer } from "tachi-common";
|
||||
|
||||
const logger = CreateLogCtx(__filename);
|
||||
|
||||
/**
|
||||
* If a user has no ongoing import, enable the import lock and return true.
|
||||
* If a user has an ongoing import, return false.
|
||||
@@ -18,6 +22,7 @@ export async function CheckAndSetOngoingImportLock(userID: integer) {
|
||||
await db["import-locks"].insert({
|
||||
userID,
|
||||
locked: false,
|
||||
lockedAt: null,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -27,10 +32,23 @@ export async function CheckAndSetOngoingImportLock(userID: integer) {
|
||||
locked: false,
|
||||
},
|
||||
{
|
||||
$set: { locked: true },
|
||||
$set: { locked: true, lockedAt: Date.now() },
|
||||
}
|
||||
);
|
||||
|
||||
if (!lockWasSet) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (lockWasSet.lockedAt !== null) {
|
||||
if (Date.now() - lockWasSet.lockedAt > ONE_HOUR) {
|
||||
logger.error(
|
||||
`User ${userID} has been locked for an hour. Automatically freeing the lock as they're stuck.`
|
||||
);
|
||||
await UnsetOngoingImportLock(userID);
|
||||
}
|
||||
}
|
||||
|
||||
return !lockWasSet;
|
||||
}
|
||||
|
||||
@@ -44,7 +62,7 @@ export function UnsetOngoingImportLock(userID: integer) {
|
||||
locked: true,
|
||||
},
|
||||
{
|
||||
$set: { locked: false },
|
||||
$set: { locked: false, lockedAt: null },
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user