2011-11-17 23:51:34 +00:00
|
|
|
#!/usr/bin/env python
|
2011-12-13 16:09:58 +00:00
|
|
|
import sys
|
2011-11-28 14:04:24 +00:00
|
|
|
import argparse
|
2011-11-24 23:46:19 +00:00
|
|
|
import logbook
|
2011-11-28 20:02:42 +00:00
|
|
|
import redis
|
2011-11-24 23:46:19 +00:00
|
|
|
from logbook import handlers
|
2012-03-23 14:15:29 +00:00
|
|
|
from rq import use_connection, Queue, Worker
|
2011-11-28 20:02:42 +00:00
|
|
|
from redis.exceptions import ConnectionError
|
2011-11-17 23:51:34 +00:00
|
|
|
|
2011-11-18 00:44:28 +00:00
|
|
|
|
2011-11-24 23:46:19 +00:00
|
|
|
def format_colors(record, handler):
|
|
|
|
from rq.utils import make_colorizer
|
|
|
|
if record.level == logbook.WARNING:
|
|
|
|
colorize = make_colorizer('darkyellow')
|
|
|
|
elif record.level >= logbook.ERROR:
|
|
|
|
colorize = make_colorizer('darkred')
|
|
|
|
else:
|
|
|
|
colorize = lambda x: x
|
|
|
|
return '%s: %s' % (record.time.strftime('%H:%M:%S'), colorize(record.msg))
|
|
|
|
|
2011-11-28 20:02:42 +00:00
|
|
|
|
2011-11-28 14:04:24 +00:00
|
|
|
def setup_loghandlers(args):
|
|
|
|
if args.verbose:
|
2011-11-24 23:46:19 +00:00
|
|
|
loglevel = logbook.DEBUG
|
|
|
|
formatter = None
|
|
|
|
else:
|
|
|
|
loglevel = logbook.INFO
|
|
|
|
formatter = format_colors
|
|
|
|
|
|
|
|
import sys
|
|
|
|
handlers.NullHandler(bubble=False).push_application()
|
|
|
|
handler = handlers.StreamHandler(sys.stdout, level=loglevel, bubble=False)
|
|
|
|
if formatter:
|
|
|
|
handler.formatter = formatter
|
|
|
|
handler.push_application()
|
|
|
|
handler = handlers.StderrHandler(level=logbook.WARNING, bubble=False)
|
|
|
|
if formatter:
|
|
|
|
handler.formatter = formatter
|
|
|
|
handler.push_application()
|
|
|
|
|
|
|
|
|
2011-11-28 14:04:24 +00:00
|
|
|
def parse_args():
|
|
|
|
parser = argparse.ArgumentParser(description='Starts an RQ worker.')
|
|
|
|
parser.add_argument('--host', '-H', default='localhost', help='The Redis hostname (default: localhost)')
|
|
|
|
parser.add_argument('--port', '-p', type=int, default=6379, help='The Redis portnumber (default: 6379)')
|
2011-11-28 20:02:42 +00:00
|
|
|
parser.add_argument('--db', '-d', type=int, default=0, help='The Redis database (default: 0)')
|
2011-11-28 14:04:24 +00:00
|
|
|
|
|
|
|
parser.add_argument('--burst', '-b', action='store_true', default=False, help='Run in burst mode (quit after all work is done)')
|
|
|
|
parser.add_argument('--name', '-n', default=None, help='Specify a different name')
|
2011-12-13 16:09:58 +00:00
|
|
|
parser.add_argument('--path', '-P', default='.', help='Specify the import path.')
|
2011-11-28 14:04:24 +00:00
|
|
|
parser.add_argument('--verbose', '-v', action='store_true', default=False, help='Show more output')
|
|
|
|
parser.add_argument('queues', nargs='*', default=['default'], help='The queues to listen on (default: \'default\')')
|
|
|
|
|
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
|
2011-11-17 23:51:34 +00:00
|
|
|
def main():
|
2011-11-28 14:04:24 +00:00
|
|
|
args = parse_args()
|
2011-11-17 23:51:34 +00:00
|
|
|
|
2011-12-13 16:09:58 +00:00
|
|
|
if args.path:
|
|
|
|
sys.path = args.path.split(':') + sys.path
|
|
|
|
|
2011-11-28 14:04:24 +00:00
|
|
|
setup_loghandlers(args)
|
2011-11-17 23:51:34 +00:00
|
|
|
|
2011-11-28 20:02:42 +00:00
|
|
|
# Setup connection to Redis
|
|
|
|
redis_conn = redis.Redis(host=args.host, port=args.port, db=args.db)
|
2012-03-23 14:15:29 +00:00
|
|
|
use_connection(redis_conn)
|
2011-11-28 20:02:42 +00:00
|
|
|
try:
|
|
|
|
queues = map(Queue, args.queues)
|
|
|
|
w = Worker(queues, name=args.name)
|
|
|
|
w.work(burst=args.burst)
|
|
|
|
except ConnectionError as e:
|
|
|
|
print(e)
|
|
|
|
|
2011-11-17 23:51:34 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|