From 26a5fbe5097f4652b516e48b4d50e989fc6e2129 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=95=E5=85=A8?= Date: Tue, 5 Mar 2019 15:07:03 +0800 Subject: [PATCH] =?UTF-8?q?v=200.1.1=20=E8=8B=A5=E5=B9=B2=E4=BC=98?= =?UTF-8?q?=E5=8C=96,=E5=A2=9E=E5=8A=A0celery=E5=BC=82=E6=AD=A5=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=20demo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit v 0.1.1 若干优化,增加celery异步任务 demo --- README.md | 27 +++++++++++++++++++- requirements.txt | 39 ++++++++++++++++++++++++++--- seal/__init__.py | 6 +++++ seal/celery.py | 34 +++++++++++++++++++++++++ seal/settings.py | 28 +++++++++++++++++++++ system/tasks.py | 45 ++++++++++++++++++++++++++++++++++ templates/assets/ecs-list.html | 1 - 7 files changed, 175 insertions(+), 5 deletions(-) create mode 100644 seal/celery.py create mode 100644 system/tasks.py diff --git a/README.md b/README.md index 8f1208a..925508e 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ # 海豹 + > django-base-templastes > 因本项目开始时间为3月1日,是 国际海豹日,故项目起名为 海豹 seal @@ -10,7 +11,7 @@ * v0.1 ## 介绍 -* 基于bootstrap4+django2.1+python3.7 +* 基于bootstrap4+django2.1+python3.7(兼容3.6)+celery异步任务 * 会尽量多加一些注释 * 采用cbv开发方式,提高开发效率 @@ -39,9 +40,33 @@ python manage.py createsuperuser python manage.py runserver 0.0.0.0:80 +``` +* 扩展功能 +```bash +#需要安装redis +#启动celery异步任务 +cd seal +celery -B -A seal worker -l info +``` + +## 注意 +* 如果想直接拿来做生产项目,请重新生成一个 settings 文件里面的 SECRET_KEY +* 时区问题 +```python +##因为开启了时区,所以django在数据库里面保存的为 utc 时间, 调用的时候会帮你 转为 东八区, celery会自动识别时间 +from django.utils import timezone +for i in Users.objects.all(): + print(i.last_login) ## 直接读取时间,会是 utc时间,未转换, 如果需要处理 请注意 + print(timezone.localtime(i.last_login).strftime("%Y-%m-%d %H:%M:%S")) ## 时间格式化为 正常时间 + +## 2019-03-05 06:41:18.040809+00:00 +## 2019-03-05 14:41:18 ``` + + + ### 售后服务 * 有问题 可以加QQ群: 620176501 django开发讨论群 diff --git a/requirements.txt b/requirements.txt index 76a213c..1e53c77 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,36 @@ -django==2.1.7 -django-bootstrap4 -django-pure-pagination \ No newline at end of file +amqp==2.4.2 +backcall==0.1.0 +billiard==3.5.0.5 +celery==4.2.1 +certifi==2018.11.29 +chardet==3.0.4 +decorator==4.3.0 +Django==2.1.7 +django-bootstrap4==0.0.7 +django-celery-beat==1.4.0 +django-celery-results==1.0.4 +django-pure-pagination==0.3.0 +django-timezone-field==3.0 +idna==2.8 +ipython==6.4.0 +ipython-genutils==0.2.0 +jedi==0.12.0 +kombu==4.4.0 +parso==0.2.1 +pexpect==4.6.0 +pickleshare==0.7.4 +prompt-toolkit==1.0.15 +ptyprocess==0.5.2 +Pygments==2.2.0 +PyMySQL==0.9.3 +python-crontab==2.3.6 +python-dateutil==2.8.0 +pytz==2018.4 +redis==3.2.0 +requests==2.21.0 +simplegeneric==0.8.1 +six==1.11.0 +traitlets==4.3.2 +urllib3==1.24.1 +vine==1.2.0 +wcwidth==0.1.7 diff --git a/seal/__init__.py b/seal/__init__.py index e69de29..c11e35e 100644 --- a/seal/__init__.py +++ b/seal/__init__.py @@ -0,0 +1,6 @@ +from .celery import app as celery_app + +__all__ = ['celery_app'] + +import pymysql +pymysql.install_as_MySQLdb() \ No newline at end of file diff --git a/seal/celery.py b/seal/celery.py new file mode 100644 index 0000000..83b9b69 --- /dev/null +++ b/seal/celery.py @@ -0,0 +1,34 @@ +from __future__ import absolute_import, unicode_literals +import os +from celery.schedules import crontab +from datetime import timedelta +from celery import Celery, platforms + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'seal.settings') + +app = Celery('seal') +app.config_from_object('django.conf:settings', namespace='CELERY') +app.autodiscover_tasks() +platforms.C_FORCE_ROOT = True + + +@app.task(bind=True) +def debug_task(self): + print('Request: {0!r}'.format(self.request)) + + +##定时任务 +app.conf.update( + CELERYBEAT_SCHEDULE={ + 'demo1': { + 'task': 'system.tasks.system_demo', + 'schedule': timedelta(seconds=10), + 'args': [111] + }, + 'demo2': { + 'task': 'system.tasks.system_demo', + 'schedule': crontab(minute=00, hour=00,day_of_month=1), + 'args': [222] + }, + } +) diff --git a/seal/settings.py b/seal/settings.py index f6173a8..bdace14 100644 --- a/seal/settings.py +++ b/seal/settings.py @@ -39,6 +39,8 @@ INSTALLED_APPS = [ 'system.apps.SystemConfig', 'assets.apps.AssetsConfig', 'bootstrap4', + 'django_celery_results', + 'django_celery_beat', ] @@ -180,3 +182,29 @@ PAGINATION_SETTINGS = { # 表格table 一页 展示数据 DISPLAY_PER_PAGE = 15 + + +## celery 4 +CELERY_RESULT_BACKEND = 'redis://localhost:6379/1' +# CELERY_RESULT_BACKEND = 'django-db' +CELERY_BROKER_URL = 'redis://localhost:6379/2' +CELERY_BEAT_SCHEDULER = 'django_celery_beat.schedulers:DatabaseScheduler' +CELERYD_CONCURRENCY = 4 +CELERY_TIMEZONE = 'Asia/Shanghai' +CELERYD_MAX_TASKS_PER_CHILD = 10 +CELERYD_FORCE_EXECV = True + +# 设置默认不存结果 +# CELERY_IGNORE_RESULT = True +CELERY_CREATE_MISSING_QUEUES = True +CELERY_DISABLE_RATE_LIMITS = True +CELERYD_TASK_SOFT_TIME_LIMIT = 600 + +CELERY_TASK_RESULT_EXPIRES = 600 +CELERY_ENABLE_UTC = False +CELERY_TASK_SERIALIZER = 'json' +CELERY_RESULT_SERIALIZER = 'json' + + +## 钉钉 报警机器人 地址 调用地方为 system.tasks.ding_ding_to_info +web_hook_url="" \ No newline at end of file diff --git a/system/tasks.py b/system/tasks.py new file mode 100644 index 0000000..a6f0883 --- /dev/null +++ b/system/tasks.py @@ -0,0 +1,45 @@ +import logging +import requests +import json +from celery import shared_task +from system.models import Users +from seal import settings +logger = logging.getLogger('system_celery') + + +@shared_task +def system_demo(one): + ##因为开启了时区,所以django在数据库里面保存的为 utc 时间, 调用的时候会帮你 转为 东八区, celery会自动识别时间 + from django.utils import timezone + for i in Users.objects.all(): + print(i.last_login) ## 直接读取时间,会是 utc时间,未转换,如果需要处理 请注意 + print(timezone.localtime(i.last_login).strftime("%Y-%m-%d %H:%M:%S")) ## 时间格式化为 正常时间 + print("celery定时任务demo 每分钟执行一遍",one) + return + + +@shared_task +def ding_ding_to_info(content,type=None): + """ + 钉钉接口 异步调用 ding_ding_to_info.delay("报警1") + :param content: 文本内容 + :param type: + :return: + """ + web_hook_url = getattr(settings, 'web_hook_url'), + headers = {'content-type': 'application/json'} + data = { + "msgtype": "text", + "text": { + "content": content + }, + "at": { + "atMobiles": [ + ], + } + } + try: + r = requests.post(web_hook_url[0], data=json.dumps(data), headers=headers) + print(r.text) + except Exception as e: + logger.error(e) \ No newline at end of file diff --git a/templates/assets/ecs-list.html b/templates/assets/ecs-list.html index 79ca3d1..852982b 100644 --- a/templates/assets/ecs-list.html +++ b/templates/assets/ecs-list.html @@ -1,5 +1,4 @@ {% extends "base/base.html" %} -{% load i18n %} {% load asset_filter %} {% load bootstrap4 %} {% load static %}