From a12a658239b19b721bbabbaf841c9afdb7d1bcf8 Mon Sep 17 00:00:00 2001 From: DongHyeonWon Date: Tue, 27 Aug 2024 13:16:05 +0900 Subject: [PATCH] =?UTF-8?q?feat:=201=EC=8B=9C=EA=B0=84=20=EB=A7=88?= =?UTF-8?q?=EB=8B=A4=20=EB=B2=84=EB=B8=94=20=EC=9C=A0=ED=9A=A8=EA=B8=B0?= =?UTF-8?q?=EA=B0=84=20=EC=B2=B4=ED=81=AC=20=ED=9B=84=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/index.js | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/backend/src/index.js b/backend/src/index.js index 2e4428d..ca5484c 100644 --- a/backend/src/index.js +++ b/backend/src/index.js @@ -50,7 +50,7 @@ io.on("connection", async (socket) => { console.log("Received input server: ", data); const id = uuidv4(); const createdAt = new Date(); - const deleteAt = new Date(Date.now() + 10 * 60 * 1000); + const deleteAt = new Date(Date.now() + 12 * 60 * 60 * 1000); await connection.query("INSERT INTO messages (id,text,created_at,delete_at) VALUES (?,?,?,?)", [ id, data, @@ -76,6 +76,32 @@ app.get("/", (req, res) => { res.send("Hello World!"); }); +cron.schedule("*/60 * * * *", async () => { + console.log("60분 마다 만료된 메시지를 삭제합니다."); + try { + const now = new Date(); + // 삭제할 메시지의 id들을 가져옴 + const [rows] = await connection.query("SELECT id FROM messages WHERE delete_at <= ?", [now]); + + // 삭제할 메시지가 있는 경우 + if (rows.length > 0) { + const idsToDelete = rows.map((row) => row.id); + await connection.query("DELETE FROM messages WHERE id IN (?)", [idsToDelete]); + + // 삭제된 메시지들의 id를 클라이언트들에게 알림 + idsToDelete.forEach((id) => { + io.emit("delete", id); + }); + + console.log(`${idsToDelete.length}개의 메시지가 삭제되었습니다.`); + } else { + console.log("삭제할 메시지가 없습니다."); + } + } catch (err) { + console.error("메시지 삭제 중 오류 발생:", err); + } +}); + server.listen(port, async () => { await connectDB(); console.log(`Server is running on http://localhost:${port}`);