remote-tasks.rst 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. .. code-block:: http
  16. GET /mytask/?arg1=a&arg2=b&arg3=c HTTP/1.1
  17. Host: example.com
  18. or using POST:
  19. .. code-block:: http
  20. POST /mytask HTTP/1.1
  21. Host: example.com
  22. .. note::
  23. POST data needs to be form encoded.
  24. Whether to use GET or POST is up to you and your requirements.
  25. The web page should then return a response in the following format
  26. if the execution was successful:
  27. .. code-block:: json
  28. {"status": "success", "retval": "RETVAL"}
  29. or if there was an error:
  30. .. code-block:: json
  31. {"status": "failure", "reason": "Invalid moon alignment."}
  32. Enabling the HTTP task
  33. ----------------------
  34. To enable the HTTP dispatch task you have to add :mod:`celery.task.http`
  35. to :setting:`imports`, or start the worker with
  36. :option:`-I celery.task.http <celery worker -I>`.
  37. .. _webhook-django-example:
  38. Django webhook example
  39. ======================
  40. With this information you could define a simple task in Django:
  41. .. code-block:: python
  42. from django.http import HttpResponse
  43. from json import dumps
  44. def multiply(request):
  45. x = int(request.GET['x'])
  46. y = int(request.GET['y'])
  47. result = x * y
  48. response = {'status': 'success', 'retval': result}
  49. return HttpResponse(dumps(response), mimetype='application/json')
  50. .. _webhook-rails-example:
  51. Ruby on Rails webhook example
  52. =============================
  53. or in Ruby on Rails:
  54. .. code-block:: ruby
  55. def multiply
  56. @x = params[:x].to_i
  57. @y = params[:y].to_i
  58. @status = {:status => 'success', :retval => @x * @y}
  59. render :json => @status
  60. end
  61. You can easily port this scheme to any language/framework;
  62. new examples and libraries are very welcome.
  63. .. _webhook-calling:
  64. Calling webhook tasks
  65. =====================
  66. To call a task you can use the :class:`~celery.task.http.URL` class:
  67. .. code-block:: pycon
  68. >>> from celery.task.http import URL
  69. >>> res = URL('http://example.com/multiply').get_async(x=10, y=10)
  70. :class:`~celery.task.http.URL` is a shortcut to the :class:`HttpDispatchTask`.
  71. You can subclass this to extend the
  72. functionality:
  73. .. code-block:: pycon
  74. >>> from celery.task.http import HttpDispatchTask
  75. >>> res = HttpDispatchTask.delay(
  76. ... url='http://example.com/multiply',
  77. ... method='GET', x=10, y=10)
  78. >>> res.get()
  79. 100
  80. The output of :program:`celery worker` (or the log file if enabled) should show the
  81. task being executed:
  82. .. code-block:: text
  83. [INFO/MainProcess] Task celery.task.http.HttpDispatchTask
  84. [f2cc8efc-2a14-40cd-85ad-f1c77c94beeb] processed: 100
  85. Since calling tasks can be done via HTTP using the
  86. :func:`djcelery.views.apply` view, calling tasks from other languages is easy.
  87. For an example service exposing tasks via HTTP you should have a look at
  88. `examples/celery_http_gateway` in the Celery distribution:
  89. https://github.com/celery/celery/tree/master/examples/celery_http_gateway/