Skip to content

Commit

Permalink
discord로 결과 기록하기
Browse files Browse the repository at this point in the history
dynamodb로 데이터 남기는거 지울까하다가 dynamodb 작동 테스트용으로 남겨둠
ayane는 삽질용 프로젝트로 쓰일 예정
  • Loading branch information
if1live committed Oct 2, 2023
1 parent 87345fb commit e903763
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 66 deletions.
2 changes: 1 addition & 1 deletion .env.localhost
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
NODE_ENV=development
REDIS_URL="redis://127.0.0.1:6379/0"
DISCORD_WEBHOOK_URL="TODO:discord-webhook"

AYANE_PLANETSCALE_LABEL="planetscale"
AYANE_PLANETSCALE_TYPE="mysql"
Expand Down
1 change: 1 addition & 0 deletions serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ provider:

STAGE: ${self:provider.stage}
REDIS_URL: ${env:REDIS_URL}
DISCORD_WEBHOOK_URL: ${env:DISCORD_WEBHOOK_URL}

AYANE_PLANETSCALE_LABEL: ${env:AYANE_PLANETSCALE_LABEL}
AYANE_PLANETSCALE_TYPE: ${env:AYANE_PLANETSCALE_TYPE}
Expand Down
37 changes: 36 additions & 1 deletion src/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
RedisNativeInput,
UpstashRedisInput,
} from "./types.js";
import { providerInputs } from "./settings.js";
import { providerInputs, DISCORD_WEBHOOK_URL } from "./settings.js";
import { dynamodb } from "./instances.js";

const execute_mysql = async (input: MysqlInput) => {
Expand Down Expand Up @@ -71,5 +71,40 @@ export const touch = async () => {
console.log(label, result.status, reason);
}
}

// discord 기록은 실패할지 모르니까 마지막에 배치
{
const blocks = entries.map((entry) => {
const { label, result } = entry;
const ok = result.status === "fulfilled" ? "ok" : "error";
const line_header = `## ${label}: ${ok}`;
const line_detail =
result.status === "fulfilled"
? "```" + JSON.stringify(result.value, null, 2) + "```"
: "```" + JSON.stringify(result.reason, null, 2) + "```";

const block = [line_header, line_detail].join("\n");
return block;
});
const text = blocks.join("\n\n");
await sendMessageToDiscord(text);
}

return entries;
};

export const sendMessageToDiscord = async (text: string) => {
const url = DISCORD_WEBHOOK_URL;
if (!url) {
return { ok: false, reason: "no webhook url" };
}

const resp = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ content: text }),
});
return { ok: true };
};
4 changes: 2 additions & 2 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ dotenv.config({ path: envpath });
export const NODE_ENV = process.env.NODE_ENV || "production";
export const STAGE = process.env.STAGE || "dev";

// TODO: 데이터 저장 목적
export const REDIS_URL = process.env.REDIS_URL!;
// 데이터 저장을 꼭 해야되나? 로그니까 디스코드에 남겨도 될거같은데
export const DISCORD_WEBHOOK_URL = process.env.DISCORD_WEBHOOK_URL;

// https://blog.logrocket.com/alternatives-dirname-node-js-es-modules/
const filename = url.fileURLToPath(import.meta.url);
Expand Down
8 changes: 8 additions & 0 deletions test/simple.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { describe, it, assert } from "vitest";

// TODO: dynamodb 기반으로 바꾸면 기존 테스트가 쓸모없어진다.
describe("blank", () => {
it("blank", () => {
assert.equal(1, 1);
});
});
62 changes: 0 additions & 62 deletions test/stores.test.ts

This file was deleted.

0 comments on commit e903763

Please sign in to comment.