-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunDaemon.py
More file actions
149 lines (120 loc) · 5.37 KB
/
RunDaemon.py
File metadata and controls
149 lines (120 loc) · 5.37 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
#!/usr/bin/env python2.7
import os
import sys
import argparse
import logging
import signal
from CCDaemon import DaemonManager
# Define the available platform modules
available_platforms = DaemonManager.AVAILABLE_PLATFORMS
def configure_logging(verbosity):
# Setting the format of the logs
FORMAT = "[%(asctime)s] %(levelname)s: %(message)s"
# Configuring the logging system to the lowest level
logging.basicConfig(level=logging.DEBUG, format=FORMAT, stream=sys.stderr)
# Defining the ANSI Escape characters
BOLD = '\033[1m'
DEBUG = '\033[92m'
INFO = '\033[94m'
WARNING = '\033[93m'
ERROR = '\033[91m'
END = '\033[0m'
# Coloring the log levels
if sys.stderr.isatty():
logging.addLevelName(logging.ERROR, "%s%s%s%s%s" % (BOLD, ERROR, "CC_DAEMON_ERROR", END, END))
logging.addLevelName(logging.WARNING, "%s%s%s%s%s" % (BOLD, WARNING, "CC_DAEMON_WARNING", END, END))
logging.addLevelName(logging.INFO, "%s%s%s%s%s" % (BOLD, INFO, "CC_DAEMON_INFO", END, END))
logging.addLevelName(logging.DEBUG, "%s%s%s%s%s" % (BOLD, DEBUG, "CC_DAEMON_DEBUG", END, END))
else:
logging.addLevelName(logging.ERROR, "CC_DAEMON_ERROR")
logging.addLevelName(logging.WARNING, "CC_DAEMON_WARNING")
logging.addLevelName(logging.INFO, "CC_DAEMON_INFO")
logging.addLevelName(logging.DEBUG, "CC_DAEMON_DEBUG")
# Setting the level of the logs
level = [logging.ERROR, logging.WARNING, logging.INFO, logging.DEBUG][verbosity]
logging.getLogger().setLevel(level)
def configure_argparser(argparser_obj):
def platform_type(arg_string):
value = arg_string.capitalize()
if value not in available_platforms:
err_msg = "%s is not a valid platform! " \
"Please view usage menu for a list of available platforms" % value
raise argparse.ArgumentTypeError(err_msg)
return arg_string
def file_type(arg_string):
"""
This function check both the existance of input file and the file size
:param arg_string: file name as string
:return: file name as string
"""
if not os.path.exists(arg_string):
err_msg = "%s does not exist!! " \
"Please provide a correct file!!" % arg_string
raise argparse.ArgumentTypeError(err_msg)
return arg_string
# Path to sample set config file
argparser_obj.add_argument("--config",
action="store",
type=file_type,
dest="config_file",
required=True,
help="Path to config file containing input files "
"and information for one or more samples.")
# Name of the platform module
available_plats = "\n".join(["%s" % item for item in available_platforms])
argparser_obj.add_argument("--platform",
action='store',
type=platform_type,
dest='platform',
required=True,
help="Platform to be used. Possible values are:\n%s" % available_plats)
# Verbosity level
argparser_obj.add_argument("-v",
action='count',
dest='verbosity_level',
required=False,
default=0,
help="Increase verbosity of the program."
"Multiple -v's increase the verbosity level:\n"
"0 = Errors\n"
"1 = Errors + Warnings\n"
"2 = Errors + Warnings + Info\n"
"3 = Errors + Warnings + Info + Debug")
def main():
# Configure argparser
argparser = argparse.ArgumentParser(prog="CC-Daemon")
configure_argparser(argparser)
# Parse the arguments
args = argparser.parse_args()
# Configure logging
configure_logging(args.verbosity_level)
# Read and validate daemon config
config_file = args.config_file
# Create CC daemon and make global
cc_daemon = DaemonManager(config_file=config_file, platform_type=args.platform)
err_msg = ""
try:
# Define inner function to update pipeline queue when a sighup is received
def update_daemon(signum, frame):
logging.debug("SIGHUP received!")
cc_daemon.update_pipeline_queue()
# Register SIGHUP as signal indicating when daemon should update
signal.signal(signal.SIGHUP, update_daemon)
# Summon the CC daemon and have it run until an error occurs
cc_daemon.summon()
except KeyboardInterrupt, e:
logging.error("(Main) Keyboard interrupt!")
err_msg = "Keyboard interrupt!"
except BaseException, e:
# Report any errors that arise
logging.error("(Main) Daemon failed!")
err_msg = "Runtime error!"
if e.message != "":
err_msg += "\nReceived the following error message:\n%s" % e.message
raise
finally:
# Safely clean-up and send error report
cc_daemon.finalize(err_msg=err_msg)
logging.info("CC-Daemon exited gracefully!")
if __name__ == "__main__":
main()