-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_job.go
56 lines (49 loc) · 1.35 KB
/
service_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
package cron
import (
context "context"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/swarm"
log "github.com/sirupsen/logrus"
)
// ServiceJob run a docker service task on a schedule.
type ServiceJob struct {
Schedule string
Action string
ServiceID string
ServiceName string
ServiceVersion swarm.Version
ServiceCreatedAt time.Time
Service swarm.Service
cron *Cron
cli DockerClient
}
// Run a docker container and log the output.
func (j *ServiceJob) Run() {
log := log.WithFields(log.Fields{
"func": "ServiceJob.Run",
"schedule": j.Schedule,
"action": j.Action,
"service.ID": j.ServiceID,
"service.Name": j.ServiceName,
})
// TODO: add all property of Service in log Fields
j.cron.sync.Add(1)
defer j.cron.sync.Done()
defer j.cli.Close()
var err error
switch j.Action {
case "update":
var r types.ServiceUpdateResponse
j.Service.Spec.TaskTemplate.ForceUpdate = j.ServiceVersion.Index
r, err = j.cli.ServiceUpdate(context.Background(), j.ServiceID, j.ServiceVersion, j.Service.Spec, types.ServiceUpdateOptions{})
for _, w := range r.Warnings {
log.Warning(w)
}
}
if err != nil {
log.WithError(err).Errorln("service job completed with error")
} else {
log.Infoln("service action completed successfully")
}
}