为什么评论保持待处理状态?

时间:2020-06-10 04:24:39

标签: express microservices

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

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

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'设为默认行为,因为我不希望当微服务从以下位置恢复在线时,它将停留在默认行为中崩溃或其他任何原因。

1 个答案:

答案 0 :(得分:1)

问题出在我的事件总线上,因为我一直在事件总线终端中不断收到未处理的承诺拒绝,我以为我必须在其中添加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" });
});