如何确保每日电子邮件仅在Django应用中的多个实例中发送一次?

发布于 2025-01-18 02:53:15 字数 771 浏览 0 评论 0原文

我正在尝试在Django应用程序上安排工作/CRON工作,该应用程序使用Apscheduler在预定的时间早上9点在上午9点发送每日电子邮件。我们有5个实例/豆荚的生产。

问题是我每天收到5封电子邮件,并且我怀疑它是由于实例/POD的数量。 Cron在所有实例/POD上运行。

如何防止所有实例发送每日电子邮件。

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版本-3.5+ Django版本-2.2.3 apscheduler-3.9.1

因此,这是在我的应用程序初始化时运行的 - 正在创建一个背景调度程序,该调度程序每天早上9点运行send_daily_emails功能。 send_daily_emails函数正是 - 这一切都是向几个人发送电子邮件。我的问题是,该应用程序运行五个实例,将创建五个单独的背景调度程序,因此每天将发送五次电子邮件而不是一次。如何仅发送一封电子邮件或停止其他电子邮件。

提前致谢 :)

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 :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文