Commit f7ab26a5 authored by ibuler's avatar ibuler

Add celery broker

parents 0d3bde11 e8d8f7c4
## Celery
Jumpserver use celery to run task async. Using redis as the broker, so
you should run a redis instance
1. Run redis
```
# yum -y install redis
or
# docker run -name jumpserver-redis -d -p 6379:6379 redis redis-server
```
2. Write tasks in app_name/tasks.py
ops/tasks.py
```
from __future__ import absolute_import
import time
from celery import shared_task
@shared_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
```
3. Run celery in development
```
# cd apps
# celery -A common worker -l info
```
4. Test using task
```
# ./manage.py shell
> from ops.tasks import longtime_add
> res = longtime_add.delay(1, 2)
> res.get()
```
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
# ~*~ 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')
print(settings.BROKER_URL)
app.autodiscover_tasks(lambda: [app_config.split('.')[0] for app_config in settings.INSTALLED_APPS])
......@@ -257,8 +257,8 @@ WEBSOCKET_URL = '/ws/'
# WebSocket Redis
WS4REDIS_CONNECTION = {
'host': '127.0.0.1',
'port': 6379,
'host': CONFIG.REDIS_HOST or '127.0.0.1',
'port': CONFIG.REDIS_PORT or 6379,
'db': 2,
}
......@@ -276,3 +276,11 @@ SESSION_REDIS_PREFIX = 'session'
# Custom User Auth model
AUTH_USER_MODEL = 'users.User'
# Celery using redis as broker
BROKER_URL = 'redis://%(password)s%(host)s:%(port)s/3' % {
'password': CONFIG.REDIS_PASSWORD + ':' if CONFIG.REDIS_PASSWORD else '',
'host': CONFIG.REDIS_HOST or '127.0.0.1',
'port': CONFIG.REDIS_PORT or 6379,
}
CELERY_RESULT_BACKEND = BROKER_URL
This diff is collapsed.
from .tasks import longtime_add
import time
result = longtime_add.delay(1,2)
print 'Task finished? ', result.ready()
print 'Task result: ', result.result
time.sleep(10)
print 'Task finished? ', result.ready()
print 'Task result: ', result.result
\ No newline at end of file
from __future__ import absolute_import
import time
from celery import shared_task
@shared_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
......@@ -20,6 +20,9 @@ class Config:
DATABASE_ENGINE = 'sqlite3'
HTTP_LISTEN_HOST = '127.0.0.1'
HTTP_LISTEN_PORT = 8000
REDIS_HOST = '127.0.0.1'
REDIS_PORT = 6379
REDIS_PASSWORD = ''
def __init__(self):
pass
......
......@@ -14,3 +14,5 @@ websocket-client==0.37.0
djangorestframework==3.4.5
ForgeryPy==0.1
paramiko==2.0.2
celery==3.1.23
ansible==2.1.1.0
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment