remote-tasks.rst 3.3 KB

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