Skip to content
This repository has been archived by the owner on Oct 5, 2023. It is now read-only.

Commit

Permalink
chore: clusterを使うように
Browse files Browse the repository at this point in the history
  • Loading branch information
yupix committed Sep 30, 2023
1 parent 31661bb commit 1be5453
Showing 1 changed file with 28 additions and 20 deletions.
48 changes: 28 additions & 20 deletions src/migration/noteWorker.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,42 @@
import { initDb } from "@/db/postgre";
import { logger } from "./common";
import {
noteQueue,
} from "./jobqueue";
import { noteQueue } from "./jobqueue";
import noteProcessor from "./processor/note.processor";
import { createConnection } from "typeorm";
import config from "@/config";
import { AyuskeyNextEntities } from "@/v13/models";
import cluster from "cluster";

async function main() {
await initDb();
await createConnection({
name: "nextDb",
type: "postgres",
host: config.db.nextDb.host,
port: config.db.nextDb.port,
username: config.db.nextDb.user,
password: config.db.nextDb.pass,
database: config.db.nextDb.db,
entities: AyuskeyNextEntities,
});
const numWorkers = 8;

noteQueue.process(noteProcessor);


noteQueue.on("completed", (job) => {
logger.succ(`Note: ${job.data.id} の処理が完了しました`);
});
if (cluster.isPrimary) {
for (let i = 0; i < numWorkers; i++) {
cluster.fork();
}
cluster.on("exit", (worker, code, signal) => {
console.log(`worker ${worker.process.pid} died`);
});
} else {
await initDb();
await createConnection({
name: "nextDb",
type: "postgres",
host: config.db.nextDb.host,
port: config.db.nextDb.port,
username: config.db.nextDb.user,
password: config.db.nextDb.pass,
database: config.db.nextDb.db,
entities: AyuskeyNextEntities,
});

noteQueue.process(noteProcessor);
noteQueue.on("completed", (job) => {
logger.succ(`Note: ${job.data.id} の処理が完了しました`);
});
}
}

main().catch((e) => {
console.warn(e);
process.exit(1);
Expand Down

0 comments on commit 1be5453

Please sign in to comment.