forked from wong2/xiaohuangji
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rqworker.py
executable file
·99 lines (80 loc) · 3.21 KB
/
rqworker.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python
import sys
import argparse
import logbook
from logbook import handlers
from rq import Queue, Worker
from redis.exceptions import ConnectionError
from rq.scripts import add_standard_arguments
from rq.scripts import setup_redis
from rq.scripts import read_config_file
from rq.scripts import setup_default_arguments
from controller import bots, getNotiData, self_match_pattern
from failure_handler import do_job_failure_handler_have_a_rest
from ai import plugin_modules
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(args):
if args.verbose:
loglevel = logbook.DEBUG
formatter = None
else:
loglevel = logbook.INFO
formatter = format_colors
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()
def parse_args():
parser = argparse.ArgumentParser(description='Starts an RQ worker.')
add_standard_arguments(parser)
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')
parser.add_argument('--path', '-P', default='.', help='Specify the import path.')
parser.add_argument('--verbose', '-v', action='store_true', default=False, help='Show more output')
parser.add_argument('--sentry-dsn', action='store', default=None, metavar='URL', help='Report exceptions to this Sentry DSN')
parser.add_argument('queues', nargs='*', default=['default'], help='The queues to listen on (default: \'default\')')
return parser.parse_args()
def main():
args = parse_args()
if args.path:
sys.path = args.path.split(':') + sys.path
settings = {}
if args.config:
settings = read_config_file(args.config)
setup_default_arguments(args, settings)
# Other default arguments
if args.sentry_dsn is None:
args.sentry_dsn = settings.get('SENTRY_DSN', None)
setup_loghandlers(args)
setup_redis(args)
try:
queues = map(Queue, args.queues)
w = Worker(queues, name=args.name)
w.push_exc_handler(do_job_failure_handler_have_a_rest)
# Should we configure Sentry?
if args.sentry_dsn:
from raven import Client
from rq.contrib.sentry import register_sentry
client = Client(args.sentry_dsn)
register_sentry(client, w)
w.work(burst=args.burst)
except ConnectionError as e:
print(e)
sys.exit(1)
if __name__ == '__main__':
reload(sys)
sys.setdefaultencoding(sys.getfilesystemencoding())
main()