-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcronify.go
150 lines (130 loc) · 3.08 KB
/
cronify.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 cronify
import (
"context"
"fmt"
"log"
"os"
"sync"
"time"
"github.com/docker/docker/client"
)
// Cronify
type Cronify struct {
DockerClient *client.Client
MaxConcurrentJobs int
JobList []*Job
sync sync.Mutex
}
func (c *Cronify) AddJob(job *Job) {
c.sync.Lock()
err := job.scheduleNextRun()
if err != nil {
log.Printf("Could not add job %s: %v", job.JobName, err)
return
}
c.JobList = append(c.JobList, job)
c.sync.Unlock()
}
func (c *Cronify) RemoveJobsByContainerID(containerID string) {
c.sync.Lock()
for i := 0; i < len(c.JobList); i++ {
if c.JobList[i].containerID == containerID {
c.JobList = append(c.JobList[:i], c.JobList[i+1:]...)
}
}
c.sync.Unlock()
}
func (c *Cronify) nextJobs() []*Job {
var jobs []*Job
for _, j := range c.JobList {
if j.shouldRun() {
jobs = append(jobs, j)
}
}
return jobs
}
// runJob handles the whole job flow
func (c *Cronify) runJob(ctx context.Context, job *Job) error {
if job.active {
return fmt.Errorf("job is still active")
}
job.active = true
// start go routine
go func(c *Cronify, ctx context.Context, job *Job) {
//todo: decouple context here to cancel the workflow
/// main run
success, err := c.execute(ctx, job.Run)
log.Printf("success: %v, error: %v\n", success, err)
// success or fail runs
var postJobs map[string]*JobTypeConfig
if success {
postJobs = job.Success
} else {
postJobs = job.Fail
}
for i, conf := range postJobs {
log.Printf("start post job: %s", i)
success, err := c.execute(ctx, conf)
log.Printf("success: %v, error: %v\n", success, err)
}
///
job.lastRun = job.nextRun
if err := job.scheduleNextRun(); err != nil {
log.Printf("Could not schedule job '%s' for '%s': %s\n", job.JobName, job.containerID, err.Error())
}
job.active = false
}(c, ctx, job)
return nil
}
// execute handles direct Run/Success/Fail executions
func (c *Cronify) execute(ctx context.Context, config *JobTypeConfig) (bool, error) {
exec, err := config.NewExecution(c.DockerClient)
if err != nil {
return false, err
}
var (
cancelCtx context.CancelFunc
execCtx context.Context
)
if config.Timeout.Seconds() > 0 {
execCtx, cancelCtx = context.WithTimeout(ctx, config.Timeout)
} else {
execCtx, cancelCtx = context.WithCancel(ctx)
}
defer cancelCtx()
return exec.execute(execCtx, os.Stdout)
}
// Start cronify main process
func (c *Cronify) Start() chan bool {
stopped := make(chan bool, 1)
ticker := time.NewTicker(1 * time.Second)
// todo: implement context cancelation
//var ctxs []context.Context
go func() {
Schedule:
for {
select {
case <-ticker.C:
jobs := c.nextJobs()
if len(jobs) == 0 {
continue
}
for _, job := range jobs {
// concurrent run ?
ctx := context.Background()
err := c.runJob(ctx, job)
if err != nil {
log.Printf("Could not start job '%s' for '%s': %s\n", job.JobName, job.containerID, err.Error())
}
}
case <-stopped:
break Schedule
}
}
// cancel every job
//for _, ctx := range ctxs {
// ctx.Done()
//}
}()
return stopped
}