When you write a message push for a work order, it can cause the system to react very slowly by sending it directly, so we study rabbitmq+celery to implement asynchronous execution of mail delivery.
Deploying RABBITMQ
RABBITMQ is based on Erlang, so you must first configure the Erlang environment.
Download the latest Erlang installation package from Erlang's official website http://www.erlang.org/download.html, the version i downloaded is otp_src_R15B01.tar.gz.
Tar xvzf otp_src_R15B01.tar.gzcd otp_src_r14b03./configuremakemake Install
After installing Erlang, start installing Rabbitmq-server.
Main references official documents: http://www.rabbitmq.com/build-server.html
A newer version of Python needs to be installed. Install slightly.
‘‘‘
This step I did not do, the installation has not been affected, this is only recorded
Need to install Simplejson. Download the latest version from here: Http://pypi.python.org/pypi/simplejson#downloads. The version I downloaded is simplejson-2.2.1.tar.gz
$ tar xvzf simplejson-2.2.1.tar.gz$ cd simplejson-2.2.1$ sudo python setup.py install
‘‘‘‘
Then install RABBITMQ Server. Download the source code version of rabbitmq:http://www.rabbitmq.com/server.html from here. The version I downloaded is rabbitmq_server-3.5.4.tar.gz
$ tar xvzf rabbitmq_server-3.5.4.tar.gz$ cd rabbitmq_server-3.5.4$ make# target_dir=/usr/local SBIN_DIR=/usr/local/ Sbin Man_dir=/usr/local/man make Install
There are three commands in the sbin/directory:
Rabbitmqctl rabbitmq-env Rabbitmq-server
The installation was successful.
Run
Locate the sbin/directory and run the program:
/usr/local/sbin/rabbitmq-server–detached
Stop program:
/usr/local/sbin/rabbitmqctl stop
To add the RABBITMQ configuration to the settings.py:
Broker_host = "127.0.0.1" Broker_port = 5672broker_user = "Guest" Broker_password = "Guest" Broker_vhost = "/"
"The following are references"
Configuration
Main references official documents: http://www.rabbitmq.com/configure.html
In general, the default configuration for RABBITMQ is sufficient. If you want a special setting, there are two ways:
One is the environment variable configuration file rabbitmq-env.conf;
A configuration file that is configuration information rabbitmq.config;
Note that these two files are not by default and must be created yourself if necessary.
Rabbitmq-env.conf
The location of this file is deterministic and immutable, located in:/etc/rabbitmq directory (this directory needs to be created by itself).
The contents of the file include some of the environment variables of RABBITMQ, commonly used are:
#RABBITMQ_NODE_PORT =//Port number
#HOSTNAME =
Rabbitmq_nodename=mq
rabbitmq_config_file=//path of the configuration file
Rabbitmq_mnesia_base=/rabbitmq/data//path of the MNESIA database to be used
Rabbitmq_log_base=/rabbitmq/log//log's Path
Rabbitmq_plugins_dir=/rabbitmq/plugins//plug-in Path
For a specific list, see: Http://www.rabbitmq.com/configure.html#define-environment-variables
Rabbitmq.config
This is a standard Erlang configuration file. It must conform to the standards of the Erlang profile.
It has both a default directory and can be configured in the rabbitmq-env.conf file.
The contents of the file are described in: http://www.rabbitmq.com/configure.html#config-items
Deploying celery
Direct execution
Pip Install Django-celery
Add the following configuration to the settings.py
Import Djcelery Djcelery.setup_loader () ... Installed_apps = (... ' Djcelery ', ...)
Finally create the data tables required for celery, and if you use South as the Data Migration Tool, run:
Python manage.py Migrate
Otherwise run: (either Django 1.6 or Django 1.7)
Python manage.py syncdb
To this django+rabbitmq+celery environment is deployed, the following is the code sent by mail, here is the use of Django's own mail sending function
To add a mail send configuration in settings.py:
email_host= ' outlook.office365.com ' email_host_user= ' **@**.com ' email_host_password= ' * * * * Email_use_tls = True
Create a new py file named tasks.py
from celery.task import taskfrom celery import taskfrom workflow import modelsfrom django.core.mail import emailmessagefrom django.template Import loaderfrom worksystem.settings import email_host_user@task () def send_ Email (subject, content, to_name_list): html_content = loader.render _to_string ( ' sendmail.html ', #需要渲染的html模板 { ' contEnt ':content } ) msg = emailmessage (Subject, html_content, email_host_user, to_name_list) msg.content_subtype = "HTML" # Main Content is now text/html msg.send ()
sendmail.html Template:
<! Doctype html>
OPS Ticket--Message Queuing send mail