Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-07-15 08:47:27

0001 #!/usr/bin/env python3
0002 '''
0003 Top level script to run SiStrip DCS O2O.
0004 @author: Huilin Qu
0005 '''
0006 
0007 import os
0008 import sys
0009 import atexit
0010 import logging
0011 import argparse
0012 import subprocess
0013 import traceback
0014 import json
0015 from functools import partial
0016 
0017 import CondTools.SiStrip.o2o_helper as helper
0018 
0019 logDirVar = 'O2O_LOG_FOLDER'
0020 
0021 def run(args):
0022     logging.debug(args)
0023 
0024     is_ok = True
0025 
0026     o2ocmd = "SiStripDCSPopCon.py"
0027     o2ocmd += ' --delay {delay}'
0028     o2ocmd += ' --destTags {destTags}'
0029     o2ocmd += ' --destDb {destDb}'
0030     o2ocmd += ' --inputTag {inputTag}'
0031     o2ocmd += ' --sourceDb {sourceDb}'
0032     o2ocmd += ' --condDbRead {condDbRead}'
0033     if args.debug:
0034         o2ocmd += ' --debug'
0035 
0036     cmd = 'o2o --db {db} -v run -n {jobname} "{o2ocmd}"'.format(db=args.db, jobname=args.jobname, o2ocmd=o2ocmd)
0037     logging.info('Start running command:\n %s' % cmd)
0038 
0039     p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
0040     atexit.register(partial(helper.kill_subproc_noexcept, p))
0041 
0042     log = p.communicate()[0]
0043     if p.returncode == 0:
0044         logging.info('O2OJob %s finished successfully!' % args.jobname)
0045     else:
0046         logging.error('O2OJob %s FAILED!' % args.jobname)
0047         is_ok = False
0048 
0049     return is_ok
0050 
0051 def summary(args, is_ok, logfile):
0052     if is_ok:
0053         return
0054 
0055     # send the detailed log if failed
0056     debugLabel = '[TEST] ' if args.debug else ''
0057     with open(logfile, 'rb') as log:
0058         helper.send_mail(subject='%sDCS O2O Failure: %s' % (debugLabel, args.jobname),
0059                  message=log.read(),
0060                  send_to=args.mail_log_to,
0061                  send_from=args.mail_from)
0062 
0063 
0064 def main():
0065     parser = argparse.ArgumentParser(description='Run SiStrip DCS O2O.')
0066     parser.add_argument('jobname', metavar='JOBNAME', type=str, help='O2O job name as in DB.')
0067     parser.add_argument('--mail-from', default='trk.o2o@cern.ch', help='Account to send email notification.')
0068     parser.add_argument('--mail-to', default='trk.o2o@cern.ch', help='List of O2O notification recipients.')
0069     parser.add_argument('--mail-log-to', default='trk.o2o@cern.ch', help='List of O2O log recipients.')
0070     parser.add_argument('--db', default='pro', help='The database for o2o job management: pro ( for prod ) or dev ( for prep ). Default: %(default)s.')
0071     parser.add_argument('--debug', action="store_true", default=False, help='Switch on debug mode. Default: %(default)s.')
0072 
0073     args = parser.parse_args()
0074     args.mail_to = args.mail_to.strip().split(',')
0075     args.mail_log_to = args.mail_log_to.strip().split(',')
0076 
0077     # Should NOT use logging before it's set up
0078     try:
0079         logdir = os.environ[logDirVar] if logDirVar in os.environ else '/tmp'
0080         if not os.path.exists(logdir):
0081             os.makedirs(logdir)
0082         logfile = os.path.join(logdir, 'SiStripsDCSO2O_%s.log' % str(args.jobname))
0083         loglevel = logging.DEBUG if args.debug else logging.INFO
0084         helper.configLogger(logfile, loglevel)
0085     except Exception:
0086         # in case we failed before logging is set up
0087         # print the error, send an email, and exit
0088         helper.send_mail('DCS O2O Failure: %s' % args.jobname, traceback.format_exc(), args.mail_to, args.mail_from)
0089         raise
0090 
0091     try:
0092         is_ok = run(args)
0093         summary(args, is_ok, logfile)
0094     except Exception:
0095         # in case we failed before logging is set up
0096         # print the error, send an email, and exit
0097         helper.send_mail('DCS O2O Failure: %s' % args.jobname, traceback.format_exc(), args.mail_to, args.mail_from)
0098         raise
0099 
0100     if not is_ok:
0101         return ' --- O2O FAILED! ---'
0102 
0103 if __name__ == '__main__':
0104     sys.exit(main())