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 intended to provide a similar pattern to Google App Engine’s taskqueue.
To use it, you need to run a redis database, a console script that exposes a Tornado web application and one process per task queue. You can then add tasks to one or more queues, either using the python client api that torque provides or via an HTTP api (or indeed by adding them directly to the database).
Tasks consist of a url and some params. When a task is executed, torque will post the params to the url. If the task errors, it backs off steeply until it errors too many times, at which point it’s deleted.
Tasks are stored in a redis SortedSet. Tornado is used to execute tasks asyncronously, without blocking.
Install
Install the redis and Tornado dependencies. (n.b.: see ./etc/redis.tiger.patch if, like me, you’re still using OSX Tiger). Then install the torque egg:
$ python setup.py install
Run
Run redis:
$ ./redis-server
Start the Tornado application:
$ ./bin/torque-serve
Start the task queue:
$ ./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
Use
To add a task to the queue, 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/hooks/add' 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, you can use the client api that torque provides:
from torque.client import add_task add_task(url='http://mywebservice.com/hooks/do/foo', params={'a': 1})
Note that this doesn’t require json encoding the params. You can specify a delay for the task, so that it’s executed after (but not necessarily at) a number of seconds:
add_task(url='...', params={...}, delay=20) # will execute after 20 seconds
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).
See the source code for more info and options, or just run it and use it ;)
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
File details
Details for the file torque-0.3.1.tar.gz
.
File metadata
- Download URL: torque-0.3.1.tar.gz
- Upload date:
- Size: 12.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e6cb9e87989bc48938c1f01b3968734e2a76705a1971499adbadad14c8e2e3f0 |
|
MD5 | 1280f4ea031ab0ce9e23b130ab615e42 |
|
BLAKE2b-256 | a7524ab97c14da2c621576a73d1f8196c1cef96aafd8f22e30bf1374e13bcdbf |