remote-tasks.rst 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. Enabling the HTTP task
  27. ----------------------
  28. To enable the HTTP dispatch task you have to add :mod:`celery.task.http`
  29. to :setting:`CELERY_IMPORTS`, or start the worker with ``-I
  30. celery.task.http``.
  31. .. _webhook-django-example:
  32. Django webhook example
  33. ======================
  34. With this information you could define a simple task in Django:
  35. .. code-block:: python
  36. from django.http import HttpResponse
  37. from anyjson import serialize
  38. def multiply(request):
  39. x = int(request.GET['x'])
  40. y = int(request.GET['y'])
  41. result = x * y
  42. response = {'status': 'success', 'retval': result}
  43. return HttpResponse(serialize(response), mimetype='application/json')
  44. .. _webhook-rails-example:
  45. Ruby on Rails webhook example
  46. =============================
  47. or in Ruby on Rails:
  48. .. code-block:: ruby
  49. def multiply
  50. @x = params[:x].to_i
  51. @y = params[:y].to_i
  52. @status = {:status => 'success', :retval => @x * @y}
  53. render :json => @status
  54. end
  55. You can easily port this scheme to any language/framework;
  56. new examples and libraries are very welcome.
  57. .. _webhook-calling:
  58. Calling webhook tasks
  59. =====================
  60. To call a task you can use the :class:`~celery.task.http.URL` class:
  61. >>> from celery.task.http import URL
  62. >>> res = URL('http://example.com/multiply').get_async(x=10, y=10)
  63. :class:`~celery.task.http.URL` is a shortcut to the :class:`HttpDispatchTask`.
  64. You can subclass this to extend the
  65. functionality.
  66. >>> from celery.task.http import HttpDispatchTask
  67. >>> res = HttpDispatchTask.delay(
  68. ... url='http://example.com/multiply',
  69. ... method='GET', x=10, y=10)
  70. >>> res.get()
  71. 100
  72. The output of :program:`celery worker` (or the log file if enabled) should show the
  73. task being executed::
  74. [INFO/MainProcess] Task celery.task.http.HttpDispatchTask
  75. [f2cc8efc-2a14-40cd-85ad-f1c77c94beeb] processed: 100
  76. Since calling tasks can be done via HTTP using the
  77. :func:`djcelery.views.apply` view, calling tasks from other languages is easy.
  78. For an example service exposing tasks via HTTP you should have a look at
  79. `examples/celery_http_gateway` in the Celery distribution:
  80. http://github.com/celery/celery/tree/master/examples/celery_http_gateway/