-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathconfig.py
More file actions
486 lines (401 loc) · 13.9 KB
/
Copy pathconfig.py
File metadata and controls
486 lines (401 loc) · 13.9 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
import argparse
import os
import sys
import re
from .version import VERSION
from .utils import get_local_ip
import atexit
def add_parser_args(parser, config_type):
# General arguments
parser.add_argument(
'--trace_greenlets',
action='store_true',
default=False,
help='Collect stats about each greenlet execution time and switches.')
parser.add_argument(
'--trace_memory',
action='store_true',
default=False,
help='Collect stats about memory for each task. Incompatible with --greenlets > 1')
parser.add_argument(
'--trace_io',
action='store_true',
default=False,
help='Collect stats about all I/O operations')
parser.add_argument(
'--print_mongodb',
action='store_true',
default=False,
help='Print all MongoDB requests')
parser.add_argument(
'--trace_memory_type',
action='store',
default="",
help='Create a .png object graph in trace_memory_output_dir ' +
'with a random object of this type.')
parser.add_argument(
'--trace_memory_output_dir',
action='store',
default="memory_traces",
help='Directory where to output .pngs with object graphs')
parser.add_argument(
'--profile',
action='store_true',
default=False,
help='Run profiling on the whole worker')
parser.add_argument(
'--mongodb_jobs', '--mongodb',
action='store',
default="mongodb://127.0.0.1:27017/mrq",
help='MongoDB URI for the jobs, scheduled_jobs & workers database')
parser.add_argument(
'--mongodb_logs',
action='store',
default="1",
help='MongoDB URI for the logs database. ' +
' "0" will disable remote logs, "1" will use main MongoDB.')
parser.add_argument(
'--mongodb_logs_size',
action='store',
default=16 *
1024 *
1024,
type=int,
help='If provided, sets the log collection to capped to that amount of bytes.')
parser.add_argument(
'--no_mongodb_ensure_indexes',
action='store_true',
default=False,
help='If provided, skip the creation of MongoDB indexes at worker startup.')
parser.add_argument(
'--redis',
action='store',
default="redis://127.0.0.1:6379",
help='Redis URI')
parser.add_argument(
'--redis_prefix',
action='store',
default="mrq",
help='Redis key prefix')
parser.add_argument(
'--redis_max_connections',
action='store',
type=int,
default=1000,
help='Redis max connection pool size')
parser.add_argument(
'--redis_timeout',
action='store',
type=float,
default=30,
help='Redis connection pool timeout to wait for an available connection')
parser.add_argument(
'--name',
default=None,
action='store',
help='Specify a different name')
parser.add_argument(
'--quiet',
default=False,
action='store_true',
help='Don\'t output task logs')
parser.add_argument(
'--config',
'-c',
default=None,
action="store",
help='Path of a config file')
parser.add_argument(
'--worker_class',
default="mrq.worker.Worker",
action="store",
help='Path to a custom worker class')
parser.add_argument(
'--version',
'-v',
default=False,
action="store_true",
help='Prints current MRQ version')
parser.add_argument(
'--no_import_patch',
default=False,
action='store_true',
help='(DEPRECATED) Skips patching __import__ to fix gevent bug #108')
parser.add_argument(
'--add_network_latency',
default="0",
action='store',
type=str,
help='Adds random latency to the network calls, zero to N seconds. Can be a range (1-2)')
parser.add_argument(
'--default_job_result_ttl',
default=7 * 24 * 3600,
action='store',
type=float,
help='Seconds the results are kept in MongoDB when status is success')
parser.add_argument(
'--default_job_abort_ttl',
default=24 * 3600,
action='store',
type=float,
help='Seconds the tasks are kept in MongoDB when status is abort')
parser.add_argument(
'--default_job_cancel_ttl',
default=24 * 3600,
action='store',
type=float,
help='Seconds the tasks are kept in MongoDB when status is cancel')
parser.add_argument(
'--default_job_timeout',
default=3600,
action='store',
type=float,
help='In seconds, delay before interrupting the job')
parser.add_argument(
'--default_job_max_retries',
default=3,
action='store',
type=int,
help='Set the status to "maxretries" after retrying that many times')
parser.add_argument(
'--default_job_retry_delay',
default=3,
action='store',
type=int,
help='Seconds before a job in retry status is requeued again')
parser.add_argument(
'--use_large_job_ids',
action='store_true',
default=False,
help='Do not use compacted job IDs in Redis. For compatibility with 0.1.x only')
# mrq-run-specific arguments
if config_type == "run":
parser.add_argument(
'--queue',
action='store',
default="",
help='Queue the task on this queue instead of running it right away')
parser.add_argument(
'taskpath',
action='store',
help='Task to run')
parser.add_argument(
'taskargs',
action='store',
default='{}',
nargs='*',
help='JSON-encoded arguments, or "key value" pairs')
# Dashboard-specific arguments
elif config_type == "dashboard":
parser.add_argument(
'--dashboard_httpauth',
default="",
action="store",
help='HTTP Auth for the Dashboard. Format is user:pass')
parser.add_argument(
'--dashboard_queue',
default=None,
action="store",
help='Default queue for dashboard actions.')
parser.add_argument(
'--dashboard_port',
default=5555,
action="store",
type=int,
help='Use this port for mrq-dashboard. 5555 by default.')
parser.add_argument(
'--dashboard_ip',
default="0.0.0.0",
action="store",
type=str,
help='Bind the dashboard to this IP. Default is "0.0.0.0", use "127.0.0.1" to restrict access.')
# Worker-specific args
elif config_type == "worker":
parser.add_argument(
'--max_jobs',
default=0,
type=int,
action='store',
help='Gevent: max number of jobs to do before quitting.' +
' Temp workaround for memory leaks')
parser.add_argument(
'--max_memory',
default=0,
type=int,
action='store',
help='Max memory (in Mb) after which the process will be shut down. Use with --processes [1-N]' +
'to have supervisord automatically respawn the worker when this happens')
parser.add_argument(
'--greenlets',
'--gevent', # deprecated
'-g',
default=1,
type=int,
action='store',
help='Max number of greenlets to use')
parser.add_argument(
'--processes',
'-p',
default=0,
type=int,
action='store',
help='Number of processes to launch with supervisord')
default_template = os.path.abspath(os.path.join(
os.path.dirname(__file__),
"supervisord_templates/default.conf"
))
parser.add_argument(
'--supervisord_template',
default=default_template,
action='store',
help='Path of supervisord template to use')
parser.add_argument(
'--scheduler',
default=False,
action='store_true',
help='Run the scheduler')
parser.add_argument(
'--scheduler_interval',
default=60,
action='store',
type=float,
help='Seconds between scheduler checks')
parser.add_argument(
'--report_interval',
default=10,
action='store',
type=float,
help='Seconds between worker reports to MongoDB')
parser.add_argument(
'--report_file',
default="",
action='store',
type=unicode,
help='Filepath of a json dump of the worker status. Disabled if none')
parser.add_argument(
'queues',
nargs='*',
default=["default"],
help='The queues to listen on (default: \'default\')')
parser.add_argument(
'--subqueues_refresh_interval',
default=60,
action='store',
type=float,
help="Seconds between worker refreshes of the known subqueues")
parser.add_argument(
'--subqueues_delimiter',
default='/',
help='Delimiter between main queue and subqueue names')
parser.add_argument(
'--admin_port',
default=0,
action="store",
type=int,
help='Start an admin server on this port, if provided. Incompatible with --processes')
parser.add_argument(
'--admin_ip',
default="127.0.0.1",
action="store",
type=str,
help='IP for the admin server to listen on. Use "0.0.0.0" to allow access from outside')
parser.add_argument(
'--local_ip',
default=get_local_ip(),
action="store",
type=str,
help='Overwrite the local IP, to be displayed in the dashboard.')
parser.add_argument(
'--max_latency',
default=1.,
type=float,
action='store',
help='Max seconds while worker may sleep waiting for a new job. ' +
'Can be < 1.')
parser.add_argument(
'--dequeue_strategy',
default="sequential",
type=str,
action='store',
help='Strategy for dequeuing multiple queues. Default is \'sequential\',' +
'to dequeue them in command-line order.')
def get_config(
sources=(
"file",
"env"),
env_prefix="MRQ_",
file_path=None,
parser=None,
extra=None,
config_type=None):
""" Returns a config dict merged from several possible sources """
if not parser:
parser = argparse.ArgumentParser()
add_parser_args(parser, config_type)
parser_types = {action.dest: action.type for action in parser._actions if action.dest}
if config_type in ["run"]:
default_config = parser.parse_args(["notask"]).__dict__
else:
default_config = parser.parse_args([]).__dict__
# Keys that can't be passed from the command line
default_config["tasks"] = {}
default_config["scheduled_tasks"] = {}
# Only keep values different from config, actually passed on the command
# line
from_args = {}
if "args" in sources:
for k, v in parser.parse_args().__dict__.iteritems():
if default_config[k] != v:
from_args[k] = v
# If we were given another config file, use it
if file_path is not None:
config_file = file_path
elif from_args.get("config"):
config_file = from_args.get("config")
# If a mrq-config.py file is in the current directory, use it!
elif os.path.isfile(os.path.join(os.getcwd(), "mrq-config.py")):
config_file = os.path.join(os.getcwd(), "mrq-config.py")
else:
config_file = None
from_file = {}
if config_file and "file" in sources:
sys.path.insert(0, os.path.dirname(config_file))
config_module = __import__(os.path.basename(config_file.replace(".py", "")))
sys.path.pop(0)
for k, v in config_module.__dict__.iteritems():
# We only keep variables starting with an uppercase character.
if k[0].isupper():
from_file[k.lower()] = v
# Merge the config in the order given by the user
merged_config = default_config
config_keys = set(default_config.keys() + from_file.keys())
for part in sources:
for name in config_keys:
if part == "env":
value = os.environ.get(env_prefix + name.upper())
if value:
if name == "queues":
value = re.split("\s+", value)
if parser_types.get(name):
value = parser_types[name](value)
merged_config[name] = value
elif part == "args" and name in from_args:
merged_config[name] = from_args[name]
elif part == "file" and name in from_file:
merged_config[name] = from_file[name]
if extra:
merged_config.update(extra)
if merged_config["profile"]:
import cProfile
profiler = cProfile.Profile()
profiler.enable()
def print_profiling():
profiler.print_stats(sort="cumulative")
atexit.register(print_profiling)
if merged_config["version"]:
print "MRQ version: %s" % VERSION
print "Python version: %s" % sys.version
sys.exit(1)
if "no_import_patch" in from_args:
print "WARNING: --no_import_patch will be deprecated in MRQ 1.0!"
return merged_config