为什么评论仍处于待处理状态?



我为一个小型博客应用程序开发了一个微服务应用程序。它有邮政服务、评论服务、查询服务和审核服务。

审核服务负责审核和过滤某些评论并批准其他评论:

const express = require("express");
const bodyParser = require("body-parser");
const axios = require("axios");
const app = express();
app.use(bodyParser.json());
app.post("/events", async (req, res) => {
const { type, data } = req.body;
if (type === "CommentCreated") {
const status = /cdc/gi.test(data.content) ? "rejected" : "approved";
await axios.post("http://localhost:4005/events", {
type: "CommentModerated",
data: {
id: data.id,
postId: data.postId,
status,
content: data.content,
},
});
}
res.send({});
});
app.listen(4003, () => {
console.log("Listening on 4003");
});

在一些手动测试期间,我关闭了查询服务并创建了一个带有评论的帖子,期望一旦查询服务重新上线,它将接收从事件总线创建的帖子和评论的事件,并且用户将能够看到审核的评论,即那些被批准和拒绝的评论, 但我相信我在应用程序中引入了一个错误,方法是在评论服务中将评论的状态默认为待处理,如下所示:

const express = require("express");
const bodyParser = require("body-parser");
const { randomBytes } = require("crypto");
const cors = require("cors");
const axios = require("axios");
const app = express();
app.use(bodyParser.json());
app.use(cors());
const commentsByPostId = {};
app.get("/posts/:id/comments", (req, res) => {
res.send(commentsByPostId[req.params.id] || []);
});
app.post("/posts/:id/comments", async (req, res) => {
const commentId = randomBytes(4).toString("hex");
const { content } = req.body;
const comments = commentsByPostId[req.params.id] || [];
comments.push({ id: commentId, content, status: "pending" });
commentsByPostId[req.params.id] = comments;
await axios.post("http://localhost:4005/events", {
type: "CommentCreated",
data: { id: commentId, content, postId: req.params.id, status: "pending" },
});
res.status(201).send(comments);
});
app.post("/events", async (req, res) => {
console.log("Event Received:", req.body.type);
const { type, data } = req.body;
if (type === "CommentModerated") {
const { id, postId, status, content } = data;
const comments = commentsByPostId[postId];
const comment = comments.find((comment) => {
return comment.id === id;
});
comment.status = status;
await axios.post("http://localhost:4005/events", {
type: "CommentUpdated",
data: {
id,
content,
postId,
status,
},
});
}
res.send({});
});

我不清楚如何摆脱我自己画的这个角落,使status: 'pending'成为默认行为,因为我没想到当微服务从崩溃或其他什么重新上线时它会停留在这个默认行为中。

问题出在我的事件总线上,因为我之前在我的事件总线终端中不断收到未处理的承诺拒绝,我认为我必须在那里添加async/await语法,如下所示:

app.post("/events", async (req, res) => {
const event = req.body;
events.push(event);
await axios.post("http://localhost:4000/events", event);
await axios.post("http://localhost:4001/events", event);
await axios.post("http://localhost:4002/events", event);
await axios.post("http://localhost:4003/events", event);
res.send({ status: "OK" });
});

但最终我意识到我在我的事件巴士总站中仍然收到未经处理的承诺拒绝,它不会影响其他任何事情,最初我不认为上述操作一开始就是异步操作。

因此,在仔细研究了包括前端代码库在内的所有代码之后,我决定回到事件总线并删除async/await语法,果然,当查询服务关闭时,我能够提交注释,当查询服务重新上线时,它不仅收到CommentCreated事件,还收到CommentModeratedCommentUpdated事件。

所以这是它应该的样子:

app.post("/events", (req, res) => {
const event = req.body;
events.push(event);
axios.post("http://localhost:4000/events", event);
axios.post("http://localhost:4001/events", event);
axios.post("http://localhost:4002/events", event);
axios.post("http://localhost:4003/events", event);
res.send({ status: "OK" });
});

最新更新