当可能有多个实例时,如何确保crontab命令在AWS上运行一次?

问题描述

我有一个使用Elastic Beanstalk在AWS上运行的Django项目。它可以运行1到6个实例。

我加载了一个crontab文件,以便在一夜之间运行一些management/commands。我有这个config文件:

container_commands:
  01_cron_job:
    command: "cp .ebextensions/crontab.txt /etc/cron.d/my_cron_jobs && chmod 644 /etc/cron.d/my_cron_jobs"
    #leader_only: true

复制的文件如下:

# Set the cron to run with utf8 encoding
PYTHONIOENCODING=utf8
   
# Specify where to send email 
MAILTO="[email protected]"

1 0 * * * root source /opt/python/current/env && nice /opt/python/current/app/src/manage.py clearsessions
15 0 * * * root source /opt/python/current/env && nice /opt/python/current/app/src/manage.py update_summary_stats >> /opt/python/log/update_summary_stats.log 2>&1
# this file needs a blank space as the last line otherwise it will fail

config文件中,如果我将leader_only设置为false,那么如果领导者实例在某个时候被删除(例如,因为在高峰时间添加了另一个实例,并且领导者静下来后就删除了)。如果将leader_only设置为true,则crontab命令将在每个实例上运行。

在AWS Elastic Beanstalk上将crontab设置为仅运行一次而与实例数量无关的最佳方法是什么?谢谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)