From 26352174214df4a755a19330bc925c43c8aa0332 Mon Sep 17 00:00:00 2001 From: ibuler Date: Fri, 11 Nov 2016 09:48:47 +0800 Subject: [PATCH] Update celery --- apps/audits/tasks.py | 3 ++- apps/celerybeat-schedule.db | 0 apps/celerybeat.pid | 1 + apps/common/__init__.py | 2 +- apps/common/celery.py | 41 ++++++++++++++++++------------------- apps/common/tasks.py | 36 +++++++++++++++++++++++++++----- 6 files changed, 55 insertions(+), 28 deletions(-) create mode 100644 apps/celerybeat-schedule.db create mode 100644 apps/celerybeat.pid diff --git a/apps/audits/tasks.py b/apps/audits/tasks.py index a43d261de..f80aa5f75 100644 --- a/apps/audits/tasks.py +++ b/apps/audits/tasks.py @@ -8,4 +8,5 @@ from .utils import write_login_log @shared_task def write_login_log_async(*args, **kwargs): - write_login_log(*args, **kwargs) \ No newline at end of file + write_login_log(*args, **kwargs) + diff --git a/apps/celerybeat-schedule.db b/apps/celerybeat-schedule.db new file mode 100644 index 000000000..e69de29bb diff --git a/apps/celerybeat.pid b/apps/celerybeat.pid new file mode 100644 index 000000000..a5a55e28c --- /dev/null +++ b/apps/celerybeat.pid @@ -0,0 +1 @@ +64256 diff --git a/apps/common/__init__.py b/apps/common/__init__.py index 88fef8e67..02bbfb61f 100644 --- a/apps/common/__init__.py +++ b/apps/common/__init__.py @@ -2,5 +2,5 @@ from __future__ import absolute_import # This will make sure the app is always imported when # Django starts so that shared_task will use this app. -from .celery import app as celery_app +# from .celery import app as celery_app diff --git a/apps/common/celery.py b/apps/common/celery.py index 9d006fe4a..681ce6a6a 100644 --- a/apps/common/celery.py +++ b/apps/common/celery.py @@ -1,21 +1,20 @@ -# ~*~ coding: utf-8 ~*~ - -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', 'jumpserver.settings') - -from django.conf import settings - -app = Celery('jumpserver') - -# Using a string here means the worker will not have to -# pickle the object when using Windows. -app.config_from_object('django.conf:settings') - -app.autodiscover_tasks(lambda: [app_config.split('.')[0] for app_config in settings.INSTALLED_APPS]) - +# # ~*~ coding: utf-8 ~*~ +# +# 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', 'jumpserver.settings') +# +# from django.conf import settings +# +# app = Celery('jumpserver') +# +# # Using a string here means the worker will not have to +# # pickle the object when using Windows. +# app.config_from_object('django.conf:settings') +# +# app.autodiscover_tasks(lambda: [app_config.split('.')[0] for app_config in settings.INSTALLED_APPS]) +# diff --git a/apps/common/tasks.py b/apps/common/tasks.py index 8df504baa..43d35f313 100644 --- a/apps/common/tasks.py +++ b/apps/common/tasks.py @@ -1,11 +1,29 @@ from __future__ import absolute_import +import os from celery import shared_task +from celery.schedules import crontab from django.core.mail import send_mail +# from django.conf import settings +# from common import celery_app + + +from celery import Celery + +# set the default Django settings module for the 'celery' program. +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'jumpserver.settings') + from django.conf import settings +app = Celery('jumpserver') -@shared_task(name='send_mail_async') +# Using a string here means the worker will not have to +# pickle the object when using Windows. +app.config_from_object('django.conf:settings') +app.autodiscover_tasks(lambda: [app_config.split('.')[0] for app_config in settings.INSTALLED_APPS]) + + +@app.task def send_mail_async(*args, **kwargs): """ Using celery to send email async @@ -28,7 +46,15 @@ def send_mail_async(*args, **kwargs): send_mail(*args, **kwargs) -# def send_mail_async(subject, message, from_mail, recipient_list, fail_silently=False, html_message=None): -# if settings.CONFIG.MAIL_SUBJECT_PREFIX: -# subject += settings.CONFIG.MAIL_SUBJECT_PREFIX -# send_mail(subject, message, from_mail, recipient_list, fail_silently=fail_silently, html_message=html_message) +# @celery_app.task +# def test(arg): +# print(arg) + + +# celery_app.conf.beat_schedule = { +# 'add-every-30-seconds': { +# 'task': 'common.test', +# 'schedule': crontab(minute='*/1'), +# 'args': ('nihao',) +# } +# }