Warm tip: This article is reproduced from serverfault.com, please click

How to make a schedule report feature using nodejs cron job ? (problem with aws autoscaling)

发布于 2020-12-04 16:57:09

I am working on "schedule report" feature but facing a problem because of AWS auto scaling feature.

This is how I am executing this feature:

Step 1: User will provide "hour", "minutes" and "report daily or not flag" as input to backend.

Step 2: Backend will make a cron job, store "cron job id" in database and send reports to user via mail according to cron job schedule.

PROBLEM: If the server shuts down and then restarts; then according to start-up script all cron jobs will be started... now if my server has heavy load then AWS auto-scaling will start and on each EC2 instance start-up script will be executed and the same cron-job will be running on different instances.

Therefore same cron job will be running on different EC2 instances and hence user will receive the same emails multiple times (this is the error iam facing, how to solve this issue)

What I have thought till now: We can store a flag in database , if flag is true then cron job will start else will not start. So if one ec2 instance is running cron job, it will set flag to true then other instances won't start it. But the issue is that how to unset the flag if serve suddenly shuts down or such other case.

I am not able to find answer to such a architectural design based issue on net. Is the above approach practical; if then how to properly do it? What is the best way to implement these kind of architectures? Any help will be great, Thanks!

Questioner
Vinit Narayan Jha
Viewed
0
user13789881 2020-12-07 15:13:35

I have faced the same issue. You can follow 2 ways:

  1. You can run a separate ec2 instance (with vertical scaling) dedicated for cron job.
  2. you can store instance id and cron job status to match running cron job instance