This repository has been archived by the owner on Mar 3, 2023. It is now read-only.
forked from dgruber/drmaa2os
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simpletrackerfake.go
150 lines (131 loc) · 3.34 KB
/
simpletrackerfake.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
package simpletrackerfakes
import (
"errors"
"fmt"
"github.com/dgruber/drmaa2interface"
"sync"
"time"
)
type JobTracker struct {
jobsession string
jobs map[string]drmaa2interface.Job
state map[string]drmaa2interface.JobState
info map[string]drmaa2interface.JobInfo
lastjobid int
sync.Mutex
}
func New(sessionname string) *JobTracker {
return &JobTracker{
jobsession: sessionname,
jobs: make(map[string]drmaa2interface.Job),
state: make(map[string]drmaa2interface.JobState),
info: make(map[string]drmaa2interface.JobInfo),
lastjobid: 0,
}
}
func (jt *JobTracker) ListJobs() ([]string, error) {
jt.Lock()
defer jt.Unlock()
var jobs []string
for id := range jt.jobs {
jobs = append(jobs, id)
}
return jobs, nil
}
func (jt *JobTracker) ListJobCategories() ([]string, error) {
return []string{"image", "otherimage"}, nil
}
func (jt *JobTracker) AddJob(t drmaa2interface.JobTemplate) (string, error) {
jt.Lock()
defer jt.Unlock()
jt.lastjobid++
jobid := fmt.Sprintf("%d", jt.lastjobid)
job := FakeJob{
id: jobid,
session: jt.jobsession,
template: t,
tracker: jt,
faketimeout: time.Millisecond * 100,
}
jt.jobs[jobid] = job
jt.state[jobid] = drmaa2interface.Running
jt.info[jobid] = drmaa2interface.JobInfo{
ID: jobid,
State: drmaa2interface.Running,
AllocatedMachines: []string{"localhost"},
SubmissionMachine: "localhost",
JobOwner: "testuser",
Slots: 1,
SubmissionTime: time.Now(),
DispatchTime: time.Now(),
}
return jobid, nil
}
func (jt *JobTracker) AddArrayJob(t drmaa2interface.JobTemplate, begin int, end int, step int, maxParallel int) (string, error) {
jt.Lock()
defer jt.Unlock()
jt.lastjobid++
jobid := fmt.Sprintf("%d", jt.lastjobid)
for i := begin; i <= end; i += step {
arrayjobid := fmt.Sprintf("%s.%d", jobid, i)
job := FakeJob{
id: arrayjobid,
session: jt.jobsession,
template: t,
tracker: jt,
}
jt.jobs[arrayjobid] = job
jt.state[arrayjobid] = drmaa2interface.Running
}
return jobid, nil
}
func (jt *JobTracker) ListArrayJobs(string) ([]string, error) {
jt.Lock()
defer jt.Unlock()
return nil, nil
}
func (jt *JobTracker) JobState(jobid string) drmaa2interface.JobState {
jt.Lock()
defer jt.Unlock()
return jt.state[jobid]
}
func (jt *JobTracker) JobInfo(jobid string) (drmaa2interface.JobInfo, error) {
jt.Lock()
defer jt.Unlock()
jinfo, exists := jt.info[jobid]
if exists == false {
return drmaa2interface.CreateJobInfo(), errors.New("job does not exist")
}
return jinfo, nil
}
func (jt *JobTracker) JobControl(jobid, state string) error {
jt.Lock()
defer jt.Unlock()
switch state {
case "suspend":
jt.state[jobid] = drmaa2interface.Suspended
case "resume":
jt.state[jobid] = drmaa2interface.Running
case "hold":
jt.state[jobid] = drmaa2interface.QueuedHeld
case "release":
jt.state[jobid] = drmaa2interface.Running
case "terminate":
jt.state[jobid] = drmaa2interface.Failed
}
return nil
}
func (jt *JobTracker) Wait(jobid string, d time.Duration, states ...drmaa2interface.JobState) error {
jt.Lock()
defer jt.Unlock()
jt.state[jobid] = states[0]
return nil
}
func (jt *JobTracker) DeleteJob(jobid string) error {
jt.Lock()
defer jt.Unlock()
delete(jt.jobs, jobid)
delete(jt.state, jobid)
delete(jt.info, jobid)
return nil
}