From 363ddb70e21f31a7910e2ad469c6ea964a051ce5 Mon Sep 17 00:00:00 2001 From: yumaojun03 <719118794@qq.com> Date: Thu, 25 Aug 2016 19:52:03 +0800 Subject: [PATCH] add celery support (not use django-celery) --- apps/common/celery.py | 21 +++++++++++++++++ apps/jumpserver/settings.py | 3 --- apps/ops/__init__.py | 1 + apps/ops/tasks.py | 45 +++++++++---------------------------- 4 files changed, 32 insertions(+), 38 deletions(-) create mode 100644 apps/common/celery.py diff --git a/apps/common/celery.py b/apps/common/celery.py new file mode 100644 index 000000000..18483a418 --- /dev/null +++ b/apps/common/celery.py @@ -0,0 +1,21 @@ +from __future__ import absolute_import +from celery import Celery + +# import traceback + +# # Import project config setting +# try: +# from config import config as env_config, env +# JMS_CONF = env_config.get(env, 'default')() +# print "ok" +# except ImportError: +# traceback.print_exc() +# JMS_CONF = type('_', (), {'__getattr__': None})() +# print "false" + +app = Celery('common', + broker='redis://localhost:6379/0', + backend='redis://localhost:6379/0', + include=['ops.tasks']) + + diff --git a/apps/jumpserver/settings.py b/apps/jumpserver/settings.py index 949755ffd..8b69953ee 100644 --- a/apps/jumpserver/settings.py +++ b/apps/jumpserver/settings.py @@ -23,8 +23,6 @@ sys.path.append(os.path.dirname(BASE_DIR)) try: from config import config as env_config, env CONFIG = env_config.get(env, 'default')() - BROKER_URL = CONFIG.BROKER_URL - except ImportError: CONFIG = type('_', (), {'__getattr__': None})() @@ -57,7 +55,6 @@ INSTALLED_APPS = [ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', - 'djcelery', ] MIDDLEWARE = [ diff --git a/apps/ops/__init__.py b/apps/ops/__init__.py index e69de29bb..8b1378917 100644 --- a/apps/ops/__init__.py +++ b/apps/ops/__init__.py @@ -0,0 +1 @@ + diff --git a/apps/ops/tasks.py b/apps/ops/tasks.py index 1e08daeed..9037a7a9f 100644 --- a/apps/ops/tasks.py +++ b/apps/ops/tasks.py @@ -1,37 +1,12 @@ -from celery import Celery -from django.conf import settings -from time import sleep - -app = Celery('ops', broker=settings.BROKER_URL) +from __future__ import absolute_import +from common.celery import app +import time -@app.task() -def UploadTask(message): - - # Update the state. The meta data is available in task.info dicttionary - # The meta data is useful to store relevant information to the task - # Here we are storing the upload progress in the meta. - - UploadTask.update_state(state='PROGRESS', meta={'progress': 0}) - sleep(30) - UploadTask.update_state(state='PROGRESS', meta={'progress': 30}) - sleep(30) - return message - - -def get_task_status(task_id): - - # If you have a task_id, this is how you query that task - task = UploadTask.AsyncResult(task_id) - - status = task.status - progress = 0 - - if status == u'SUCCESS': - progress = 100 - elif status == u'FAILURE': - progress = 0 - elif status == 'PROGRESS': - progress = task.info['progress'] - - return {'status': status, 'progress': progress} \ No newline at end of file +@app.task +def longtime_add(x, y): + print 'long time task begins' + # sleep 5 seconds + time.sleep(5) + print 'long time task finished' + return x + y \ No newline at end of file