问题
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