1 year ago
#360805
SAGY
How to ensure daily email is only sent once over multiple instances in Django app?
I am trying to scheduling a job/CRON job, on Django app that uses APScheduler to send out a daily email at a scheduled time morning 9 AM. And We have 5 instances/pods in production.
The problem is each day I am getting 5 email and I suspecting its due to number of instance/pod. Cron is running on all instance/pod.
How to prevent the daily email from being sent out by all instances.
from apscheduler.schedulers.background import BackgroundScheduler
def cron_job():
scheduler = BackgroundScheduler()
scheduler.add_job(send_daily_emails, "cron", day_of_week='fri', hour=8, minute=0,
id="send_daily_emails_trigger", replace_existing=True)
scheduler.start()
Python version - 3.5+ Django Version - 2.2.3 APScheduler - 3.9.1
So this is run when my app initializes--this creates a background scheduler that runs the send_daily_emails function at 9AM each morning. The send_daily_emails function is exactly that--all it does is send email to couple of folks. My problem is that there are five instances of the app running, five separate background schedulers will be created and thus the emails will be sent five times each day instead of once. How to send only one email or stop other emails to go.
Thanks in Advance :)
python-3.x
django
cron
apscheduler
0 Answers
Your Answer