Browse Source

views.apply: View applying a task. Example:
http://e.com/celery/apply/task_name/arg1/arg2//?kwarg1=a&kwarg2=b

**NOTE** Use with caution, preferably not make this publicly accessible
without ensuring your code is safe!

Ask Solem 16 years ago
parent
commit
91b5a7e623
1 changed files with 24 additions and 2 deletions
  1. 24 2
      celery/views.py

+ 24 - 2
celery/views.py

@@ -1,10 +1,32 @@
 """celery.views"""
-from django.http import HttpResponse
-from celery.task import is_done, delay_task
+from django.http import HttpResponse, Http404
+from celery.task import tasks, is_done, apply_async
 from celery.result import AsyncResult
 from carrot.serialization import serialize as JSON_dump
 
 
+def apply(request, task_name, *args):
+    """View applying a task.
+
+    Example:
+        http://e.com/celery/apply/task_name/arg1/arg2//?kwarg1=a&kwarg2=b
+
+    **NOTE** Use with caution, preferably not make this publicly accessible 
+    without ensuring your code is safe!
+
+    """
+    kwargs = request.method == "POST" and \
+            request.POST.copy() or request.GET.copy()
+    kwargs = [(k.encode("utf-8"), v)
+                    for k,v in kwargs.items()]
+    if task_name not in tasks:
+        raise Http404("apply: no such task")
+        
+    task = tasks[task_name]
+    result = apply_async(task, args=args, kwargs=kwargs)
+    return JSON_dump({"ok": "true", "task_id": result.task_id
+
+
 def is_task_done(request, task_id):
     """Returns task execute status in JSON format."""
     response_data = {"task": {"id": task_id, "executed": is_done(task_id)}}