diff --git a/reportservice/automatic_run.py b/reportservice/automatic_run.py index 93a2454a9fbb3b25bfb376befc8a4fd518413fce..87e57602a5231789629595c5cc1d9e203de76520 100644 --- a/reportservice/automatic_run.py +++ b/reportservice/automatic_run.py @@ -1,6 +1,8 @@ +import argparse import asyncio from datetime import datetime, timedelta import logging +import urllib.parse from dateutil import parser, tz import yaml @@ -10,10 +12,10 @@ import zmq.asyncio async def auto_run(): """ Run the report service automatically depending on the scheduled times - in the run_time list, read from the config yaml file (cal_conf.yaml) + in the run_time list, read from the config yaml file (report_conf.yaml) """ - with open("cal_conf.yaml", 'r') as ymlfile: + with open("report_conf.yaml", 'r') as ymlfile: cfg = yaml.load(ymlfile) # time index that points at a timestamp, when the next @@ -55,15 +57,14 @@ async def auto_run(): await asyncio.sleep(600) - - -parser = argparse.ArgumentParser(description='Start the report service') -parser.add_argument('--config-file', type=str, default='./cal_conf.yaml') -parser.add_argument('--log-file', type=str, default='./report.log') -parser.add_argument('--logging', type=str, default="INFO", +arg_parser = argparse.ArgumentParser(description='Automatic Launch') +arg_parser.add_argument('--config-file', type=str, default='./report_conf.yaml') +arg_parser.add_argument('--log-file', type=str, default='./report.log') +arg_parser.add_argument('--logging', type=str, default="INFO", choices=['INFO', 'DEBUG', 'ERROR']) + if __name__ == "__main__": - args = vars(parser.parse_args()) + args = vars(arg_parser.parse_args()) conf_file = args["config_file"] with open(conf_file, "r") as f: config = yaml.load(f.read()) @@ -75,11 +76,6 @@ if __name__ == "__main__": level=getattr(logging, args['logging']), format='%(levelname)-6s: %(asctime)s %(message)s', datefmt='%Y-%m-%d %H:%M:%S') - logfile = './report.log' - logging.basicConfig(filename=logfile, filemode='a+', - level=logging.INFO, - format='%(levelname)-6s: %(asctime)s %(message)s', - datefmt='%Y-%m-%d %H:%M:%S') loop = asyncio.get_event_loop() loop.run_until_complete(auto_run()) diff --git a/reportservice/manual_run.py b/reportservice/manual_run.py index d5c088b8d5e12d39d352dc1ca1de2609615eca1c..1639e8e43315e36f8e54d77a9a8ecd0f40e399ec 100644 --- a/reportservice/manual_run.py +++ b/reportservice/manual_run.py @@ -1,20 +1,15 @@ -import asyncio +import argparse import logging import os import sys +import urllib.parse import yaml import zmq import zmq.asyncio -logfile = './report.log' -logging.basicConfig(filename=logfile, filemode='a+', - level=logging.INFO, - format='%(levelname)-6s: %(asctime)s %(message)s', - datefmt='%Y-%m-%d %H:%M:%S') - -async def manual_run(req_conf): +def manual_run(req_conf): """ Run the report service manually from any machine and provide the requested configuration for @@ -24,21 +19,41 @@ async def manual_run(req_conf): requested Instruments. This list can contain the Instruments names e.g ['SPB'] or ['all'] for generating reports for all - instruments in the "cal_conf.yaml". + instruments in the "report_conf.yaml". This can also be a customized conf. file(dict) for test purposes. """ - with open("cal_conf.yaml", 'r') as ymlfile: + with open("report_conf.yaml", 'r') as ymlfile: cfg = yaml.load(ymlfile) port = cfg['GLOBAL']['server-port'] - con = zmq.asyncio.Context() + con = zmq.Context() socket = con.socket(zmq.REQ) con = socket.connect(port) - await socket.send_pyobj(req_conf) - msg = await socket.recv_pyobj() + socket.send_pyobj(req_conf) + msg = socket.recv_pyobj() logging.info('{} Manual Run'.format(msg)) +arg_parser = argparse.ArgumentParser(description='Manual Launch') +arg_parser.add_argument('--config-file', type=str, default='./report_conf.yaml') +arg_parser.add_argument('--log-file', type=str, default='./report.log') +arg_parser.add_argument('--logging', type=str, default="INFO", + choices=['INFO', 'DEBUG', 'ERROR']) + +if __name__ == "__main__": + args = vars(arg_parser.parse_args()) + conf_file = args["config_file"] + with open(conf_file, "r") as f: + config = yaml.load(f.read()) + + logfile = args["log_file"] + fmt = '%(asctime)s - %(name)s - %(levelname)s - %(message)s' + + logging.basicConfig(filename=logfile, filemode='a+', + level=getattr(logging, args['logging']), + format='%(levelname)-6s: %(asctime)s %(message)s', + datefmt='%Y-%m-%d %H:%M:%S') + manual_run(['all']) diff --git a/reportservice/cal_conf.yaml b/reportservice/report_conf.yaml similarity index 95% rename from reportservice/cal_conf.yaml rename to reportservice/report_conf.yaml index 9cb54f3cfcd94cad19551db3e83a72a37f2448a3..87e51687d5b62b59caaf35cb8f9cf40bed971d59 100644 --- a/reportservice/cal_conf.yaml +++ b/reportservice/report_conf.yaml @@ -6,8 +6,8 @@ GLOBAL: server-port: "tcp://max-exfl015:5555" run-on: - - Monday 08:30:00 UTC - - Thursday 10:33:00 UTC + - Friday 08:30:00 UTC + - Friday 10:33:00 UTC report-service: port: 5555 diff --git a/reportservice/report_service.py b/reportservice/report_service.py index 1641f93ac50afb83badee3bc168ab65b4e09466c..c5017f1fc6813ab83b0acc0f905c4a7d1802ed5b 100644 --- a/reportservice/report_service.py +++ b/reportservice/report_service.py @@ -155,13 +155,13 @@ async def server_runner(config): while True: response = await socket.recv_pyobj() - socket.send_pyobj('Build DC reports through -->') + await socket.send_pyobj('Build DC reports through -->') logging.info("response: {}".format(response)) # Check if response is a list or a dict. # if list, it should either have instrument names or ['all']. # if dict, it should acquires the details of the requested reports - # for generation. As it will be used instead of cal_conf.yaml + # for generation. As it will be used instead of report_conf.yaml # reports config file req_cfg = {} @@ -172,7 +172,7 @@ async def server_runner(config): if len(response) == 1 and response[0] == 'all': req_cfg = config else: - req_cfg['GLOBAL'] = config[instr] + req_cfg['GLOBAL'] = config['GLOBAL'] for instr in response: try: req_cfg[instr] = config[instr] @@ -268,14 +268,14 @@ async def server_runner(config): logging.error(str(e)) break -parser = argparse.ArgumentParser(description='Start the report service') -parser.add_argument('--config-file', type=str, default='./cal_conf.yaml') -parser.add_argument('--log-file', type=str, default='./report.log') -parser.add_argument('--logging', type=str, default="INFO", +arg_parser = argparse.ArgumentParser(description='Start the report service') +arg_parser.add_argument('--config-file', type=str, default='./report_conf.yaml') +arg_parser.add_argument('--log-file', type=str, default='./report.log') +arg_parser.add_argument('--logging', type=str, default="INFO", choices=['INFO', 'DEBUG', 'ERROR']) if __name__ == "__main__": - args = vars(parser.parse_args()) + args = vars(arg_parser.parse_args()) conf_file = args["config_file"] with open(conf_file, "r") as f: config = yaml.load(f.read())