CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
CrabWatch.py
Go to the documentation of this file.
1 #from CrabTask import CrabTask
2 from crabWrap import crabStatus,convertStatus,getOutput
3 import os,time
4 from threading import Thread,Lock,Event
5 
6 class CrabWatch(Thread):
7  #def __init__(self, task):
8  # Thread.__init__(self)
9  # self.task = task
10  def __init__(self, project, action = getOutput):
11  Thread.__init__(self)
12  self.project = project
13  self.action = action
14  self.threshold = 100.0
15 
16  self.lock = Lock()
17  self.finish = Event()
18 
19  def setThreshold(self,threshold):
20  self.threshold = float(threshold)
21 
22  def run(self):
23  exit = False
24  failed=0
25  while not exit:
26  #if checkStatus(self.project,80.0): break
27  try:
28  status = crabStatus(self.project)
29  except CrabException:
30  failed+=1
31  print "Crab retrieve status failed (",failed,")"
32  if failed>10:
33  raise
34  else:
35  statusNew = convertStatus(status)
36  print "Relative percentage finished: %.0f%%" % statusNew['Finished']
37  print "Relative percentage failed : %.0f%%" % statusNew['Failed']
38  print "Relative percentage running : %.0f%%" % statusNew['Running']
39  if statusNew['Failed'] > 50.0: raise RuntimeError,'Too many jobs have failed (%.0f%%).' % statusNew['Failed']
40  if statusNew['Finished'] >= self.threshold: break
41 
42  self.lock.acquire()
43  if self.finish.isSet(): exit = True
44  self.lock.release()
45 
46  if not exit: time.sleep(180)
47 
48  print "Finished..."
49 
50  if self.action: self.action(self.project)
51 
52 if __name__ == '__main__':
53 
54  project = None
55  import sys
56  for opt in sys.argv:
57  if opt[:8] == 'project=':
58  project = opt[8:]
59 
60  if not project: raise ValueError,'Need to set project'
61 
62  crab = CrabWatch(project)
63  crab.start()
def convertStatus
Definition: crabWrap.py:174
def crabStatus
Definition: crabWrap.py:157