Game or forum web applications often have points systems later on: how many points do you get for Posting a successful article or replying to a blog? Points systems are relevant to many business scenarios, but not to specific user requests. Integration systems should be isolated from specific business processes to reduce coupling. The integral system to be developed by the author does not need to have real time, so we plan to implement the integral calculation program in an asynchronous way. Implementation scheme:

  • useceleryImplementation of integral calculation and verification procedures
  • Prepare parameters in Django Middleware and call the credits system asynchronously

Django middleware profile

Middleware is a framework of hooks into Django’s request/response processing. It’s a light, Low-level “plugin” system for globally altering Django’s input or output. Personally, Django Middleware is an implementation of the idea of aspect-oriented programming (AOP) : providing uniform, insensitive processing for all views applications. In real life scenarios, middleware can be used to unify response output formats, exception handling, and login authentication information in input. The new version of custom Middleware is simple:

  1. Creating a Python file
class TestMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response
        The code written after this initializes the middleware only once

    def __call__(self, request):
        The code written here executes before calling the view method to handle the request

        response = self.get_response(request)

        The code written here is executed after calling the view method. It can format the return value of response
        
        return response
Copy the code
  1. After writing the middleware logic, place the class in Django’s Settings file
# Note that djangos middle calls are decorated in this order, the outer layer of the preceding decoration, i.e. :
The earlier the views method is called, the later the views method is called
The later the views method is called, the earlier the views method is called
MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware'.'django.contrib.sessions.middleware.SessionMiddleware'.'django.middleware.common.CommonMiddleware'.'django.middleware.csrf.CsrfViewMiddleware'.'django.contrib.auth.middleware.AuthenticationMiddleware'.'django.contrib.messages.middleware.MessageMiddleware'.'django.middleware.clickjacking.XFrameOptionsMiddleware'.'your middleware path',]Copy the code

With that in mind, this middleware completes Django Middleware


Django integration celery

