remote-tasks.rst 2.9 KB

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