在寫運維工單的消息推送時,直接使用郵件發送會致使系統反應特別慢,因此研究了一下rabbitmq+celery來實現郵件發送的異步執行html
部署rabbitmqpython
RabbitMQ是基於Erlang的,因此首先必須配置Erlang環境。數據庫
從Erlang的官網 http://www.erlang.org/download.html 下載最新的erlang安裝包,我下載的版本是 otp_src_R15B01.tar.gz。django
tar xvzf otp_src_R15B01.tar.gz cd otp_src_R14B03 ./configure make make install
安裝完Erlang,開始安裝RabbitMQ-Server。json
主要參考官方文檔:http://www.rabbitmq.com/build-server.htmlbash
須要安裝一個比較新的Python版本。安裝略。app
'''運維
這一步我沒有作,安裝也沒收到影響,在此僅是記載異步
須要安裝simplejson。今後處下載最新的版本: http://pypi.python.org/pypi/simplejson#downloads 。我下載的版本是 simplejson-2.2.1.tar.gzide
$ tar xvzf simplejson-2.2.1.tar.gz $ cd simplejson-2.2.1 $ sudo python setup.py install
''''
而後安裝RabbitMQ Server。今後處下載源代碼版本的RabbitMQ: http://www.rabbitmq.com/server.html。我下載的版本是 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
在sbin/目錄下出現了三個命令:
rabbitmqctl rabbitmq-env rabbitmq-server
安裝成功。
運行
找到sbin/目錄,運行程序:
/usr/local/sbin/rabbitmq-server –detached
中止程序:
/usr/local/sbin/rabbitmqctl stop
在settings.py中加入rabbitmq配置:
BROKER_HOST = "127.0.0.1" BROKER_PORT = 5672 BROKER_USER = "guest" BROKER_PASSWORD = "guest" BROKER_VHOST = "/"
'''如下爲參考資料'''
配置
主要參考官方文檔:http://www.rabbitmq.com/configure.html
通常狀況下,RabbitMQ的默認配置就足夠了。若是但願特殊設置的話,有兩個途徑:
一個是環境變量的配置文件 rabbitmq-env.conf ;
一個是配置信息的配置文件 rabbitmq.config;
注意,這兩個文件默認是沒有的,若是須要必須本身建立。
rabbitmq-env.conf
這個文件的位置是肯定和不能改變的,位於:/etc/rabbitmq目錄下(這個目錄須要本身建立)。
文件的內容包括了RabbitMQ的一些環境變量,經常使用的有:
#RABBITMQ_NODE_PORT= //端口號
#HOSTNAME=
RABBITMQ_NODENAME=mq
RABBITMQ_CONFIG_FILE= //配置文件的路徑
RABBITMQ_MNESIA_BASE=/rabbitmq/data //須要使用的MNESIA數據庫的路徑
RABBITMQ_LOG_BASE=/rabbitmq/log //log的路徑
RABBITMQ_PLUGINS_DIR=/rabbitmq/plugins //插件的路徑
具體的列表見:http://www.rabbitmq.com/configure.html#define-environment-variables
rabbitmq.config
這是一個標準的erlang配置文件。它必須符合erlang配置文件的標準。
它既有默認的目錄,也能夠在rabbitmq-env.conf文件中配置。
文件的內容詳見:http://www.rabbitmq.com/configure.html#config-items
部署celery
直接執行
pip install django-celery
在settings.py中加入如下配置
import djcelery djcelery.setup_loader() ... INSTALLED_APPS = ( ... 'djcelery', ... )
最後建立Celery所需的數據表, 若是使用South做爲數據遷移工具, 則運行:
python manage.py migrate
不然運行: (Django 1.6或Django 1.7均可以)
python manage.py syncdb
到此django+rabbitmq+celery的環境就部署完了,下面是郵件發送的代碼,這裏是用的django自帶的郵件發送功能
在settings.py中加入郵件發送配置:
EMAIL_HOST='outlook.office365.com' EMAIL_HOST_USER='**@**.com' EMAIL_HOST_PASSWORD='***' EMAIL_USE_TLS = True
建立一個新py文件,命名爲tasks.py
from celery.task import Task from celery import task from workflow import models from django.core.mail import EmailMessage from django.template import loader from 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模版:
<!DOCTYPE html> <html lang="en"> <h>您好,您有新工單消息提醒,詳情請點擊 http://**.**.com/</h> <table class="cellspacing="1px" style="width: 50%; border: 1px outset rgb(128, 128, 128); border-spacing: 1px;""> <thead> <tr> <th style="border:1px #808080 inset;padding:1px;">列名</th> <th style="border:1px #808080 inset;padding:1px;">相應值</th> </tr> </thead> <tbody> {% for item in content %} <tr> <th style="border:1px #808080 inset;padding:1px;">主鍵ID</th> <th style="border:1px #808080 inset;padding:1px;">{{ item.id }}</th> </tr> <tr> <th style="border:1px #808080 inset;padding:1px;">工單名稱</th> <th style="border:1px #808080 inset;padding:1px;">{{ item.title }}</th> </tr> <tr> <th style="border:1px #808080 inset;padding:1px;">申請人</th> <th style="border:1px #808080 inset;padding:1px;">{{ item.creator }}</th> </tr> <tr> <th style="border:1px #808080 inset;padding:1px;">工單類型</th> <th style="border:1px #808080 inset;padding:1px;">{{ item.type }}</th> </tr> <tr> <th style="border:1px #808080 inset;padding:1px;">建立時間</th> <th style="border:1px #808080 inset;padding:1px;">{{ item.create_time|date:"Y-m-d H:i:s" }}</th> </tr> <tr> <th style="border:1px #808080 inset;padding:1px;">審批人</th> <th style="border:1px #808080 inset;padding:1px;">{{ item.approveuser }}</th> </tr> <tr> <th style="border:1px #808080 inset;padding:1px;">描述</th> <th style="border:1px #808080 inset;padding:1px;">{{ item.description }}</th> </tr> <tr> <th style="border:1px #808080 inset;padding:1px;">處理人</th> <th style="border:1px #808080 inset;padding:1px;">{{ item.deal_user }}</th> </tr> <tr> <th style="border:1px #808080 inset;padding:1px;">運帷回覆</th> <th style="border:1px #808080 inset;padding:1px;">{{ item.opsreply }}</th> </tr> <tr> <th style="border:1px #808080 inset;padding:1px;">狀態</th> <th style="border:1px #808080 inset;padding:1px;">{{ item.state }}</th> </tr> {% endfor %} </tbody> </table> </html>