<s> from . models import ( Model , StringField , ModelField , DictField , IntegerField , FloatField , <EOL> ModelListField , ModelDictField , ListField ) <EOL> from . enums import NODE_BLOCKED , NODE_CANCELED , NODE_DONE , NODE_ERROR , NODE_PAUSED , NODE_READY , NODE_RUNNING <EOL> from collections import defaultdict <EOL> import logging <EOL> import datetime <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> class TaskGroup ( Model ) : <EOL> name = StringField ( ) <EOL> parent = ModelField ( allow_null = True ) <EOL> user = StringField ( ) <EOL> arguments = DictField ( ) <EOL> environment = DictField ( ) <EOL> maxRN = IntegerField ( ) <EOL> priority = IntegerField ( ) <EOL> dispatchKey = FloatField ( ) <EOL> nodes = ModelDictField ( ) <EOL> tasks = ModelListField ( ) <EOL> tags = DictField ( ) <EOL> status = IntegerField ( ) <EOL> completion = FloatField ( ) <EOL> creationTime = FloatField ( ) <EOL> startTime = FloatField ( allow_null = True ) <EOL> updateTime = FloatField ( allow_null = True ) <EOL> endTime = FloatField ( allow_null = True ) <EOL> timer = FloatField ( allow_null = True ) <EOL> def __init__ ( self , id , name , parent , user , arguments , environment , requirements , <EOL> maxRN , priority , dispatchKey , strategy , nodes = { } , tags = { } , timer = None ) : <EOL> Model . __init__ ( self ) <EOL> self . id = int ( id ) if id else None <EOL> self . name = str ( name ) <EOL> self . parent = parent <EOL> self . user = str ( user ) <EOL> self . arguments = arguments <EOL> self . environment = environment <EOL> self . requirements = requirements <EOL> self . maxRN = int ( maxRN ) <EOL> self . priority = int ( priority ) <EOL> self . dispatchKey = float ( dispatchKey ) <EOL> self . strategy = strategy <EOL> self . nodes = nodes . copy ( ) <EOL> self . tasks = [ ] <EOL> self . tags = tags . copy ( ) <EOL> self . completion = <NUM_LIT:0> <EOL> self . status = <NUM_LIT:0> <EOL> self . _modified = True <EOL> self . creationTime = None <EOL> self . startTime = None <EOL> self . updateTime = None <EOL> self . endTime = None <EOL> self . timer = timer <EOL> def addTask ( self , task ) : <EOL> assert isinstance ( task , Task ) or isinstance ( task , TaskGroup ) <EOL> task . setTimer ( self . timer ) <EOL> self . tasks . append ( task ) <EOL> def setTimer ( self , timer ) : <EOL> if timer is not None : <EOL> logger . warning ( "<STR_LIT>" % ( self . id , datetime . datetime . fromtimestamp ( timer ) ) ) <EOL> self . timer = timer <EOL> for task in self . tasks : <EOL> task . setTimer ( timer ) <EOL> def removeTask ( self , task ) : <EOL> self . tasks . remove ( task ) <EOL> def archive ( self ) : <EOL> self . fireDestructionEvent ( self ) <EOL> def updateStatusAndCompletion ( self ) : <EOL> if not self . tasks : <EOL> self . completion = <NUM_LIT:1.0> <EOL> self . status = NODE_DONE <EOL> else : <EOL> completion = <NUM_LIT:0.0> <EOL> status = defaultdict ( int ) <EOL> for child in self . tasks : <EOL> if isinstance ( child , TaskGroup ) : <EOL> child . updateStatusAndCompletion ( ) <EOL> completion += child . completion <EOL> status [ child . status ] += <NUM_LIT:1> <EOL> self . completion = completion / len ( self . tasks ) <EOL> if NODE_PAUSED in status : <EOL> self . status = NODE_PAUSED <EOL> elif NODE_RUNNING in status : <EOL> self . status = NODE_RUNNING <EOL> elif NODE_ERROR in status : <EOL> self . status = NODE_ERROR <EOL> elif NODE_CANCELED in status : <EOL> self . status = NODE_CANCELED <EOL> elif NODE_READY in status or self . completion != <NUM_LIT:1.0> : <EOL> self . status = NODE_READY <EOL> elif NODE_BLOCKED in status : <EOL> self . status = NODE_BLOCKED <EOL> else : <EOL> self . status = NODE_DONE <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . id , <EOL> self . name , <EOL> self . parent , <EOL> self . nodes ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . id , self . name , self . parent ) <EOL> def __iter__ ( self ) : <EOL> return iter ( self . tasks ) <EOL> class Task ( Model ) : <EOL> changeEventSourceFields = [ '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> name = StringField ( ) <EOL> parent = ModelField ( allow_null = True ) <EOL> user = StringField ( ) <EOL> maxRN = IntegerField ( ) <EOL> priority = IntegerField ( ) <EOL> dispatchKey = FloatField ( ) <EOL> runner = StringField ( ) <EOL> arguments = DictField ( ) <EOL> validationExpression = StringField ( ) <EOL> commands = ModelListField ( ) <EOL> requirements = DictField ( ) <EOL> environment = DictField ( ) <EOL> minNbCores = IntegerField ( ) <EOL> maxNbCores = IntegerField ( ) <EOL> ramUse = IntegerField ( ) <EOL> nodes = ModelDictField ( ) <EOL> tags = DictField ( ) <EOL> status = IntegerField ( ) <EOL> completion = FloatField ( ) <EOL> creationTime = FloatField ( allow_null = True ) <EOL> startTime = FloatField ( allow_null = True ) <EOL> updateTime = FloatField ( allow_null = True ) <EOL> endTime = FloatField ( allow_null = True ) <EOL> lic = StringField ( ) <EOL> timer = FloatField ( allow_null = True ) <EOL> maxAttempt = IntegerField ( ) <EOL> runnerPackages = StringField ( ) <EOL> watcherPackages = StringField ( ) <EOL> def __init__ ( <EOL> self , <EOL> id , name , parent , user , maxRN , priority , dispatchKey , <EOL> runner , arguments , validationExpression , commands , <EOL> requirements = [ ] , minNbCores = <NUM_LIT:1> , maxNbCores = <NUM_LIT:0> , ramUse = <NUM_LIT:0> , <EOL> environment = { } , nodes = { } , lic = "<STR_LIT>" , tags = { } , maxAttempt = <NUM_LIT:1> , timer = None , <EOL> runnerPackages = None , <EOL> watcherPackages = None <EOL> ) : <EOL> assert parent is None or isinstance ( parent , TaskGroup ) <EOL> Model . __init__ ( self ) <EOL> self . id = int ( id ) if id else None <EOL> self . name = str ( name ) <EOL> self . parent = parent <EOL> self . user = user <EOL> self . maxRN = int ( maxRN ) <EOL> self . priority = int ( priority ) <EOL> self . dispatchKey = int ( dispatchKey ) <EOL> self . runner = str ( runner ) <EOL> self . arguments = arguments <EOL> self . validationExpression = str ( validationExpression ) <EOL> self . commands = commands <EOL> self . requirements = requirements <EOL> self . environment = environment <EOL> self . minNbCores = int ( minNbCores ) <EOL> self . maxNbCores = int ( maxNbCores ) <EOL> self . ramUse = int ( ramUse ) <EOL> self . nodes = nodes . copy ( ) <EOL> self . maxAttempt = int ( maxAttempt ) <EOL> self . lic = lic <EOL> self . tags = tags . copy ( ) <EOL> self . completion = <NUM_LIT:0> <EOL> self . status = <NUM_LIT:0> <EOL> self . creationTime = None <EOL> self . startTime = None <EOL> self . updateTime = None <EOL> self . endTime = None <EOL> self . timer = timer <EOL> self . runnerPackages = runnerPackages <EOL> self . watcherPackages = watcherPackages <EOL> def addValidationExpression ( self , validationExpression ) : <EOL> self . validationExpression = "<STR_LIT:&>" . join ( self . validationExpression , <EOL> validationExpression ) <EOL> def archive ( self ) : <EOL> self . fireDestructionEvent ( self ) <EOL> def setTimer ( self , timer ) : <EOL> if timer is not None : <EOL> logger . warning ( "<STR_LIT>" % ( self . id , datetime . datetime . fromtimestamp ( timer ) ) ) <EOL> self . timer = timer <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . id , <EOL> self . name , <EOL> self . maxAttempt , <EOL> self . nodes , <EOL> self . runner , <EOL> self . commands ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . id , self . name ) <EOL> class TaskListener ( object ) : <EOL> def __init__ ( self ) : <EOL> self . created = False <EOL> self . modified = False <EOL> def onCreationEvent ( self , * args ) : <EOL> pass <EOL> def onDestructionEvent ( self , * args ) : <EOL> pass <EOL> def onChangeEvent ( self , obj , name , oldvalue , newvalue ) : <EOL> if obj . parent is not None : <EOL> obj . parent . modified = True </s>
