You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jumpserver/apps/common/tasks.py

61 lines
1.6 KiB

8 years ago
from __future__ import absolute_import
8 years ago
import os
8 years ago
from celery import shared_task
8 years ago
from celery.schedules import crontab
8 years ago
from django.core.mail import send_mail
8 years ago
# 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')
8 years ago
from django.conf import settings
8 years ago
app = Celery('jumpserver')
8 years ago
8 years ago
# 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
8 years ago
def send_mail_async(*args, **kwargs):
""" Using celery to send email async
You can use it as django send_mail function
Example:
send_mail_sync.delay(subject, message, from_mail, recipient_list, fail_silently=False, html_message=None)
Also you can ignore the from_mail, unlike django send_mail, from_email is not a require args:
Example:
send_mail_sync.delay(subject, message, recipient_list, fail_silently=False, html_message=None)
"""
if len(args) == 3:
args = list(args)
args[0] = settings.EMAIL_SUBJECT_PREFIX + args[0]
args.insert(2, settings.EMAIL_HOST_USER)
args = tuple(args)
send_mail(*args, **kwargs)
8 years ago
# @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',)
# }
# }