Skip to main content

A web hook task queue based on tornado and redis

Project description

Overview

Torque is a web hook task queue based on Tornado and redis. It’s designed to solve two problems in the context of a web application:

  1. you want to do something later

  2. you want to do a number of things in parallel

There are many ways of approaching these problems. For example, in python, you might look at Twisted, Celery and Stackless.

Torque is inspired by Google App Engine’s taskqueue, which models tasks as webhooks. This approach allows you to handle tasks within your normal web application environment by writing request handlers, just as you would to handle a user initiated request.

To use it, you need to run:

  1. a redis database

  2. ./bin/torque-serve, which exposes a Tornado application (by default on http://localhost:8889)

  3. one ./bin/torque-process per queue

Or, if you’re just using the default queue, you can replace ./bin/torque-serve and ./bin/torque-process with:

  1. ./bin/torque-run

Which runs the web application and the process loop in seperate threads.

You can process queues ad infinitum, or until they are empty. See torque.process.QueueProcessor.__doc__ for the details.

You can add tasks to the queue in two ways:

  1. by posting an HTTP request to the Tornado application run by ./bin/torque-serve

  2. or by using the python client api in torque.client

This first method allows you to use Torque from any programming language. The second makes it much simpler if you’re using python.

To add a task using an HTTP request, post to /add_task with two params:

  • url which is the url to the webhook you want the task to request

  • params which is a json encoded dictionary of the params you want to post to the webhook you’re requesting

An example in python (with the Tornado application available on localhost, running on port 8889) would be:

import json
import urllib

mytask = {
    'url': 'http://mywebservice.com/hooks/do/foo',
    'params': json.dumps({'foo', 'somevalue', 'baz': 99})
}
target_url = 'http://localhost:8889/add_task'
urllib.urlopen(target_url, urllib.urlencode(mytask))

This queued a POST request to http://mywebservice.com/hooks/do/foo with the params foo=somevalue and baz=99 to be made as soon as possible.

You can do something similar using any programming language that can make url requests. However, if you are using python, it’s much simpler to use the client api that Torque provides:

from torque.client import add_task
t = add_task(url='http://mywebservice.com/hooks/do/foo', params={'a': 1})

Note that this doesn’t require json encoding the params. For all the client api options, see torque.client.Task.__doc__.

Individual tasks backoff exponentially if they error, upto a maximum backoff delay that’s configurable as --max_task_delay, until they error --max_task_errors times (at which point they get deleted).

Install

Install the redis and Tornado dependencies. Then install Torque:

$ easy_install torque

Or manually from source:

$ git clone git://github.com/thruflo/torque.git
$ cd torque
$ python setup.py install

Run

Run redis:

$ ./redis-server

Start the Tornado application:

$ ./bin/torque-serve

If you want to run the tests, use:

$ ./bin/nosetests -w ./src/torque --with-doctest
.......
----------------------------------------------------------------------
Ran 7 tests in 22.627s

OK

Start the default task queue running ad infinitum:

$ ./bin/torque-process

See --help against either of the torque console scripts for a list of configuration options. For example, to run a second queue called foobar, you might use:

./bin/torque-process --queue_name=foobar

Or to process the default queue once until empty you might use:

./bin/torque-process --finish_on_empty=true --max_task_errors=3

Or to do exactly the same from python code:

from torque.processor import QueueProcessor
QueueProcessor(max_task_errors=3).process(finish_on_empty=true)

Read the source code for more information.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

torque-0.4.5.tar.gz (17.8 kB view details)

Uploaded Source

File details

Details for the file torque-0.4.5.tar.gz.

File metadata

  • Download URL: torque-0.4.5.tar.gz
  • Upload date:
  • Size: 17.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for torque-0.4.5.tar.gz
Algorithm Hash digest
SHA256 8a45d852e04428decf8f991d3da56497ea1435454cf61f1054c8b049649cf261
MD5 1b4e8118f06ae2a6be43278b29320e62
BLAKE2b-256 b7edf0d034945991052b2e5858c4133622463089b169c5747dd769fede305018

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page