-
Notifications
You must be signed in to change notification settings - Fork 5
/
scheduler.go
316 lines (282 loc) · 7.83 KB
/
scheduler.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package scheduler
import (
"context"
"fmt"
"io"
"log"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"sync/atomic"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
"github.com/kballard/go-shellquote"
"github.com/robfig/cron/v3"
)
const (
composeProjectLabel = "com.docker.compose.project"
composeServiceLabel = "com.docker.compose.service"
schedulerLabel = "net.reddec.scheduler.cron"
commandLabel = "net.reddec.scheduler.exec"
logsLabel = "net.reddec.scheduler.logs"
)
func Create(ctx context.Context, options ...Option) (*Scheduler, error) {
sc := &Scheduler{}
for _, opt := range options {
opt(sc)
}
if sc.client == nil {
dockerClient, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
return nil, fmt.Errorf("create docker client: %w", err)
}
sc.client = dockerClient
sc.borrowed = false
}
if sc.project == "" {
project, err := getComposeProject(ctx, sc.client)
if err != nil {
_ = sc.Close()
return nil, fmt.Errorf("get compose project: %w", err)
}
sc.project = project
}
return sc, nil
}
type Task struct {
Service string
Container string
Schedule string
Command []string
logging bool
}
type Scheduler struct {
project string
client *client.Client
borrowed bool
notification *HTTPNotification
}
func (sc *Scheduler) Close() error {
if sc.borrowed {
return nil
}
return sc.client.Close()
}
func (sc *Scheduler) Run(ctx context.Context) error {
tasks, err := sc.listTasks(ctx)
if err != nil {
return fmt.Errorf("list tasks: %w", err)
}
engine := cron.New()
for _, t := range tasks {
log.Println("task for service", t.Service, "at", t.Schedule, "| logging:", t.logging)
running := new(int32)
t := t
_, err = engine.AddFunc(t.Schedule, func() {
sc.runJob(ctx, running, t)
})
if err != nil {
return fmt.Errorf("add service %s: %w", t.Service, err)
}
}
engine.Start()
<-ctx.Done()
<-engine.Stop().Done()
return nil
}
func (sc *Scheduler) runJob(ctx context.Context, running *int32, t Task) {
started := time.Now()
err := sc.runTask(ctx, running, t)
end := time.Now()
var errMessage string
if err != nil {
errMessage = err.Error()
log.Println("service", t.Service, "failed after", end.Sub(started), "with error:", err)
} else {
log.Println("service", t.Service, "finished after", end.Sub(started), "successfully")
}
if sc.notification == nil {
return
}
err = sc.notification.Notify(ctx, &Payload{
Project: sc.project,
Service: t.Service,
Container: t.Container,
Schedule: t.Schedule,
Started: started,
Finished: end,
Failed: err != nil,
Error: errMessage,
})
if err != nil {
log.Println("notification for service", t.Service, "failed:", err)
} else {
log.Println("notification for service", t.Service, "succeeded")
}
}
func (sc *Scheduler) runTask(ctx context.Context, running *int32, task Task) error {
if !atomic.CompareAndSwapInt32(running, 0, 1) {
return fmt.Errorf("task is running")
}
defer atomic.StoreInt32(running, 0)
if len(task.Command) == 0 {
log.Println("running service", task.Service)
return sc.runService(ctx, task)
}
log.Println("executing service", task.Service, "with command", task.Command)
return sc.execService(ctx, task)
}
func (sc *Scheduler) execService(ctx context.Context, task Task) error {
if task.logging {
return sc.execAttachService(ctx, task)
} else {
return sc.execStartService(ctx, task)
}
}
func (sc *Scheduler) execStartService(ctx context.Context, task Task) error {
execID, err := sc.client.ContainerExecCreate(ctx, task.Container, types.ExecConfig{
Cmd: task.Command,
})
if err != nil {
return fmt.Errorf("create exec for %s: %w", task.Service, err)
}
err = sc.client.ContainerExecStart(ctx, execID.ID, types.ExecStartCheck{})
if err != nil {
return fmt.Errorf("exec for %s: %w", task.Service, err)
}
return nil
}
func (sc *Scheduler) execAttachService(ctx context.Context, task Task) error {
execID, err := sc.client.ContainerExecCreate(ctx, task.Container, types.ExecConfig{
Cmd: task.Command,
AttachStderr: true,
AttachStdout: true,
})
if err != nil {
return fmt.Errorf("create exec for %s: %w", task.Service, err)
}
attach, err := sc.client.ContainerExecAttach(ctx, execID.ID, types.ExecStartCheck{})
if err != nil {
return fmt.Errorf("exec for %s: %w", task.Service, err)
}
defer attach.Close()
io.Copy(log.Writer(), attach.Reader)
inspect, err := sc.client.ContainerExecInspect(ctx, execID.ID)
if err != nil {
return fmt.Errorf("inspect exec for %s: %w", task.Service, err)
}
if inspect.ExitCode != 0 {
return fmt.Errorf("command returned non-zero code %d", inspect.ExitCode)
}
return nil
}
func (sc *Scheduler) runService(ctx context.Context, task Task) error {
err := sc.client.ContainerStart(ctx, task.Container, types.ContainerStartOptions{})
if err != nil {
return fmt.Errorf("start service %s: %w", task.Service, err)
}
ok, failed := sc.client.ContainerWait(ctx, task.Container, container.WaitConditionNotRunning)
select {
case res := <-ok:
if res.Error != nil {
return fmt.Errorf("service %s: %s", task.Service, res.Error.Message)
}
if res.StatusCode != 0 {
return fmt.Errorf("service %s: status code %d", task.Service, res.StatusCode)
}
case err = <-failed:
return fmt.Errorf("wait for service %s: %w", task.Service, err)
}
return nil
}
func (sc *Scheduler) listTasks(ctx context.Context) ([]Task, error) {
list, err := sc.client.ContainerList(ctx, types.ContainerListOptions{
Filters: filters.NewArgs(
filters.Arg("label", composeProjectLabel+"="+sc.project),
filters.Arg("label", composeServiceLabel),
filters.Arg("label", schedulerLabel),
),
All: true,
})
if err != nil {
return nil, fmt.Errorf("list container: %w", err)
}
var ans = make([]Task, 0, len(list))
for _, c := range list {
service := c.Labels[composeServiceLabel]
var args []string
if v := c.Labels[commandLabel]; v != "" {
cmd, err := shellquote.Split(v)
if err != nil {
return nil, fmt.Errorf("parse command in service %s: %w", service, err)
}
args = cmd
}
isLoggingEnabled, err := strconv.ParseBool(c.Labels[logsLabel])
if err != nil {
isLoggingEnabled = false
}
ans = append(ans, Task{
Container: c.ID,
Schedule: c.Labels[schedulerLabel],
Service: service,
Command: args,
logging: isLoggingEnabled,
})
}
return ans, nil
}
func containerID() (string, error) {
const path = `/proc/1/cpuset`
data, err := os.ReadFile(path)
if err != nil {
return "", fmt.Errorf("detect container ID: %w", err)
}
id := filepath.Base(strings.TrimSpace(string(data)))
if id == "/" || id == ".." {
return "", fmt.Errorf("calculate container ID from %s: %w", string(data), err)
}
return id, nil
}
func getComposeProject(ctx context.Context, dockerClient *client.Client) (string, error) {
var cID string
for _, lookup := range containerIDLookup {
v, err := lookup()
if err == nil {
cID = v
break
}
}
if cID == "" {
return "", fmt.Errorf("failed detect self container ID")
}
info, err := dockerClient.ContainerInspect(ctx, cID)
if err != nil {
return "", fmt.Errorf("inspect self container: %w", err)
}
project, ok := info.Config.Labels[composeProjectLabel]
if !ok {
return "", fmt.Errorf("compose label not found - probably container is not part of compose")
}
return project, nil
}
func containerIDv2() (string, error) {
content, err := os.ReadFile("/proc/self/mountinfo")
if err != nil {
return "", err
}
idRegex := regexp.MustCompile(`containers/([[:alnum:]]{64})/`)
matches := idRegex.FindStringSubmatch(string(content))
if len(matches) == 0 {
return "", fmt.Errorf("no container id")
}
return matches[len(matches)-1], nil
}
var containerIDLookup = []func() (string, error){
containerID, containerIDv2,
}