Cron Job使用AWS Lambda在Serverless中激发两次

时间:2018-05-27 11:42:10

标签: cron aws-lambda serverless

我正在运行一个Cron工作,每周日上午10:00 UTC运行

它将文本消息推送到电报组

这是我的full code

以下相关部分 -

serverless.yml

service: lessons-of-hn-telegram-bot

provider:
  name: aws
  runtime: nodejs8.10
  environment:
    BOT_API_KEY: ${file(./env.json):BOT_API_KEY}
    CHANNEL_ID: ${file(./env.json):CHANNEL_ID}

functions:
  cron:
    handler: handler.run
    description: Cron job that runs every Sunday at 10 am UTC
    events:
      - schedule: cron(0 10 ? * SUN *)

handler.js

const axios = require("axios");

const { lessonsOfHN } = require("./src/lessonsOfHN");

exports.run = async (event, context, callback) => {
  const time = new Date();
  console.log(`Your cron function "${context.functionName}" ran at ${time}`);

  const lesson = await lessonsOfHN();
  const ENDPOINT = `https://api.telegram.org/bot${
    process.env.BOT_API_KEY
  }/sendMessage`;

  await axios({
    method: "get",
    url: ENDPOINT,
    data: {
      chat_id: process.env.CHANNEL_ID,
      parse_mode: "markdown",
      disable_web_page_preview: true,
      text: lesson
    }
  });

  callback(null, { lesson, success: true });
};

代码中没有任何地方向Telegram发送两次请求,即axios只写一次

我得到的日志说cronjob在10:00:09 am&在10:01:08 am

我正在使用AWS Lambda的无服务器框架

如何确保它只运行一次?由于人们在电报组中收到2条消息,因此:(

0 个答案:

没有答案