forked from povik/envdeploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjobs.go
172 lines (144 loc) · 3.12 KB
/
jobs.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
package main
import (
"fmt"
"log"
"net/http"
"net/http/httputil"
"os"
"path"
"sync"
"time"
)
type job struct {
ID string
Cgroup string
Owner user
Public bool
http.RoundTripper
*httputil.ReverseProxy
Stderr *os.File
StderrFn string
Dialer *pinnedDialer
Statem sync.RWMutex
Started bool
StartTime time.Time
Finished bool
FinishTime time.Time
}
type jobsMap struct {
m map[string]*job
sync.RWMutex
}
var jobs jobsMap = jobsMap{m: make(map[string]*job)}
func initJob(id string, owner user) (*job, error) {
var err error
cgroupPath := path.Join(cgroupJobsPath, id)
createCgroup(cgroupPath)
logDir := *flagLogDir
err = os.MkdirAll(logDir, 0750)
if err != nil {
return nil, err
}
stderrFn := path.Join(
logDir, /* TODO: sub-second time formatting is wrong */
fmt.Sprintf("%s_%s", time.Now().Format("060102-15040507"), id),
)
stderr, err := os.Create(stderrFn)
if err != nil {
return nil, err
}
pd := CreatePinnedDialer()
rt := &http.Transport{
Dial: pd.Dial,
MaxIdleConns: 24,
IdleConnTimeout: 1 * time.Hour,
TLSHandshakeTimeout: 10 * time.Second,
DisableCompression: true,
}
return &job{
ID: id,
Cgroup: cgroupPath,
Owner: owner,
Dialer: pd,
Stderr: stderr,
StderrFn: stderrFn,
RoundTripper: rt,
ReverseProxy: &httputil.ReverseProxy{
Director: func(req *http.Request) {
req.URL.Path = *flagBasePath + req.URL.Path
req.URL.Scheme = "http"
req.URL.Host = "127.0.0.1:8000"
},
Transport: rt,
},
}, nil
}
func (jobs *jobsMap) CreateJob(id string, owner user) (ret *job, err error) {
jobs.Lock()
defer jobs.Unlock()
if _, ok := jobs.m[id]; ok {
return nil, errJobExists
}
ret, err = initJob(id, owner)
if err != nil {
return
}
jobs.m[id] = ret
return
}
func (jobs *jobsMap) Lookup(id string) (ret *job) {
jobs.RLock()
defer jobs.RUnlock()
ret, _ = jobs.m[id]
return
}
func (jobs *jobsMap) Remove(id string) error {
jobs.RLock()
defer jobs.RUnlock()
job, ok := jobs.m[id]
if !ok {
return errJobNotFound
}
if !job.IsFinished() {
return errJobNotFinished
}
delete(jobs.m, id)
return nil
}
func (j *job) Start(cmd string, env []string, dir string) {
j.Statem.Lock()
if j.Started {
log.Printf("attempt to start already started job %s", j.ID)
j.Statem.Unlock()
return
}
j.Started = true
j.StartTime = time.Now()
j.Statem.Unlock()
RunContainedWithDialer(j.ID, cmd, env, dir, j.Cgroup, j.Stderr, j.Dialer)
/* wait for the job to get unpopulated, then Quit the pinnedDialer */
go func() {
<-waitForCgroupUnpopulated(j.Cgroup)
j.Statem.Lock()
j.Finished = true
j.FinishTime = time.Now()
j.Statem.Unlock()
j.Dialer.Quit()
err_str := Sh(fmt.Sprintf("rmdir %s", j.Cgroup))
if err_str != "" {
log.Println(err_str)
}
}()
}
func (j *job) IsFinished() bool {
j.Statem.RLock()
defer j.Statem.RUnlock()
return j.Finished
}
func (j *job) SendSignal(sig int) {
/* IMPROVE */
err_str := Sh(fmt.Sprintf("kill -s %d $(find %s -name cgroup.procs | xargs cat | sort -n | head -n 1)", sig, j.Cgroup))
if err_str != "" {
log.Println(err_str)
}
}