remote-tasks.rst 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. ================================
  2. HTTP Callback Tasks (Webhooks)
  3. ================================
  4. .. module:: celery.task.http
  5. .. contents::
  6. :local:
  7. Basics
  8. ======
  9. If you need to call into another language, framework or similar, you can
  10. do so by using HTTP callback tasks.
  11. The HTTP callback tasks use GET/POST arguments and a simple JSON response
  12. to return results. The scheme to call a task is::
  13. GET http://example.com/mytask/?arg1=a&arg2=b&arg3=c
  14. or using POST::
  15. POST http://example.com/mytask
  16. **Note:** POST data has to be form encoded.
  17. Whether to use GET or POST is up to you and your requirements.
  18. The web page should then return a response in the following format
  19. if the execution was successful::
  20. {"status": "success", "retval": ....}
  21. or if there was an error::
  22. {"status": "failure": "reason": "Invalid moon alignment."}
  23. Django webhook example
  24. ======================
  25. With this information you could define a simple task in Django:
  26. .. code-block:: python
  27. from django.http import HttpResponse
  28. from anyjson import serialize
  29. def multiply(request):
  30. x = int(request.GET["x"])
  31. y = int(request.GET["y"])
  32. result = x * y
  33. response = {"status": "success", "retval": result}
  34. return HttpResponse(serialize(response), mimetype="application/json")
  35. Ruby on Rails webhook example
  36. =============================
  37. or in Ruby on Rails:
  38. .. code-block:: ruby
  39. def multiply
  40. @x = params[:x].to_i
  41. @y = params[:y].to_i
  42. @status = {:status => "success", :retval => @x * @y}
  43. render :json => @status
  44. end
  45. You can easily port this scheme to any language/framework;
  46. new examples and libraries are very welcome.
  47. Executing webhook tasks
  48. =======================
  49. To execute the task you use the :class:`URL` class:
  50. >>> from celery.task.http import URL
  51. >>> res = URL("http://example.com/multiply").get_async(x=10, y=10)
  52. :class:`URL` is a shortcut to the :class:`HttpDispatchTask`. You can subclass this to extend the
  53. functionality.
  54. >>> from celery.task.http import HttpDispatchTask
  55. >>> res = HttpDispatchTask.delay(url="http://example.com/multiply", method="GET", x=10, y=10)
  56. >>> res.get()
  57. 100
  58. The output of celeryd (or the logfile if you've enabled it) should show the task being processed::
  59. [INFO/MainProcess] Task celery.task.http.HttpDispatchTask
  60. [f2cc8efc-2a14-40cd-85ad-f1c77c94beeb] processed: 100
  61. Since applying tasks can be done via HTTP using the
  62. ``celery.views.apply`` view, executing tasks from other languages is easy.
  63. For an example service exposing tasks via HTTP you should have a look at
  64. ``examples/celery_http_gateway``.