DynamoDB 写入不一致

发布于 2025-01-20 10:49:40 字数 2395 浏览 4 评论 0原文

在无服务器框架中,我们将以下代码用作LAMBDA功能,每2分钟用Cron触发。我们面临的问题是,DynamoDB中的写作是不一致的,我们想拥有3篇文章,但是我们每2分钟收到1或2篇著作。 DynamoDB具有HASH键的时间,然后对键进行分类日期和计费模式:配置。是否有人面对DynamoDB的行为或同样的问题来分享他如何so索。谢谢

"use strict";

const AWS = require("aws-sdk");
const axios = require("axios");

const dynamoDb = new AWS.DynamoDB.DocumentClient();

const lambda = new AWS.Lambda({
  region: "us-east-1",
});

module.exports.getWeather = async (event, context, callback) => {
  const openWeatherMapAPIURL = `http://api.openweathermap.org/data/2.5/weather?id=${event}&appid=XXXXXXXXXXXXXXXXXXXXXXX&units=metric`;
  const currentWeather = await axios
    .get(openWeatherMapAPIURL)
    .then((records) => {
      console.log(records);

      const d = new Date(records.headers.date);
      let hour = d.getHours();

      const params = {
        TableName: process.env.DYNAMODB_TABLE_NAME,
        Item: {
          hour: hour,
          date: records.headers.date,
          city: records.data.name,
          temp: records.data.main.temp,
          feelsLike: records.data.main.feels_like,
          description: records.data.weather[0].description,
        },
      };
      setTimeout(function () {
        dynamoDb.put(params, (error) => {
          // handle potential errors
          console.log(`zapis na: ${records.data.name} ${records.headers.date}`);
          if (error) {
            console.log(error);
            console.error(error);
            return;
          }
        });
      }, 3000);
      
    })
    .catch((error) => {
      console.log(error);
      return;
    });
 

  const response = {
    statusCode: 200,
    body: JSON.stringify({
      message: `Weather from ${event} was requested!`,
    }),
  };
  callback(null, response);
};

module.exports.cron_launcher = (event, context, callback) => {
  const requestedID = ["786735", "792578", "785842"];

  requestedID.forEach((requestedID) => {
    const params = {
      FunctionName: process.env.HANDLER_LOCATION + "-getWeather",
      InvocationType: "RequestResponse",
      Payload: JSON.stringify(requestedID),
    };

    return lambda.invoke(params, function (error, data) {
      if (error) {
        console.error(JSON.stringify(error));
        return new Error(`Error printing messages: ${JSON.stringify(error)}`);
      } else if (data) {
        console.log(data);
      }
    });
  });
};

We have the following code used as lambda Function in Serverless Framework triggered every 2min with cron. The issue we are facing is that the writing in DynamoDB is inconsistent , we want to have 3 writings but instead we receive 1 or 2 writings every 2 minutes.
DynamoDB has a HASH key the HOUR and SORT KEY the DATE and Billing mode: PROVISIONED. Has someone faced the same behavior from DynamoDB or the same issue to share how he sovled it. Thanks

"use strict";

const AWS = require("aws-sdk");
const axios = require("axios");

const dynamoDb = new AWS.DynamoDB.DocumentClient();

const lambda = new AWS.Lambda({
  region: "us-east-1",
});

module.exports.getWeather = async (event, context, callback) => {
  const openWeatherMapAPIURL = `http://api.openweathermap.org/data/2.5/weather?id=${event}&appid=XXXXXXXXXXXXXXXXXXXXXXX&units=metric`;
  const currentWeather = await axios
    .get(openWeatherMapAPIURL)
    .then((records) => {
      console.log(records);

      const d = new Date(records.headers.date);
      let hour = d.getHours();

      const params = {
        TableName: process.env.DYNAMODB_TABLE_NAME,
        Item: {
          hour: hour,
          date: records.headers.date,
          city: records.data.name,
          temp: records.data.main.temp,
          feelsLike: records.data.main.feels_like,
          description: records.data.weather[0].description,
        },
      };
      setTimeout(function () {
        dynamoDb.put(params, (error) => {
          // handle potential errors
          console.log(`zapis na: ${records.data.name} ${records.headers.date}`);
          if (error) {
            console.log(error);
            console.error(error);
            return;
          }
        });
      }, 3000);
      
    })
    .catch((error) => {
      console.log(error);
      return;
    });
 

  const response = {
    statusCode: 200,
    body: JSON.stringify({
      message: `Weather from ${event} was requested!`,
    }),
  };
  callback(null, response);
};

module.exports.cron_launcher = (event, context, callback) => {
  const requestedID = ["786735", "792578", "785842"];

  requestedID.forEach((requestedID) => {
    const params = {
      FunctionName: process.env.HANDLER_LOCATION + "-getWeather",
      InvocationType: "RequestResponse",
      Payload: JSON.stringify(requestedID),
    };

    return lambda.invoke(params, function (error, data) {
      if (error) {
        console.error(JSON.stringify(error));
        return new Error(`Error printing messages: ${JSON.stringify(error)}`);
      } else if (data) {
        console.log(data);
      }
    });
  });
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

辞慾 2025-01-27 10:49:40

您不在等待DynamoDB.put操作完成。此外,您正在将呼叫包装在settimeout中。在进行网络操作之前​​,您的lambda功能正在返回。在返回Lambda的结果之前,请确保PUT操作成功。

  1. 我认为您没有理由在此处使用Settimeout。
  2. 您可以调用dynamoDB.put(...)。promise()以从DynamoDB SDK中获得承诺并等待该承诺。
    2.a,或者您可以继续使用回调,但将整个代码的整个部分包裹在新的Promise对象中,在DynamoDB之后调用Resolve方法。输入调用完成。

You are not waiting for the dynamodb.put operation to finish. Additionally, you are wrapping the call in a setTimeout. Your lambda function is returning before the network operation can be made. Make sure the put operation succeeds before returning a result from your lambda.

  1. I see no reason for you to use a setTimeout here.
  2. You can call dynamodb.put(...).promise() to get a promise from the dynamodb SDK and await that promise.
    2.a Or you can continue using a callback, but wrap the entire section of code in a new promise object, calling the resolve method after the dynamodb.put call finishes.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文