Browse Source

Added cookbook entry: Executing tasks on a remote web server

Ask Solem 15 years ago
parent
commit
79599385e6
2 changed files with 61 additions and 0 deletions
  1. 1 0
      docs/cookbook/index.rst
  2. 60 0
      docs/cookbook/remote-tasks.rst

+ 1 - 0
docs/cookbook/index.rst

@@ -7,6 +7,7 @@
 
     tasks
     task-retries
+    remote-tasks
 
 This page contains common recipes and techniques.
 Whenever a setting is mentioned, you should use ``celeryconf.py`` if using

+ 60 - 0
docs/cookbook/remote-tasks.rst

@@ -0,0 +1,60 @@
+==============
+ Remote Tasks 
+==============
+
+
+Executing tasks on a remote web server
+--------------------------------------
+
+If you need to call into another language, framework or similar, you can
+do so by using HTTP tasks.
+
+The HTTP tasks (or REST task) uses a simple REST+JSON scheme to take arguments
+and return results, the scheme to call a task is::
+
+    GET http://example.com/mytask/?arg1=a,arg2=b,arg3=c
+
+The web page should then return a response in the following format
+if the execution was successful::
+
+    {"status": "success", "retval": ....}
+
+or in the following format if there was an error::
+
+    {"status": "failure": "reason": "Invalid moon alignment."}
+
+
+With this information we can define a simple task in Django:
+
+.. code-block:: python
+
+    from django.http import HttpResponse
+    from anyjson import serialize
+
+
+    def multiply(request):
+        x = int(request.GET["x"])
+        y = int(request.GET["y"])
+        result = x * y
+        response = {"status": "success", "retval": result}
+        return HttpResponse(serialize(response), mimetype="application/json")
+
+I'm sure you'll be able to port this scheme to any language and framework,
+and maybe if you're lucky someone has already made a library for it.
+
+
+To execute the task you use :class:`celery.task.rest.RESTProxyTask`:
+
+    >>> from celery.task import RESTProxyTask
+    >>> res = RESTProxyTask.delay("http://example.com/multiply", x=10, y=10)
+    >>> res.get()
+    100
+
+In your ``celeryd.log`` file you should see the task being processed::
+
+    [INFO/MainProcess] Task celery.task.rest.RESTProxyTask
+        [f2cc8efc-2a14-40cd-85ad-f1c77c94beeb] processed: 100
+
+Since applying tasks can also simply be done via the web and the
+``celery.views.apply`` view, executing tasks from other languages should be a
+no-brainer.