ref: wrap some worker functions with withDbConnection

This commit is contained in:
alikia2x (寒寒) 2025-04-13 05:06:52 +08:00
parent 4ee4d2ede9
commit a9582722f4
Signed by: alikia2x
GPG Key ID: 56209E0CCD8420C6
6 changed files with 52 additions and 58 deletions

View File

@ -1,12 +1,9 @@
import { Job } from "npm:bullmq@5.45.2";
import { db } from "db/init.ts";
import { collectSongs } from "mq/task/collectSongs.ts";
import { withDbConnection } from "db/withConnection.ts";
import { Client } from "https://deno.land/x/postgres@v0.19.3/mod.ts";
export const collectSongsWorker = async (_job: Job): Promise<void> => {
const client = await db.connect();
try {
export const collectSongsWorker = (_job: Job): Promise<void> =>
withDbConnection(async (client: Client) => {
await collectSongs(client);
} finally {
client.release();
}
};
});

View File

@ -1,3 +1,4 @@
export * from "mq/exec/getLatestVideos.ts";
export * from "./getVideoInfo.ts";
export * from "./collectSongs.ts";
export * from "./takeBulkSnapshot.ts";

View File

@ -1,12 +1,10 @@
import { Job } from "bullmq";
import { queueLatestVideos } from "mq/task/queueLatestVideo.ts";
import { db } from "db/init.ts";
import { withDbConnection } from "db/withConnection.ts";
import { Client } from "https://deno.land/x/postgres@v0.19.3/mod.ts";
export const getLatestVideosWorker = async (_job: Job): Promise<void> => {
const client = await db.connect();
try {
await queueLatestVideos(client);
} finally {
client.release();
}
};
export const getLatestVideosWorker = (_job: Job): Promise<void> =>
withDbConnection(async (client: Client) => {
await queueLatestVideos(client)
});

View File

@ -1,17 +1,15 @@
import { Job } from "npm:bullmq@5.45.2";
import { db } from "db/init.ts";
import { insertVideoInfo } from "mq/task/getVideoDetails.ts";
import { withDbConnection } from "db/withConnection.ts";
import { Client } from "https://deno.land/x/postgres@v0.19.3/mod.ts";
import logger from "log/logger.ts";
export const getVideoInfoWorker = async (job: Job): Promise<number> => {
const client = await db.connect();
try {
export const getVideoInfoWorker = async (job: Job): Promise<void> =>
await withDbConnection<void>(async (client: Client) => {
const aid = job.data.aid;
if (!aid) {
return 3;
logger.warn("aid does not exists", "mq", "job:getVideoInfo");
return;
}
await insertVideoInfo(client, aid);
return 0;
} finally {
client.release();
}
};
await insertVideoInfo(client, aid)
});

View File

@ -12,7 +12,7 @@ import {
snapshotTickWorker,
takeSnapshotForVideoWorker,
} from "mq/exec/snapshotTick.ts";
import {takeBulkSnapshotForVideosWorker} from "../mq/exec/takeBulkSnapshot.ts";
import {takeBulkSnapshotForVideosWorker} from "mq/exec/executors.ts";
Deno.addSignalListener("SIGINT", async () => {
logger.log("SIGINT Received: Shutting down workers...", "mq");