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  while not exit:
25  #if checkStatus(self.project,80.0): break
26  status = crabStatus(self.project)
27  statusNew = convertStatus(status)
28  print "Relative percentage finished: %.0f%%" % statusNew['Finished']
29  print "Relative percentage failed : %.0f%%" % statusNew['Failed']
30  print "Relative percentage running : %.0f%%" % statusNew['Running']
31  if statusNew['Failed'] > 50.0: raise RuntimeError,'Too many jobs have failed (%.0f%%).' % statusNew['Failed']
32  if statusNew['Finished'] >= self.threshold: break
33 
34  self.lock.acquire()
35  if self.finish.isSet(): exit = True
36  self.lock.release()
37 
38  if not exit: time.sleep(180)
39 
40  print "Finished..."
41 
42  if self.action: self.action(self.project)
43 
44 if __name__ == '__main__':
45 
46  project = None
47  import sys
48  for opt in sys.argv:
49  if opt[:8] == 'project=':
50  project = opt[8:]
51 
52  if not project: raise ValueError,'Need to set project'
53 
54  crab = CrabWatch(project)
55  crab.start()
def convertStatus
Definition: crabWrap.py:174
def crabStatus
Definition: crabWrap.py:157