From 97d7e6cb9b5ecad64c9c048184be54297596a2aa Mon Sep 17 00:00:00 2001 From: Administrator Date: Fri, 28 Oct 2016 17:28:32 +0800 Subject: [PATCH] =?UTF-8?q?[future]=20=E4=BF=AE=E6=94=B9celery=20=E4=BD=BF?= =?UTF-8?q?=E7=94=A8eventlet=20=E4=BD=9C=E4=B8=BAconcurrent=20pool,=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=8E=B7=E5=8F=96=E8=B5=84=E4=BA=A7=E7=A1=AC?= =?UTF-8?q?=E4=BB=B6=E4=BF=A1=E6=81=AF=E7=9A=84=20=E5=88=9D=E6=AD=A5?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/ops/api.py | 4 +++- apps/ops/run_tasks.py | 9 --------- apps/ops/taskers.py | 17 +++++++++++++++++ apps/ops/tasks.py | 36 +++++++++++++++++++++++++++--------- requirements.txt | 2 +- run_server.py | 2 +- 6 files changed, 49 insertions(+), 21 deletions(-) delete mode 100644 apps/ops/run_tasks.py create mode 100644 apps/ops/taskers.py diff --git a/apps/ops/api.py b/apps/ops/api.py index ecbf4289f..e36433172 100644 --- a/apps/ops/api.py +++ b/apps/ops/api.py @@ -1,3 +1,5 @@ # ~*~ coding: utf-8 ~*~ -# + +from __future__ import unicode_literals + diff --git a/apps/ops/run_tasks.py b/apps/ops/run_tasks.py deleted file mode 100644 index 4c3115fac..000000000 --- a/apps/ops/run_tasks.py +++ /dev/null @@ -1,9 +0,0 @@ -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 diff --git a/apps/ops/taskers.py b/apps/ops/taskers.py new file mode 100644 index 000000000..7a4f2e801 --- /dev/null +++ b/apps/ops/taskers.py @@ -0,0 +1,17 @@ +from __future__ import unicode_literals + +from .tasks import get_asset_hardware_info +from celery.result import AsyncResult + + +def start_get_hardware_info(*assets): + result = get_asset_hardware_info.delay(*assets) + return result.id + + +def get_hardware_info(task_id): + result = AsyncResult(task_id) + if result.ready(): + return {"Completed": False, "data": result.get()} + else: + return {"Completed": True, "data": None} diff --git a/apps/ops/tasks.py b/apps/ops/tasks.py index ae4b23f01..8d6a32312 100644 --- a/apps/ops/tasks.py +++ b/apps/ops/tasks.py @@ -1,17 +1,35 @@ -from __future__ import absolute_import -import time +from __future__ import absolute_import, unicode_literals from celery import shared_task from common import celery_app +from ops.ansible_api import Config, ADHocRunner -@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 + +@shared_task(name="get_asset_hardware_info") +def get_asset_hardware_info(*assets): + conf = Config() + play_source = { + "name": "Get host hardware information", + "hosts": "default", + "gather_facts": "no", + "tasks": [ + dict(action=dict(module='setup')) + ] + } + hoc = ADHocRunner(conf, play_source, *assets) + ext_code, result = hoc.run() + return ext_code, result + + +@shared_task(name="asset_test_ping_check") +def asset_test_ping_check(): + pass + + +@shared_task(name="add_user_to_assert") +def add_user_to_asset(): + pass @celery_app.task(name='hello-world') diff --git a/requirements.txt b/requirements.txt index 017720c87..12c2e48a4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -23,4 +23,4 @@ sshpubkeys==2.2.0 djangorestframework-bulk==0.2.1 python-gssapi==0.6.4 tornado==4.4.2 - +eventlet==0.19.0 diff --git a/run_server.py b/run_server.py index e1d3240c3..da9daeb7f 100644 --- a/run_server.py +++ b/run_server.py @@ -29,7 +29,7 @@ def start_django(): def start_celery(): os.chdir(apps_dir) print('start celery') - subprocess.call('celery -A common worker -l info', shell=True) + subprocess.call('celery -A common worker -P eventlet -l info', shell=True) def main():