fix: 1 dispatchRegularSnapshots working at a time

This commit is contained in:
alikia2x (寒寒) 2025-03-24 01:06:48 +08:00
parent 9060d28823
commit 723b6090c4
Signed by: alikia2x
GPG Key ID: 56209E0CCD8420C6
2 changed files with 7 additions and 0 deletions

View File

@ -20,6 +20,7 @@ import { insertVideoSnapshot } from "lib/mq/task/getVideoStats.ts";
import { NetSchedulerError } from "lib/mq/scheduler.ts"; import { NetSchedulerError } from "lib/mq/scheduler.ts";
import { setBiliVideoStatus } from "lib/db/allData.ts"; import { setBiliVideoStatus } from "lib/db/allData.ts";
import { truncate } from "lib/utils/truncate.ts"; import { truncate } from "lib/utils/truncate.ts";
import { lockManager } from "lib/mq/lockManager.ts";
const priorityMap: { [key: string]: number } = { const priorityMap: { [key: string]: number } = {
"milestone": 1, "milestone": 1,
@ -132,6 +133,7 @@ export const collectMilestoneSnapshotsWorker = async (_job: Job) => {
export const regularSnapshotsWorker = async (_job: Job) => { export const regularSnapshotsWorker = async (_job: Job) => {
const client = await db.connect(); const client = await db.connect();
const startedAt = Date.now(); const startedAt = Date.now();
await lockManager.acquireLock("dispatchRegularSnapshots");
try { try {
const aids = await getVideosWithoutActiveSnapshotSchedule(client); const aids = await getVideosWithoutActiveSnapshotSchedule(client);
for (const rawAid of aids) { for (const rawAid of aids) {
@ -148,6 +150,7 @@ export const regularSnapshotsWorker = async (_job: Job) => {
} catch (e) { } catch (e) {
logger.error(e as Error, "mq", "fn:regularSnapshotsWorker"); logger.error(e as Error, "mq", "fn:regularSnapshotsWorker");
} finally { } finally {
lockManager.releaseLock("dispatchRegularSnapshots");
client.release(); client.release();
} }
}; };

View File

@ -91,3 +91,7 @@ snapshotWorker.on("error", (err) => {
const e = err as WorkerError; const e = err as WorkerError;
logger.error(e.rawError, e.service, e.codePath); logger.error(e.rawError, e.service, e.codePath);
}); });
snapshotWorker.on("closed", async () => {
await lockManager.releaseLock("dispatchRegularSnapshots");
})