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:

    1. {'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:

    1. from anyjson import serialize
    2. def multiply(request):
    3. x = int(request.GET['x'])
    4. y = int(request.GET['y'])
    5. response = {'status': 'success', 'retval': result}
    6. return HttpResponse(serialize(response), mimetype='application/json')
    1. def multiply
    2. @x = params[:x].to_i
    3. @y = params[:y].to_i
    4. @status = {:status => 'success', :retval => @x * @y}
    5. 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.

    1. >>> from celery.task.http import HttpDispatchTask
    2. >>> res = HttpDispatchTask.delay(
    3. ... url='http://example.com/multiply',
    4. ... method='GET', x=10, y=10)
    5. >>> res.get()
    6. 100

    The output of celery worker (or the log file if enabled) should show the task being executed:

    1. [INFO/MainProcess] Task celery.task.http.HttpDispatchTask