Skip to content
Browse files

Reuse tornado json utilities

  • Loading branch information...
1 parent 6e402b1 commit 713c057ac7b1d7e8b5a411459b90a6cba314915e @mher committed May 19, 2012
Showing with 12 additions and 23 deletions.
  1. +12 −18 tcelery/handlers.py
  2. +0 −5 tcelery/utils.py
View
30 tcelery/handlers.py
@@ -7,29 +7,23 @@
from tornado import web
from tornado import ioloop
from tornado.options import options as tornado_options
+from tornado.escape import json_decode
from celery.result import AsyncResult
from celery.task.control import inspect
from celery.task.control import revoke
from celery.execute import send_task
-from .utils import route, to_json
+from .utils import route
class ApplyHandlerBase(web.RequestHandler):
registered_tasks = []
def get_task_args(self):
- options = dict(self.request.arguments)
+ options = json_decode(self.request.body)
args = options.pop('args', [])
kwargs = options.pop('kwargs', {})
- for o, v in options.iteritems():
- options[o] = v[0]
- try:
- options[o] = float(v[0])
- options[o] = int(v[0])
- except:
- pass
return args, kwargs, options
@classmethod
@@ -42,7 +36,7 @@ def get_registered_tasks(cls):
cls.registered_tasks = set(tasks)
return cls.registered_tasks
-
+
@route('/async-apply/(.*)/')
class AsyncApplyHandler(ApplyHandlerBase):
@@ -52,7 +46,7 @@ def post(self, taskname):
args, kwargs, options = self.get_task_args()
result = send_task(taskname, args=args, kwargs=kwargs)
- self.write(to_json({'task-id': result.task_id, 'state': result.state}))
+ self.write({'task-id': result.task_id, 'state': result.state})
@route('/tasks/result/(.*)/')
@@ -62,14 +56,14 @@ def get(self, task_id):
response = {'task-id': task_id, 'state': result.state}
if result.ready():
response.update({'result': result.result})
- self.write(to_json(response))
+ self.write(response)
@route('/tasks/revoke/(.*)/')
class TaskRevokeHandler(web.RequestHandler):
def delete(self, task_id):
revoke(task_id)
- self.write(to_json({'task-id': task_id}))
+ self.write({'task-id': task_id})
@route('/apply/(.*)/')
@@ -101,7 +95,7 @@ def on_complete(cls, task_id):
response = {'task-id': task_id, 'state': result.state}
if result.successful():
response.update({'result': result.result})
- handler.write(to_json(response))
+ handler.write(response)
if htimeout:
ioloop.IOLoop.instance().remove_timeout(htimeout)
handler.finish()
@@ -110,7 +104,7 @@ def on_complete(cls, task_id):
def on_time(cls, task_id):
result, handler, _ = cls.tasks.pop(task_id)
revoke(task_id)
- handler.write(to_json({'task-id': task_id, 'state': result.state}))
+ handler.write({'task-id': task_id, 'state': result.state})
handler.finish()
@@ -119,23 +113,23 @@ class RegisteredTaskHandler(web.RequestHandler):
def get(self, host):
host = [host] if host else None
i = inspect(host)
- self.write(to_json(i.registered()))
+ self.write(i.registered())
@route('/tasks/active/(.*)')
class ActiveTaskHandler(web.RequestHandler):
def get(self, host):
host = [host] if host else None
i = inspect(host)
- self.write(to_json(i.active()))
+ self.write(i.active())
@route('/tasks/scheduled/(.*)')
class ScheduledTaskHandler(web.RequestHandler):
def get(self, host):
host = [host] if host else None
i = inspect(host)
- self.write(to_json(i.active()))
+ self.write(i.active())
@route('/')
View
5 tcelery/utils.py
@@ -1,11 +1,6 @@
-import json
import tornado.web
-to_json = lambda x: json.dumps(x)
-from_json = lambda x: json.loads(x)
-
-
class route(object):
"""route decorator from https://github.com/peterbe/tornado-utils"""
_routes = []

0 comments on commit 713c057

Please sign in to comment.
Something went wrong with that request. Please try again.