2011-11-17 23:51:34 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
import optparse
|
2011-11-24 23:46:19 +00:00
|
|
|
import logbook
|
|
|
|
from logbook import handlers
|
2011-11-17 23:51:34 +00:00
|
|
|
from rq import use_redis, Queue, Worker
|
|
|
|
|
2011-11-18 00:44:28 +00:00
|
|
|
|
2011-11-17 23:51:34 +00:00
|
|
|
def parse_args():
|
|
|
|
parser = optparse.OptionParser()
|
|
|
|
parser.add_option('-b', '--burst', dest='burst',
|
|
|
|
action='store_true', default=False,
|
|
|
|
help='Run in burst mode (quit after all work is done).')
|
|
|
|
parser.add_option('-n', '--name', dest='name',
|
|
|
|
action='store', type='string', default=None,
|
|
|
|
help='Specify a different name.')
|
2011-11-24 23:46:19 +00:00
|
|
|
parser.add_option('-v', '--verbose', dest='verbose',
|
|
|
|
action='store_true', default=False,
|
|
|
|
help='Show more output.')
|
2011-11-17 23:51:34 +00:00
|
|
|
opts, args = parser.parse_args()
|
|
|
|
return (opts, args, parser)
|
|
|
|
|
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))
|
|
|
|
|
|
|
|
|
|
|
|
def setup_loghandlers(opts):
|
|
|
|
if opts.verbose:
|
|
|
|
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-17 23:51:34 +00:00
|
|
|
def main():
|
|
|
|
opts, args, parser = parse_args()
|
|
|
|
|
|
|
|
use_redis()
|
2011-11-24 23:46:19 +00:00
|
|
|
setup_loghandlers(opts)
|
2011-11-17 23:51:34 +00:00
|
|
|
|
|
|
|
if len(args) == 0:
|
|
|
|
# Use the default queue
|
|
|
|
queues = [Queue()]
|
|
|
|
else:
|
|
|
|
queues = map(Queue, args)
|
|
|
|
|
|
|
|
w = Worker(queues, name=opts.name)
|
2011-11-26 08:26:13 +00:00
|
|
|
w.work(burst=opts.burst)
|
2011-11-17 23:51:34 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|