Skip to content

Commit

Permalink
Merge pull request #54 from sora-vp/fix-processor-log-placement
Browse files Browse the repository at this point in the history
Memperbaiki penempatan posisi log file
  • Loading branch information
reacto11mecha authored Jul 15, 2024
2 parents ed5c1c5 + f76af02 commit d0d9561
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 31 deletions.
14 changes: 13 additions & 1 deletion apps/processor/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,16 @@ COPY --from=builder --chown=processor:nodejs /vote-processor /vote-processor

WORKDIR /vote-processor

CMD ["node", "node_modules/@sora-vp/processor/dist/index.js"]
COPY <<EOF /vote-processor/index.js
import path from "path";
import { fileURLToPath } from "url";

import { consumeMessagesFromQueue } from "@sora-vp/processor";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

void consumeMessagesFromQueue(__dirname);
EOF

CMD ["node", "index.js"]
2 changes: 1 addition & 1 deletion apps/processor/eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import baseConfig from "@sora-vp/eslint-config/base";
/** @type {import('typescript-eslint').Config} */
export default [
{
ignores: [".dist/**", "tsup.config.ts"],
ignores: ["dist/**", "index.ts", "tsup.config.ts"],
},
...baseConfig,
];
9 changes: 9 additions & 0 deletions apps/processor/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import path from "path";
import { fileURLToPath } from "url";

import { consumeMessagesFromQueue } from "./src/index";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

void consumeMessagesFromQueue(__dirname);
2 changes: 1 addition & 1 deletion apps/processor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"scripts": {
"build": "tsup",
"clean": "rm -rf .turbo node_modules",
"dev": "yarn with-env tsx watch src/index.ts",
"dev": "yarn with-env tsx watch ./index.ts",
"lint": "eslint",
"format": "prettier --check . --ignore-path ../../.gitignore",
"typecheck": "tsc --noEmit",
Expand Down
8 changes: 4 additions & 4 deletions apps/processor/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ import { validateId } from "@sora-vp/id-generator";
import { api } from "./api";
import { db, schema } from "./db";
import { env } from "./env";
import { logger } from "./logger";
import { initLogger } from "./logger";
import { canVoteNow } from "./utils";

const inputValidator = z.object({
id: z.number().positive(),
qrId: z.string().refine(validateId),
});

const consumeMessagesFromQueue = async () => {
export const consumeMessagesFromQueue = async (loggerDirectory: string) => {
const logger = initLogger(loggerDirectory);

try {
logger.debug(`[MQ] MQ AMQP: ${env.AMQP_URL}`);
logger.debug(`[TRPC] TRPC URL: ${env.PROCESSOR_API_URL}`);
Expand Down Expand Up @@ -226,5 +228,3 @@ const consumeMessagesFromQueue = async () => {
logger.error(error);
}
};

void consumeMessagesFromQueue();
45 changes: 21 additions & 24 deletions apps/processor/src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
import path from "path";
import { fileURLToPath } from "url";
import pino from "pino";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

export const logger = pino({
transport: {
targets: [
{
target: "pino-pretty",
level: "debug",
options: {
colorize: true,
ignore: "pid,hostname",
translateTime: "SYS:standard",
export const initLogger = (destinationDirectoryPath: string) =>
pino({
transport: {
targets: [
{
target: "pino-pretty",
level: "debug",
options: {
colorize: true,
ignore: "pid,hostname",
translateTime: "SYS:standard",
},
},
},
{
target: "pino/file",
level: "debug",
options: {
destination: path.join(__dirname, "..", "processor.log"),
{
target: "pino/file",
level: "debug",
options: {
destination: path.join(destinationDirectoryPath, "processor.log"),
},
},
},
],
},
});
],
},
});

0 comments on commit d0d9561

Please sign in to comment.