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 { Job } from "npm:bullmq@5.45.2";
import { db } from "db/init.ts";
import { collectSongs } from "mq/task/collectSongs.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> => { export const collectSongsWorker = (_job: Job): Promise<void> =>
const client = await db.connect(); withDbConnection(async (client: Client) => {
try {
await collectSongs(client); await collectSongs(client);
} finally { });
client.release();
}
};

View File

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

View File

@ -1,12 +1,10 @@
import { Job } from "bullmq"; import { Job } from "bullmq";
import { queueLatestVideos } from "mq/task/queueLatestVideo.ts"; 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(); export const getLatestVideosWorker = (_job: Job): Promise<void> =>
try { withDbConnection(async (client: Client) => {
await queueLatestVideos(client); await queueLatestVideos(client)
} finally { });
client.release();
}
};

View File

@ -1,17 +1,15 @@
import { Job } from "npm:bullmq@5.45.2"; import { Job } from "npm:bullmq@5.45.2";
import { db } from "db/init.ts";
import { insertVideoInfo } from "mq/task/getVideoDetails.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> => { export const getVideoInfoWorker = async (job: Job): Promise<void> =>
const client = await db.connect(); await withDbConnection<void>(async (client: Client) => {
try {
const aid = job.data.aid; const aid = job.data.aid;
if (!aid) { if (!aid) {
return 3; logger.warn("aid does not exists", "mq", "job:getVideoInfo");
return;
} }
await insertVideoInfo(client, aid); await insertVideoInfo(client, aid)
return 0; });
} finally {
client.release();
}
};

View File

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