1、動態配置:上一篇分享過了,貼個鏈接:CSDNhttps://mp.csdn.net/mp_blog/creation/editor/122606790
2、linux的crontab定時任務 :
CSDNhttps://mp.csdn.net/mp_blog/creation/editor/1207335073、任務sh腳本:
#!/bin/sh
# 進入當前腳本文件夾,確保django導包初始目錄,pwd
cd `dirname $0`
# 虛擬環境的python相對路徑,當前pwd
venv/bin/python -u ticket/auto_ticket.py > logs/auto_ticket.log 2>&1 &
4、任務具體內容.py腳本
import re
import sys
import django
import os
sys.path.extend([os.getcwd()])
# 因為shell腳本裡面不能讀取到系統變量,采用遍歷匹配方法查找配置
env_for_dynaconf = 'DEFAULT'
with open('/root/.bashrc') as file_obj:
for file_row in file_obj:
if re.match(r'^export ENV_FOR_DYNACONF=DEVELOPMENT', file_row):
env_for_dynaconf = 'DEVELOPMENT'
os.environ.setdefault('ENV_FOR_DYNACONF', env_for_dynaconf)
os.environ['DJANGO_SETTINGS_MODULE'] = 'book_store_miniapp_backend.settings'
django.setup()
# print(os.environ)
# print(os.environ.get('TEST'))
from ticket.models import TicketBatch
from member.models import MemberCategory
from configs.config import config as cfg
from user.models import UserProfile
from django.utils.timezone import now
auto_ticket_member_category_levels = [cfg.MEMBER.MOTH_MEMBER_LEVEL,
cfg.MEMBER.YEAR_MEMBER_LEVEL]
auto_ticket_member_categories = MemberCategory.objects.filter(
level__in=auto_ticket_member_category_levels)
auto_ticket_user_count = UserProfile.objects.filter(
member_category__level__in=auto_ticket_member_category_levels).count()
create_user = UserProfile.objects.filter(uuid=cfg.TICKET.ADMIN_UUID).first()
base_time_str = now().strftime('%Y-%m-%d')
start_time = base_time_str + ' 00:00:00'
end_time = base_time_str + ' 23:59:59'
ticket_batch = TicketBatch.objects.create(
ticket_name=cfg.TICKET.AUTO_TICKET_NAME,
full_amount=cfg.TICKET.AUTO_TICKET_FULL,
minus_amount=cfg.TICKET.AUTO_TICKET_MINUS,
provide_sum=auto_ticket_user_count,
remain_sum=auto_ticket_user_count,
start_time=start_time,
end_time=end_time,
is_active=True,
create_user=create_user
)
ticket_batch.member_category.set(auto_ticket_member_categories)