/
bottlehotqueue.py
55 lines (48 loc) · 1.75 KB
/
bottlehotqueue.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import inspect
from functools import wraps
try:
import simplejson as json
except ImportError:
import json
import hotqueue
import redis
class HotQueuePlugin(object):
name = 'hotqueue'
api = 2
def __init__(self, host='localhost', port=6379, database=0,
keyword='queue', asjson=True, prefix="hotqueue"):
self.keyword = keyword
if asjson is True:
self.asjson = json
else:
self.asjson = asjson or None
hotqueue.key_for_name = lambda x: ''.join([prefix, ":", x])
self.host = host
self.port = port
self.database = database
def setup(self, app):
for other in app.plugins:
if not isinstance(other, HotQueuePlugin):
continue
if other.keyword == self.keyword:
raise PluginError("Found another hotqueue plugin with "\
"conflicting settings (non-unique keyword).")
self.redispool = redis.ConnectionPool(host=self.host,
port=self.port, db=self.database)
def apply(self, callback, route):
conf = route.config.get(self.keyword, {})
if not 'queue' in conf:
keyword = self.keyword
else:
keyword = conf['queue']
args = inspect.getargspec(route.callback)[0]
if keyword not in args:
return route.callback
@wraps(route.callback)
def wrapper(*args, **kwargs):
queue = hotqueue.HotQueue(keyword, serializer=self.asjson,
connection_pool=self.redispool)
kwargs[keyword] = queue
return route.callback(*args, **kwargs)
return wrapper
Plugin = HotQueuePlugin