Cron Job fires twice in Serverless using AWS Lambda

大城市里の小女人 提交于 2020-01-24 12:33:53

问题


I am running a Cron Job which runs every Sunday at 10:00am UTC

It pushes a Text Message to a Telegram Group

Here's my full code

Relevant parts below -

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 });
};

Nowhere in the code a request is sent to Telegram twice ,i.e, axios is only written once

The logs I got says the cronjob ran twice once at 10:00:09 am & at 10:01:08 am

I am using Serverless Framework with AWS Lambda

How to make sure it only runs once? Since people get 2 messages in the telegram group because of this :(

来源:https://stackoverflow.com/questions/50551768/cron-job-fires-twice-in-serverless-using-aws-lambda

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!