-
Notifications
You must be signed in to change notification settings - Fork 11
/
dispatcher.py
97 lines (81 loc) · 2.75 KB
/
dispatcher.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2012-2019
#
# Author(s):
#
# Martin Raspaud <martin.raspaud@smhi.se>
# Panu Lahtinen <panu.lahtinen@fmi.fi>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Dispatcher.
The configuration file is watched with inotify. If needed, the reload of the
configuration file can be triggered with a `kill -10 <dispatcher pid>`.
"""
import argparse
import logging
import logging.handlers
import os
import sys
from trollmoves.dispatcher import Dispatcher
LOG_FORMAT = "[%(asctime)s %(levelname)-8s] %(message)s"
logger = logging.getLogger('dispatcher')
log_levels = {
0: logging.WARN,
1: logging.INFO,
2: logging.DEBUG,
}
def setup_logging(cmd_args):
"""Set up logging."""
root = logging.getLogger('')
root.setLevel(log_levels[cmd_args.verbosity])
if cmd_args.log:
fh_ = logging.handlers.TimedRotatingFileHandler(
os.path.join(cmd_args.log),
"midnight",
backupCount=7)
else:
fh_ = logging.StreamHandler()
formatter = logging.Formatter(LOG_FORMAT)
fh_.setFormatter(formatter)
root.addHandler(fh_)
def main():
"""Start and run the dispatcher."""
parser = argparse.ArgumentParser()
parser.add_argument("config_file",
help="The configuration file to run on.")
parser.add_argument("-l", "--log",
help="The file to log to. stdout otherwise.")
parser.add_argument("-v", "--verbose", dest="verbosity", action="count", default=0,
help="Verbosity (between 1 and 2 occurrences with more leading to more "
"verbose logging). WARN=0, INFO=1, "
"DEBUG=2")
cmd_args = parser.parse_args()
setup_logging(cmd_args)
logger.info("Starting up.")
try:
dispatcher = Dispatcher(cmd_args.config_file)
except Exception as err:
logger.error('Dispatcher crashed: %s', str(err))
sys.exit(1)
try:
dispatcher.start()
dispatcher.join()
except KeyboardInterrupt:
logger.debug("Interrupting")
finally:
dispatcher.close()
if __name__ == '__main__':
main()