Celery – Distributed Task Queue Celery is a simple, flexible, and reliable distributed system to process vast amounts of messages, While providing operations with the tools required to maintain such a system. It’s a task queue with focus on real-time processing, while also supporting task scheduling. Celery is a distributed task queue developed in Python which can be used for synchronous processing of tasks (receive returns from tasks) or asynchronous processing (not receive returns). Handling asynchronous tasks in Django (emailing; Celery is a good option when pushing messages, etc. Running celery requires middleware, here rabbitMQ is used, a brief description of how to integrate.

Install the rabbitMQ

  1. Brew Install RabbitMQ
  2. Setting environment VariablesPATH=$PATH:/usr/local/sbin
  3. Start sudo Rabbitmq-server-detached
  4. Set user

$ sudo rabbitmqctl add_user myuser mypassword $ sudo rabbitmqctl add_vhost myvhost $ sudo rabbitmqctl set_user_tags Myuser mytag $sudo rabbitmqctl set_permissions -p myvhost myuser “.*” “.*” “.* ‘amqp://myuser:mypassword@localhost:5672/myvhost

  1. Start stop start:sudo rabbitmq-serverStart in background mode:$ sudo rabbitmq-server -detachedStop:$ sudo rabbitmqctl stopNever use kill(kill(1)) to stop the RabbitMQ server. For detailed operation, seeThe rabbit’s official website

Install the celery

If not, let’s not install celery. If not, let’s not install celery

Django integration celery

Django build-celery incorporates celery configurations into django’s Settings file and, more importantly, uses the django project runtime environment directly when tasks are performed.

  • Configure the Settings file: Once the rabbitMQ installation is set up, import it directly into the Settings fileCELERY_BROKER_URL = 'amqp://myuser:mypassword@localhost:5672/myvhost'
  • Write celery execute entry filecelery.py.The files are stored in the parent directory of Settings
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery


# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE'.'test_app.settings')

app = Celery('msl')

# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
# should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')

# Load task modules from all registered Django app configs.
app.autodiscover_tasks()

@app.task(bind=True)
def debug_task(self):
    print('Request: {0! r}'.format(self.request))
Copy the code
  • Write custom tasks in custom appOne levelLet’s create a new Python file called tasks.py to build custom tasks.
# Create your tasks here
from __future__ import absolute_import, unicode_literals
from celery import shared_task


@shared_task
def add(x, y):
    return x + y


@shared_task
def mul(x, y):
    return x * y


@shared_task
def xsum(numbers):
    return sum(numbers)
Copy the code
  • Start stop celery to start in background running mode, can be usedsupervisordAnd other management procedures for start and stop management. The non-background startup mode is:celery -A proj worker -l info; Celery: proj = name of the directory in which celery is located, otherwise the project name. The use of such a bootctrl + cJust stop.Django integrated Celery official example

Integral system implementation attempt

Train of thought

  • After calling a business process to get a response, Django Middleware can be determined by accessing data in request and Response:
  1. Which business is called this time (request METHOD and path are determined together)
  1. Corresponding user information (user information needs to be added to request in middleware beforehand)
  2. Whether the call was successful (state of response or defined return code, etc.)
  3. Data returned (retrieved from Response)
  • Formulate task rules, in celery task, the invassions of the interface are abstracted into a task, the tasks that meet the requirements are completed and the completed tasks are recorded.

Check-in interface call: After the call is completed, it is judged in task that if the check-in call is successful, it will record that a user has completed a check-in task at this point in time

  • Develop bonus rules for tasks, how many tasks can be completed within the specified time to earn points. And the reward rules corresponding to the just completed tasks were recorded to record the task completion progress of the rule, and the task completion points were recorded to obtain records.

The reward rules are set as: 1 point for completing one check-in task, and 2 points for completing ten consecutive check-in tasks. These two rules will be judged accordingly after the completion of the check-in task. The first one must be directly satisfied, and the progress of sign-in and score acquisition under this rule shall be recorded as completed, and the score acquisition record shall be recorded. If the second rule is not met, record that this rule is lowered to the progress of score acquisition as incomplete, and the incomplete score acquisition will not be recorded.

Library Table Design (Lite version)

from django.db import models


class TaskConfig(models.Model):
    """ Task_type Task type task_desc Task description Method Method (GET, POST,...) Interface_name Interface name ID_group_name Service primary key group name (group name in the regular format) Status Status (editing, using, Created_at Created time deleted_AT Deleted time updated_AT Modified time """
    

class UserTaskRecord(models.Model):
    """ user task Record table User Task Task type (foreign key) business_id Service primary key (ID of a blog created) created_AT Created time updated_AT Modified time delete_AT Deleted time ""
    
    
class ScoreRule(SoftDeleteModel):
    Score_type Task type score_type Integral rule type score_desc Integral rule Description Statistical_period Statistical period (day, week, month, year, None...) Required_number_of_times Required number of times number_limit_all Upper limit on the number of people to obtain start_time Rule validity time end_time rule expiration time status Rule status (edited and effective) can_repeat Whether task content is repeatable (repeatable by default) created_AT Creation time deleted_AT Deletion time updated_AT Modification time ""


class UserScoreInfo(SoftDeleteModel):
    Achievement_type Integral rule type (foreign key) required_number Amount of task achievement_amount_now Achieved achieved Created_at Creation time deadline Calculation deadline updated_AT Modification time deleted_AT Deletion time """
    

class ScoreRecord(SoftDeleteModel):
    Score record Profile account user serial_Number Serial number trade_amount amount_before_trade Volume of the account before calculation amount_after_trade Volume of the account after calculation Business_type Service type (integral rule description) score_info Integral information (foreign key) location Location Country Lang_type language created_AT Creation time updated_AT modification time Deleted_at Delete time """

Copy the code

At this point, a crude points system is basically complete, but there are many limitations to the rules. For example, sharing a blog can earn points, but if the points are different for a specific blog, this will require further design.