HTTP Callback Tasks (Webhooks)
The HTTP callback tasks uses GET/POST data to pass arguments and returns result as a JSON response. The scheme to call a task is:
or using POST:
注解
POST data needs to be form encoded.
The web page should then return a response in the following format if the execution was successful:
{'status': 'success', 'retval': …}
or if there was an error:
To enable the HTTP dispatch task you have to add to CELERY_IMPORTS, or start the worker with -I celery.task.http.
With this information you could define a simple task in Django:
from anyjson import serialize
def multiply(request):
x = int(request.GET['x'])
y = int(request.GET['y'])
response = {'status': 'success', 'retval': result}
return HttpResponse(serialize(response), mimetype='application/json')
def multiply
@x = params[:x].to_i
@y = params[:y].to_i
@status = {:status => 'success', :retval => @x * @y}
end
You can easily port this scheme to any language/framework; new examples and libraries are very welcome.
To call a task you can use the class:
URL is a shortcut to the HttpDispatchTask. You can subclass this to extend the functionality.
>>> from celery.task.http import HttpDispatchTask
>>> res = HttpDispatchTask.delay(
... url='http://example.com/multiply',
... method='GET', x=10, y=10)
>>> res.get()
100
The output of celery worker (or the log file if enabled) should show the task being executed:
[INFO/MainProcess] Task celery.task.http.HttpDispatchTask