diff --git a/.env.localhost b/.env.localhost index 866fcf3..1796b01 100644 --- a/.env.localhost +++ b/.env.localhost @@ -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" diff --git a/serverless.yml b/serverless.yml index 758545c..62e327a 100644 --- a/serverless.yml +++ b/serverless.yml @@ -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} diff --git a/src/services.ts b/src/services.ts index 10abb4a..3bdbb17 100644 --- a/src/services.ts +++ b/src/services.ts @@ -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) => { @@ -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 }; +}; diff --git a/src/settings.ts b/src/settings.ts index b216b45..ca99c5d 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -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); diff --git a/test/simple.test.ts b/test/simple.test.ts new file mode 100644 index 0000000..d316e54 --- /dev/null +++ b/test/simple.test.ts @@ -0,0 +1,8 @@ +import { describe, it, assert } from "vitest"; + +// TODO: dynamodb 기반으로 바꾸면 기존 테스트가 쓸모없어진다. +describe("blank", () => { + it("blank", () => { + assert.equal(1, 1); + }); +}); diff --git a/test/stores.test.ts b/test/stores.test.ts deleted file mode 100644 index 69134d8..0000000 --- a/test/stores.test.ts +++ /dev/null @@ -1,62 +0,0 @@ -import { describe, it, assert } from "vitest"; - -// TODO: dynamodb 기반으로 바꾸면 기존 테스트가 쓸모없어진다. -describe("blank", () => { - it("blank", () => { - assert.equal(1, 1); - }); -}); - -/* -import type { Redis } from "ioredis"; -import { default as RedisMock } from "ioredis-mock"; -import { loadResults, saveResult } from "../src/stores.js"; - -function createRedisMock(): Redis { - const redis = new (RedisMock as any)(); - return redis as Redis; -} - -describe("store", () => { - const redis = createRedisMock(); - - it("loadResults: empty", async () => { - const results = await loadResults(redis); - assert.equal(results.length, 0); - }); - - it("scenario: ignore", async () => { - await saveResult(redis, "not-supported-service", { - status: "fulfilled", - value: false, - at: new Date(), - }); - - const results = await loadResults(redis); - assert.equal(results.length, 0); - }); - - it("scenario: ok", async () => { - const label = "foo"; - const value = { hello: "world" }; - - await saveResult(redis, label, { - status: "fulfilled", - value, - at: new Date(), - }); - - const results = await loadResults(redis); - assert.equal(results.length, 1); - - const [result] = results; - assert.equal(result?.label, label); - - const health = result?.health; - if (health?.tag !== "ok") { - assert.fail(); - } - assert.deepEqual(health.value, value); - }); -}); -*/