程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Django dynamic configuration environment dynaconf + crontab scheduled task script

編輯:Python

1、 Dynamic configuration : I shared the last one , Post a link :CSDNhttps://mp.csdn.net/mp_blog/creation/editor/122606790

2、linux Of crontab Timing task  :

CSDNhttps://mp.csdn.net/mp_blog/creation/editor/1207335073、 Mission sh Script :

#!/bin/sh
# Enter the current script folder , Make sure django Initial directory of import package ,pwd
cd `dirname $0`
# Virtual environment python Relative paths , At present pwd
venv/bin/python -u ticket/auto_ticket.py > logs/auto_ticket.log 2>&1 &

4、 Specific content of the task .py Script

import re
import sys
import django
import os
sys.path.extend([os.getcwd()])
# because shell System variables cannot be read in the script , Use the traversal matching method to find the configuration
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)


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved