remote-tasks.rst 2.7 KB

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