-
Notifications
You must be signed in to change notification settings - Fork 13
/
Jobs.py
78 lines (64 loc) · 2.58 KB
/
Jobs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from .Sensor import Sensor
from .utils import GET
from .utils import DELETE
class Job( object ):
'''Representation of a Job created by Services.'''
def __init__( self, manager, data ):
self._man = manager
self.jobId = None
self.lastNarration = None
self.cause = None
self.finished = None
self.changed = None
self.created = None
self.sensors = None
self.service = None
self.activity = None
self._data = data
self._parseData( data )
def _parseData( self, data ):
self.jobId = data.get( 'job_id', None )
self.lastNarration = data.get( 'last_narration', None )
self.cause = data.get( 'cause', None )
self.finished = data.get( 'stopped', None )
self.changed = data.get( 'last_change', None )
if self.changed is not None:
# The Changed timestamp is not ms-based
# but the others are so we normalize.
self.changed = self.changed * 1000
self.created = data.get( 'created', None )
self.sensors = [ Sensor( self._man, s ) for s in data.get( 'sids', [] ) ]
self.service = data.get( 'replicant', None )
if 'record' in data:
self.activity = data[ 'record' ].get( 'hist', None )
def update( self ):
'''Fetch any updates to the job found in the cloud.'''
data = self._man._apiCall( 'job/%s/%s' % ( self._man._oid, self.jobId ), GET, queryParams = {
'is_compressed' : 'true',
'with_data' : 'false',
} )
data = self._man._unwrap( data[ 'job' ] )
self._data = data
self._parseData( data )
def fetchDetails( self ):
'''Fetch detailed activity for this job in the cloud.'''
data = self._man._apiCall( 'job/%s/%s' % ( self._man._oid, self.jobId ), GET, queryParams = {
'is_compressed' : 'true',
'with_data' : 'true',
} )
data = self._man._unwrap( data[ 'job' ] )
self._data = data
self._parseData( data )
def delete( self ):
'''Delete this job.'''
self._man._apiCall( 'job/%s/%s' % ( self._oid, self.jobId ), DELETE )
def isFinished( self ):
'''Check if this job has terminated.
Returns:
True if the job is finished.
'''
return self.finished is not None
def __str__( self ):
return "Job-%s@[%s]" % ( self.jobId, ", ".join( [ str( s ) for s in self.sensors ] ) )
def __repr__( self ):
return "Job-%s@[%s]" % ( self.jobId, ", ".join( [ str( s ) for s in self.sensors ] ) )