<s> import datetime <EOL> import six <EOL> from bson import json_util <EOL> from girder import events <EOL> from girder . constants import AccessType , SortDir <EOL> from girder . models . model_base import AccessControlledModel , ValidationException <EOL> from girder . plugins . jobs . constants import JobStatus , JOB_HANDLER_LOCAL <EOL> class Job ( AccessControlledModel ) : <EOL> def initialize ( self ) : <EOL> self . name = '<STR_LIT>' <EOL> compoundSearchIndex = ( <EOL> ( '<STR_LIT>' , SortDir . ASCENDING ) , <EOL> ( '<STR_LIT>' , SortDir . DESCENDING ) <EOL> ) <EOL> self . ensureIndices ( [ ( compoundSearchIndex , { } ) ] ) <EOL> self . exposeFields ( level = AccessType . READ , fields = { <EOL> '<STR_LIT:title>' , '<STR_LIT:type>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:status>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' } ) <EOL> self . exposeFields ( level = AccessType . SITE_ADMIN , fields = { <EOL> '<STR_LIT:args>' , '<STR_LIT>' } ) <EOL> def validate ( self , job ) : <EOL> if not JobStatus . isValid ( job [ '<STR_LIT:status>' ] ) : <EOL> raise ValidationException ( <EOL> '<STR_LIT>' % job [ '<STR_LIT:status>' ] , field = '<STR_LIT:status>' ) <EOL> return job <EOL> def list ( self , user = None , limit = <NUM_LIT:0> , offset = <NUM_LIT:0> , sort = None , currentUser = None ) : <EOL> """<STR_LIT>""" <EOL> userId = user [ '<STR_LIT>' ] if user else None <EOL> cursor = self . find ( { '<STR_LIT>' : userId } , sort = sort ) <EOL> for r in self . filterResultsByPermission ( cursor = cursor , user = currentUser , <EOL> level = AccessType . READ , <EOL> limit = limit , offset = offset ) : <EOL> yield r <EOL> def cancelJob ( self , job ) : <EOL> """<STR_LIT>""" <EOL> event = events . trigger ( '<STR_LIT>' , info = job ) <EOL> if not event . defaultPrevented : <EOL> job [ '<STR_LIT:status>' ] = JobStatus . CANCELED <EOL> self . save ( job ) <EOL> return job <EOL> def createLocalJob ( self , module , function = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> kwargs [ '<STR_LIT>' ] = JOB_HANDLER_LOCAL <EOL> kwargs [ '<STR_LIT>' ] = False <EOL> job = self . createJob ( ** kwargs ) <EOL> job [ '<STR_LIT>' ] = module <EOL> if function is not None : <EOL> job [ '<STR_LIT>' ] = function <EOL> return self . save ( job ) <EOL> def createJob ( self , title , type , args = ( ) , kwargs = None , user = None , when = None , <EOL> interval = <NUM_LIT:0> , public = False , handler = None , async = False , <EOL> save = True ) : <EOL> """<STR_LIT>""" <EOL> now = datetime . datetime . utcnow ( ) <EOL> if when is None : <EOL> when = now <EOL> if kwargs is None : <EOL> kwargs = { } <EOL> job = { <EOL> '<STR_LIT:title>' : title , <EOL> '<STR_LIT:type>' : type , <EOL> '<STR_LIT:args>' : args , <EOL> '<STR_LIT>' : kwargs , <EOL> '<STR_LIT>' : now , <EOL> '<STR_LIT>' : now , <EOL> '<STR_LIT>' : when , <EOL> '<STR_LIT>' : interval , <EOL> '<STR_LIT:status>' : JobStatus . INACTIVE , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : handler , <EOL> '<STR_LIT>' : async , <EOL> '<STR_LIT>' : [ ] <EOL> } <EOL> self . setPublic ( job , public = public ) <EOL> if user : <EOL> job [ '<STR_LIT>' ] = user [ '<STR_LIT>' ] <EOL> self . setUserAccess ( job , user = user , level = AccessType . ADMIN ) <EOL> if save : <EOL> job = self . save ( job ) <EOL> return job <EOL> def save ( self , job , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> deserialized = job [ '<STR_LIT>' ] <EOL> job [ '<STR_LIT>' ] = json_util . dumps ( job [ '<STR_LIT>' ] ) <EOL> job = AccessControlledModel . save ( self , job , * args , ** kwargs ) <EOL> job [ '<STR_LIT>' ] = deserialized <EOL> return job <EOL> def load ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> job = AccessControlledModel . load ( self , * args , ** kwargs ) <EOL> if job and isinstance ( job [ '<STR_LIT>' ] , six . string_types ) : <EOL> job [ '<STR_LIT>' ] = json_util . loads ( job [ '<STR_LIT>' ] ) <EOL> return job <EOL> def scheduleJob ( self , job ) : <EOL> """<STR_LIT>""" <EOL> if job . get ( '<STR_LIT>' ) is True : <EOL> events . daemon . trigger ( '<STR_LIT>' , info = job ) <EOL> else : <EOL> events . trigger ( '<STR_LIT>' , info = job ) <EOL> def createJobToken ( self , job , days = <NUM_LIT:7> ) : <EOL> """<STR_LIT>""" <EOL> return self . model ( '<STR_LIT>' ) . createToken ( <EOL> days = days , scope = '<STR_LIT>' + str ( job [ '<STR_LIT>' ] ) ) <EOL> def updateJob ( self , job , log = None , overwrite = False , status = None , <EOL> progressTotal = None , progressCurrent = None , notify = True , <EOL> progressMessage = None ) : <EOL> """<STR_LIT>""" <EOL> changed = False <EOL> now = datetime . datetime . utcnow ( ) <EOL> if log is not None : <EOL> changed = True <EOL> if overwrite : <EOL> job [ '<STR_LIT>' ] = log <EOL> else : <EOL> job [ '<STR_LIT>' ] += log <EOL> if status is not None : <EOL> status = int ( status ) <EOL> if status != job [ '<STR_LIT:status>' ] : <EOL> changed = True <EOL> job [ '<STR_LIT:status>' ] = status <EOL> job [ '<STR_LIT>' ] = job . get ( '<STR_LIT>' , [ ] ) <EOL> job [ '<STR_LIT>' ] . append ( { <EOL> '<STR_LIT:status>' : status , <EOL> '<STR_LIT:time>' : now <EOL> } ) <EOL> if notify and job [ '<STR_LIT>' ] : <EOL> user = self . model ( '<STR_LIT:user>' ) . load ( job [ '<STR_LIT>' ] , force = True ) <EOL> expires = now + datetime . timedelta ( seconds = <NUM_LIT:30> ) <EOL> filtered = self . filter ( job , user ) <EOL> filtered . pop ( '<STR_LIT>' , None ) <EOL> self . model ( '<STR_LIT>' ) . createNotification ( <EOL> type = '<STR_LIT>' , data = filtered , user = user , <EOL> expires = expires ) <EOL> if ( progressMessage is not None or progressCurrent is not None or <EOL> progressTotal is not None ) : <EOL> changed = True <EOL> self . _updateJobProgress ( job , progressTotal , progressCurrent , <EOL> progressMessage , notify ) <EOL> if changed : <EOL> job [ '<STR_LIT>' ] = now <EOL> job = self . save ( job ) <EOL> return job <EOL> def _updateJobProgress ( self , job , total , current , message , notify ) : <EOL> """<STR_LIT>""" <EOL> state = JobStatus . toNotificationStatus ( job [ '<STR_LIT:status>' ] ) <EOL> if current is not None : <EOL> current = float ( current ) <EOL> if total is not None : <EOL> total = float ( total ) <EOL> if job [ '<STR_LIT>' ] is None : <EOL> if notify and job [ '<STR_LIT>' ] : <EOL> notification = self . _createProgressNotification ( <EOL> job , total , current , state , message ) <EOL> notificationId = notification [ '<STR_LIT>' ] <EOL> else : <EOL> notificationId = None <EOL> job [ '<STR_LIT>' ] = { <EOL> '<STR_LIT:message>' : message , <EOL> '<STR_LIT>' : total , <EOL> '<STR_LIT>' : current , <EOL> '<STR_LIT>' : notificationId <EOL> } <EOL> else : <EOL> if total is not None : <EOL> job [ '<STR_LIT>' ] [ '<STR_LIT>' ] = total <EOL> if current is not None : <EOL> job [ '<STR_LIT>' ] [ '<STR_LIT>' ] = current <EOL> if message is not None : <EOL> job [ '<STR_LIT>' ] [ '<STR_LIT:message>' ] = message <EOL> if notify and job [ '<STR_LIT>' ] : <EOL> if job [ '<STR_LIT>' ] [ '<STR_LIT>' ] is None : <EOL> notification = self . _createProgressNotification ( <EOL> job , total , current , state , message ) <EOL> job [ '<STR_LIT>' ] [ '<STR_LIT>' ] = notification [ '<STR_LIT>' ] <EOL> self . save ( job ) <EOL> else : <EOL> notification = self . model ( '<STR_LIT>' ) . load ( <EOL> job [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> self . model ( '<STR_LIT>' ) . updateProgress ( <EOL> notification , state = state , <EOL> message = job [ '<STR_LIT>' ] [ '<STR_LIT:message>' ] , <EOL> current = job [ '<STR_LIT>' ] [ '<STR_LIT>' ] , <EOL> total = job [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> def _createProgressNotification ( self , job , total , current , state , message ) : <EOL> user = self . model ( '<STR_LIT:user>' ) . load ( job [ '<STR_LIT>' ] , force = True ) <EOL> return self . model ( '<STR_LIT>' ) . initProgress ( <EOL> user , job [ '<STR_LIT:title>' ] , total , state = state , current = current , <EOL> message = message , estimateTime = False ) <EOL> def filter ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in kwargs : <EOL> args = [ kwargs . pop ( '<STR_LIT>' ) ] + list ( args ) <EOL> job = args [ <NUM_LIT:0> ] <EOL> if '<STR_LIT:user>' in kwargs : <EOL> user = kwargs . pop ( '<STR_LIT:user>' ) <EOL> else : <EOL> user = args [ <NUM_LIT:1> ] <EOL> event = events . trigger ( '<STR_LIT>' , info = { <EOL> '<STR_LIT>' : job , <EOL> '<STR_LIT:user>' : user <EOL> } ) <EOL> keys = [ ] <EOL> if '<STR_LIT>' in kwargs : <EOL> keys . extend ( kwargs . pop ( '<STR_LIT>' ) ) <EOL> removeFields = set ( ) <EOL> for resp in event . responses : <EOL> if '<STR_LIT>' in resp : <EOL> keys . extend ( resp [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in resp : <EOL> removeFields |= set ( resp [ '<STR_LIT>' ] ) <EOL> doc = super ( Job , self ) . filter ( job , user , additionalKeys = keys , ** kwargs ) <EOL> if '<STR_LIT>' in doc and isinstance ( doc [ '<STR_LIT>' ] , six . string_types ) : <EOL> doc [ '<STR_LIT>' ] = json_util . loads ( doc [ '<STR_LIT>' ] ) <EOL> for field in removeFields : <EOL> if field in doc : <EOL> del doc [ field ] <EOL> return doc </s>
