mirror of https://github.com/jumpserver/jumpserver
add celery support (not use django-celery)
parent
97a2e8bb50
commit
363ddb70e2
|
@ -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'])
|
||||||
|
|
||||||
|
|
|
@ -23,8 +23,6 @@ sys.path.append(os.path.dirname(BASE_DIR))
|
||||||
try:
|
try:
|
||||||
from config import config as env_config, env
|
from config import config as env_config, env
|
||||||
CONFIG = env_config.get(env, 'default')()
|
CONFIG = env_config.get(env, 'default')()
|
||||||
BROKER_URL = CONFIG.BROKER_URL
|
|
||||||
|
|
||||||
except ImportError:
|
except ImportError:
|
||||||
CONFIG = type('_', (), {'__getattr__': None})()
|
CONFIG = type('_', (), {'__getattr__': None})()
|
||||||
|
|
||||||
|
@ -57,7 +55,6 @@ INSTALLED_APPS = [
|
||||||
'django.contrib.sessions',
|
'django.contrib.sessions',
|
||||||
'django.contrib.messages',
|
'django.contrib.messages',
|
||||||
'django.contrib.staticfiles',
|
'django.contrib.staticfiles',
|
||||||
'djcelery',
|
|
||||||
]
|
]
|
||||||
|
|
||||||
MIDDLEWARE = [
|
MIDDLEWARE = [
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
|
|
@ -1,37 +1,12 @@
|
||||||
from celery import Celery
|
from __future__ import absolute_import
|
||||||
from django.conf import settings
|
from common.celery import app
|
||||||
from time import sleep
|
import time
|
||||||
|
|
||||||
app = Celery('ops', broker=settings.BROKER_URL)
|
|
||||||
|
|
||||||
|
|
||||||
@app.task()
|
@app.task
|
||||||
def UploadTask(message):
|
def longtime_add(x, y):
|
||||||
|
print 'long time task begins'
|
||||||
# Update the state. The meta data is available in task.info dicttionary
|
# sleep 5 seconds
|
||||||
# The meta data is useful to store relevant information to the task
|
time.sleep(5)
|
||||||
# Here we are storing the upload progress in the meta.
|
print 'long time task finished'
|
||||||
|
return x + y
|
||||||
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}
|
|
Loading…
Reference in New Issue