def start(self): with daemon.DaemonContext(): logger = get_logger('all_supporters') #logger takes no effect #print logger.name logger.info('Daemon pid is %s' % os.getpid()) os.system("echo %d > %s" % (os.getpid(), PathConst.SUPPORTERS_DAEMON_PID)) signal.signal(signal.SIGTERM, self.handle_quit) #os.system("python /home/anshun/python/django_site/manage.py students_supporter") child = subprocess.Popen([ 'python', os.path.join(settings.BASE_DIR, 'manage.py'), 'students_supporter' ]) self.children.append(child) while not self.is_stop: time.sleep(60 * 60) os.system("echo %s > %s" % ('begin terminate subprocess', PathConst.DAEMON_LOG)) for child in self.children: #A None value indicates that the process hasn't terminated yet. # A negative value -N indicates that the child was terminated by signal N (Unix only). if child.poll() is None: child.terminate() for child in self.children: child.wait() os.remove(PathConst.SUPPORTERS_DAEMON_PID)
import logging from django.shortcuts import render from django.contrib.auth import authenticate, login, logout from django.contrib.auth.models import User from rest_framework.views import APIView from rest_framework.response import Response from rest_framework import status from teachers.models import Teacher from students.models import Student from common_interface.log_interface import get_logger logger = get_logger() class LoginView(APIView): authentication_classes = () permission_classes = () def post(self, request): #admin ***** username = request.data.get('username', None) password = request.data.get('password', None) logger.info('%s login' % username) user = authenticate(username=username, password=password) if user is not None: login(request, user)
import os from openpyxl import Workbook from helper.models import Account from common_interface.log_interface import get_logger logger = get_logger('account_supporter') from django.core.management.base import BaseCommand, CommandError from django.conf import settings class Command(BaseCommand): def add_arguments(self, parser): parser.add_argument('action', type=str) parser.add_argument( '--time', default=None, help='select the year or month you want to export ex. 201801 ') def export(self, **options): wb = Workbook() sheet = wb.create_sheet('account', index=0) sheet.append(['编号', '用户', '金额', '备注', '文件地址']) if len(options['time']) == len('201801'): month = options['time'][-2:] #logger.info(month) year = options['time'][0:4] #logger.info(year) for account in Account.objects.all().filter(
from common_interface.log_interface import get_logger logger = get_logger('all_supporters') import daemon import os import subprocess import time import signal from django.core.management.base import BaseCommand, CommandError from django.conf import settings from common_interface.const import PathConst class Command(BaseCommand): def __init__(self): super(Command, self).__init__() self.children = [] self.is_stop = False def add_arguments(self, parser): parser.add_argument('action', type=str) def handle_quit( self, signum, frame ): # these two parameters must be included. Or this method may not be executed. logger.info('handle quit signal') self.is_stop = True os.system("echo %s > %s" % ('quit signal received', PathConst.DAEMON_LOG))
import os from common_interface.log_interface import get_logger logger = get_logger('students_supporter') from django.core.management.base import BaseCommand, CommandError class Command(BaseCommand): def handle(self, *args, **options): logger.info('start students supporter') try: while True: pass except Exception as e: logger.error('%s' % str(e))