django cron 计划任务
2024年9月25日大约 1 分钟约 331 字
project_name/cron_tasks.py
"""cron 计划任务
docker开启命令:
RUN echo "* * * * * root python ${PROJECT_PATH}/manage.py shell -c 'import cron_tasks; cron_tasks.main_tasks()' >> /proc/1/fd/1 2>&1" >> /etc/crontab
"""
import datetime
def minutely_tasks():
"""每分钟执行一次"""
pass
def five_minutely_tasks():
"""每5分钟执行一次"""
pass
def twenty_minutely_tasks():
"""每20分钟执行一次"""
pass
def hourly_tasks():
"""每小时执行一次"""
pass
def eight_hourly_tasks():
"""每8小时执行一次"""
pass
def daily_tasks():
"""每天执行一次"""
pass
def weekly_tasks():
"""每周执行一次"""
pass
def monthly_tasks():
"""每月执行一次"""
pass
def quarterly_tasks():
"""每个季度执行一次"""
pass
def yearly_tasks():
"""每年执行一次"""
pass
def main_tasks():
current_time = datetime.datetime.now()
# 执行每分钟的任务
minutely_tasks()
if current_time.minute % 5 == 0: # 每五分钟执行(5的倍数)
five_minutely_tasks()
if current_time.minute % 20 == 0: # 每20分钟执行(20的倍数)
twenty_minutely_tasks()
if current_time.minute == 0: # 每小时执行(每小时的第一分钟)
hourly_tasks()
if current_time.hour % 8 == 0 and current_time.minute == 0: # 每8小时执行(8的倍数)
eight_hourly_tasks()
if current_time.hour == 0 and current_time.minute == 0: # 每天的第一分钟执行
daily_tasks()
if current_time.weekday() == 0 and current_time.hour == 0 and current_time.minute == 0: # 每周的第一天开始时间(周一)
weekly_tasks()
if current_time.day == 1 and current_time.hour == 0 and current_time.minute == 0: # 每月的第一天
monthly_tasks()
if current_time.month in [1, 4, 7, 10] and current_time.day == 1 and current_time.hour == 0 and current_time.minute == 0: # 每个季度的第一天
quarterly_tasks()
if current_time.month == 1 and current_time.day == 1 and current_time.hour == 0 and current_time.minute == 0: # 每年的第一分钟
yearly_tasks()