CMS 3D CMS Logo

o2oRun_SiStripDCS.py
Go to the documentation of this file.
1 #!/usr/bin/env python
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 += ' --condDbRead {condDbRead}'
32  if args.debug:
33  o2ocmd += ' --debug'
34 
35  cmd = 'o2o --db {db} -v run -n {jobname} "{o2ocmd}"'.format(db=args.db, jobname=args.jobname, o2ocmd=o2ocmd)
36  logging.info('Start running command:\n %s' % cmd)
37 
38  p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
39  atexit.register(partial(helper.kill_subproc_noexcept, p))
40 
41  log = p.communicate()[0]
42  if p.returncode == 0:
43  logging.info('O2OJob %s finished successfully!' % args.jobname)
44  else:
45  logging.error('O2OJob %s FAILED!' % args.jobname)
46  is_ok = False
47 
48  return is_ok
49 
50 def summary(args, is_ok, logfile):
51  if is_ok:
52  return
53 
54  # send the detailed log if failed
55  debugLabel = '[TEST] ' if args.debug else ''
56  with open(logfile, 'rb') as log:
57  helper.send_mail(subject='%sDCS O2O Failure: %s' % (debugLabel, args.jobname),
58  message=log.read(),
59  send_to=args.mail_log_to,
60  send_from=args.mail_from)
61 
62 
63 def main():
64  parser = argparse.ArgumentParser(description='Run SiStrip DCS O2O.')
65  parser.add_argument('jobname', metavar='JOBNAME', type=str, help='O2O job name as in DB.')
66  parser.add_argument('--mail-from', default='trk.o2o@cern.ch', help='Account to send email notification.')
67  parser.add_argument('--mail-to', default='trk.o2o@cern.ch', help='List of O2O notification recipients.')
68  parser.add_argument('--mail-log-to', default='trk.o2o@cern.ch', help='List of O2O log recipients.')
69  parser.add_argument('--db', default='pro', help='The database for o2o job management: pro ( for prod ) or dev ( for prep ). Default: %(default)s.')
70  parser.add_argument('--debug', action="store_true", default=False, help='Switch on debug mode. Default: %(default)s.')
71 
72  args = parser.parse_args()
73  args.mail_to = args.mail_to.strip().split(',')
74  args.mail_log_to = args.mail_log_to.strip().split(',')
75 
76  # Should NOT use logging before it's set up
77  try:
78  logdir = os.environ[logDirVar] if logDirVar in os.environ else '/tmp'
79  if not os.path.exists(logdir):
80  os.makedirs(logdir)
81  logfile = os.path.join(logdir, 'SiStripsDCSO2O_%s.log' % str(args.jobname))
82  loglevel = logging.DEBUG if args.debug else logging.INFO
83  helper.configLogger(logfile, loglevel)
84  except Exception:
85  # in case we failed before logging is set up
86  # print the error, send an email, and exit
87  helper.send_mail('DCS O2O Failure: %s' % args.jobname, traceback.format_exc(), args.mail_to, args.mail_from)
88  raise
89 
90  try:
91  is_ok = run(args)
92  summary(args, is_ok, logfile)
93  except Exception:
94  # in case we failed before logging is set up
95  # print the error, send an email, and exit
96  helper.send_mail('DCS O2O Failure: %s' % args.jobname, traceback.format_exc(), args.mail_to, args.mail_from)
97  raise
98 
99  if not is_ok:
100  return ' --- O2O FAILED! ---'
101 
102 if __name__ == '__main__':
103  sys.exit(main())
def summary(args, is_ok, logfile)
Definition: main.py:1
double split
Definition: MVATrainer.cc:139