-
Notifications
You must be signed in to change notification settings - Fork 11
/
job.go
174 lines (154 loc) · 6.18 KB
/
job.go
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package drmaa2os
import (
"fmt"
"time"
"github.com/dgruber/drmaa2interface"
"github.com/dgruber/drmaa2os/pkg/jobtracker"
)
type OriginType int
const (
OriginJobSession OriginType = iota
OriginMonitoringSession
)
// Job represents a single computational activity that is
// executed by the DRM system. There are three relevant method sets
// for working with jobs: The JobSession interface represents all
// control and monitoring functions available for jobs. The Job
// interface represents the common control functionality for one
// existing job. Sets of jobs resulting from a bulk submission are
// controllable as a whole by the JobArray interface.
type Job struct {
id string
origin OriginType
session string
template drmaa2interface.JobTemplate
tracker jobtracker.JobTracker // reference to external job tracker
monitorer jobtracker.Monitorer // reference to external job tracker
}
// newMonitoringJob creates a DRMAA2OS job implementing the drmaa2interface.Job interface.
func newJob(id, session string, jt drmaa2interface.JobTemplate, tracker jobtracker.JobTracker) *Job {
return &Job{
id: id,
origin: OriginJobSession,
session: session,
template: jt,
tracker: tracker,
}
}
// newMonitoringJob creates a DRMAA2OS job implementing the drmaa2interface.Job interface
// which can't be modified (read-only).
func newMonitoringJob(id, session string, jt drmaa2interface.JobTemplate, tracker jobtracker.JobTracker, monitorer jobtracker.Monitorer) *Job {
job := newJob(id, session, jt, tracker)
job.origin = OriginMonitoringSession // read only for now
job.monitorer = monitorer
return job
}
// GetID returns the job identifier assigned by the DRM system in text form. This
// method is expected to be used as a fast alternative to the fetching of a complete
// JobInfo instance.
func (j *Job) GetID() string {
return j.id
}
// GetSessionName reports the name of the JobSession that was used to create
// the job. If the session name cannot be determined, for example since the
// job was created outside of a DRMAA session, the attribute SHOULD be
// UNSET (i.e. equals "").
func (j *Job) GetSessionName() string {
return j.session
}
// GetJobTemplate returns a reference to a JobTemplate instance that has
// equal values to the one that was used for the job submission creating this
// Job instance.
// For jobs created outside of a DRMAA session, implementations MUST also return a
// JobTemplate instance here, which MAY be empty or only partially filled.
func (j *Job) GetJobTemplate() (drmaa2interface.JobTemplate, error) {
return j.template, nil
}
// GetJobInfo returns a JobInfo instance for the particular job.
func (j *Job) GetJobInfo() (drmaa2interface.JobInfo, error) {
if j.origin == OriginMonitoringSession {
return j.monitorer.JobInfoFromMonitor(j.id)
}
return j.tracker.JobInfo(j.id)
}
// GetState allows the application to get the current status of the job
// according to the DRMAA state model, together with an implementation
// specific sub state (see Section 8.1). It is intended as a fast
// alternative to the fetching of a complete JobInfo instance.
func (j *Job) GetState() drmaa2interface.JobState {
// state of a monitoring job might be determined in a different way
if j.origin == OriginMonitoringSession {
ji, err := j.monitorer.JobInfoFromMonitor(j.id)
if err != nil {
return drmaa2interface.Undetermined
}
return ji.State
}
state, _, _ := j.tracker.JobState(j.id)
return state
}
// Suspend triggers a job state transition from RUNNING to SUSPENDED state.
func (j *Job) Suspend() error {
if j.origin == OriginMonitoringSession {
return fmt.Errorf("MonitoringSession jobs can't be suspended")
}
return j.tracker.JobControl(j.id, jobtracker.JobControlSuspend)
}
// Resume triggers a job state transition from SUSPENDED to RUNNING state.
func (j *Job) Resume() error {
if j.origin == OriginMonitoringSession {
return fmt.Errorf("MonitoringSession jobs can't be resumed")
}
return j.tracker.JobControl(j.id, jobtracker.JobControlResume)
}
// Hold triggers a transition from QUEUED to QUEUED_HELD,
// or from REQUEUED to REQUEUED_HELD state.
func (j *Job) Hold() error {
if j.origin == OriginMonitoringSession {
return fmt.Errorf("MonitoringSession jobs can't be held")
}
return j.tracker.JobControl(j.id, jobtracker.JobControlHold)
}
// Release triggers a transition from QUEUED_HELD to QUEUED,
// or from REQUEUED_HELD to REQUEUED state.
func (j *Job) Release() error {
if j.origin == OriginMonitoringSession {
return fmt.Errorf("MonitoringSession jobs can't be released")
}
return j.tracker.JobControl(j.id, jobtracker.JobControlRelease)
}
// Terminate triggers a transition from any of the "Started"
// states to one of the "Terminated" states.
func (j *Job) Terminate() error {
if j.origin == OriginMonitoringSession {
return fmt.Errorf("MonitoringSession jobs can't be terminated")
}
return j.tracker.JobControl(j.id, jobtracker.JobControlTerminate)
}
// WaitStarted blocks until the job entered one of the
// "Started" states.
func (j *Job) WaitStarted(timeout time.Duration) error {
return j.tracker.Wait(j.id, timeout, drmaa2interface.Running, drmaa2interface.Failed, drmaa2interface.Done)
}
// WaitTerminated blocks until the job entered one of the "Terminated" states
func (j *Job) WaitTerminated(timeout time.Duration) error {
return j.tracker.Wait(j.id, timeout, drmaa2interface.Done, drmaa2interface.Failed)
}
// Reap is intended to let the DRMAA implementation clean up any data
// about this job. The motivating factor are long-running applications
// maintaining large amounts of jobs as part of a monitoring session.
// Using a reaped job in any subsequent activity MUST generate an
// InvalidArgumentException for the job parameter.
// This function MUST only work for jobs in "Terminated" states, so that
// the job is promised to not change its status while being reaped.
// Jobs from Monitoring Sessions can't be reaped as they are read-only.
func (j *Job) Reap() error {
state, _, _ := j.tracker.JobState(j.id)
if state != drmaa2interface.Done && state != drmaa2interface.Failed {
return ErrorInvalidState
}
if j.origin == OriginMonitoringSession {
return fmt.Errorf("MonitoringSession jobs can't be reaped")
}
return j.tracker.DeleteJob(j.id)
}