CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
o2oRun_SiStripDCS.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 '''
3 Top level script to run SiStrip DCS O2O.
4 @author: Huilin Qu
5 '''
6 
7 import os
8 import sys
9 import atexit
10 import logging
11 import argparse
12 import subprocess
13 import traceback
14 import json
15 from functools import partial
16 
17 import CondTools.SiStrip.o2o_helper as helper
18 
19 logDirVar = 'O2O_LOG_FOLDER'
20 
21 def run(args):
22  logging.debug(args)
23 
24  is_ok = True
25 
26  o2ocmd = "SiStripDCSPopCon.py"
27  o2ocmd += ' --delay {delay}'
28  o2ocmd += ' --destTags {destTags}'
29  o2ocmd += ' --destDb {destDb}'
30  o2ocmd += ' --inputTag {inputTag}'
31  o2ocmd += ' --sourceDb {sourceDb}'
32  o2ocmd += ' --condDbRead {condDbRead}'
33  if args.debug:
34  o2ocmd += ' --debug'
35 
36  cmd = 'o2o --db {db} -v run -n {jobname} "{o2ocmd}"'.format(db=args.db, jobname=args.jobname, o2ocmd=o2ocmd)
37  logging.info('Start running command:\n %s' % cmd)
38 
39  p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
40  atexit.register(partial(helper.kill_subproc_noexcept, p))
41 
42  log = p.communicate()[0]
43  if p.returncode == 0:
44  logging.info('O2OJob %s finished successfully!' % args.jobname)
45  else:
46  logging.error('O2OJob %s FAILED!' % args.jobname)
47  is_ok = False
48 
49  return is_ok
50 
51 def summary(args, is_ok, logfile):
52  if is_ok:
53  return
54 
55  # send the detailed log if failed
56  debugLabel = '[TEST] ' if args.debug else ''
57  with open(logfile, 'rb') as log:
58  helper.send_mail(subject='%sDCS O2O Failure: %s' % (debugLabel, args.jobname),
59  message=log.read(),
60  send_to=args.mail_log_to,
61  send_from=args.mail_from)
62 
63 
64 def main():
65  parser = argparse.ArgumentParser(description='Run SiStrip DCS O2O.')
66  parser.add_argument('jobname', metavar='JOBNAME', type=str, help='O2O job name as in DB.')
67  parser.add_argument('--mail-from', default='trk.o2o@cern.ch', help='Account to send email notification.')
68  parser.add_argument('--mail-to', default='trk.o2o@cern.ch', help='List of O2O notification recipients.')
69  parser.add_argument('--mail-log-to', default='trk.o2o@cern.ch', help='List of O2O log recipients.')
70  parser.add_argument('--db', default='pro', help='The database for o2o job management: pro ( for prod ) or dev ( for prep ). Default: %(default)s.')
71  parser.add_argument('--debug', action="store_true", default=False, help='Switch on debug mode. Default: %(default)s.')
72 
73  args = parser.parse_args()
74  args.mail_to = args.mail_to.strip().split(',')
75  args.mail_log_to = args.mail_log_to.strip().split(',')
76 
77  # Should NOT use logging before it's set up
78  try:
79  logdir = os.environ[logDirVar] if logDirVar in os.environ else '/tmp'
80  if not os.path.exists(logdir):
81  os.makedirs(logdir)
82  logfile = os.path.join(logdir, 'SiStripsDCSO2O_%s.log' % str(args.jobname))
83  loglevel = logging.DEBUG if args.debug else logging.INFO
84  helper.configLogger(logfile, loglevel)
85  except Exception:
86  # in case we failed before logging is set up
87  # print the error, send an email, and exit
88  helper.send_mail('DCS O2O Failure: %s' % args.jobname, traceback.format_exc(), args.mail_to, args.mail_from)
89  raise
90 
91  try:
92  is_ok = run(args)
93  summary(args, is_ok, logfile)
94  except Exception:
95  # in case we failed before logging is set up
96  # print the error, send an email, and exit
97  helper.send_mail('DCS O2O Failure: %s' % args.jobname, traceback.format_exc(), args.mail_to, args.mail_from)
98  raise
99 
100  if not is_ok:
101  return ' --- O2O FAILED! ---'
102 
103 if __name__ == '__main__':
104  sys.exit(main())
Definition: main.py:1
#define str(s)