Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# (c) Stefan Countryman, 2019
3"""
4Launch the GCN Notice listener.
5"""
7import os
8import logging
9import gcn
10from llama.utils import (
11 __version__,
12 LOGDIR,
13)
14from llama.listen import Parsers as ListenParsers
15from llama.cli import Parsers
16from llama.listen.gcn import (
17 LlamaHandlers,
18 GCN_ARCHIVE,
19 DEFAULT_HOST,
20 PUBLIC_GCN_PORT,
21 PRIVATE_GCN_PORT,
22)
23from llama.cli import (
24 log_exceptions_and_recover,
25 get_logging_cli,
26 CliParser,
27)
29GCN_LOGFILE = os.path.join(LOGDIR, 'gcn.log')
30LOGGER = logging.getLogger(__name__)
32# TODO Handle loggers properly as part of a unified daemonizing procedure.
35def get_parser():
36 """Get CLI."""
37 parser = CliParser(
38 description=__doc__,
39 parents=(
40 get_logging_cli(GCN_LOGFILE, 'debug'),
41 ListenParsers.rundir,
42 Parsers.erralert,
43 ),
44 )
45 parser.add_argument("-L", "--lvc-private", action="store_true", help="""
46 If provided, listen for private LIGO GCN circulars on port 8099. This
47 argument will OVERRIDE the -p/--port argument.""")
48 parser.add_argument("-g", "--gcn-archive", default=GCN_ARCHIVE, help="""
49 Change the GCN archive directory (i.e. where all GCN Notices are
50 archived too for recordkeeping). Creates the directory if it does not
51 already exist.""")
52 parser.add_argument("-i", "--host", default=DEFAULT_HOST, help="""
53 The host to listen to. Defaults to the main GCN server:
54 {}""".format(DEFAULT_HOST))
55 parser.add_argument("-p", "--port", default=PUBLIC_GCN_PORT, help="""
56 The port on the GCN server to connect to. Default:
57 {}""".format(PUBLIC_GCN_PORT))
58 return parser
61def main():
62 """Run a GCN VOEvent listener with ``all_llama_handlers``."""
63 parser = get_parser()
64 args = parser.parse_args()
65 if args.lvc_private:
66 args.port = PRIVATE_GCN_PORT
67 for opt in 'rundir', 'gcn_archive':
68 dirpath = getattr(args, opt)
69 if not os.path.isdir(dirpath):
70 LOGGER.info("specified %s does not exist, creating: %s",
71 opt, dirpath)
72 os.makedirs(dirpath)
73 # pylint: disable=no-member
74 LOGGER.info("Starting GCN Listener, llama version %s.", __version__)
75 handler = log_exceptions_and_recover()(
76 LlamaHandlers(rundir=args.rundir, gcn_archive=args.gcn_archive)
77 )
78 gcn.listen(host=args.host, port=args.port, handler=handler)
81if __name__ == "__main__":
82 main()