-
Notifications
You must be signed in to change notification settings - Fork 0
/
job.go
82 lines (75 loc) · 1.85 KB
/
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
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
package batcher
import (
"log"
)
type JobConfig func(*defaultJob)
type JobListener func(*JobExecutionContext)
type defaultJob struct {
name string
steps []StepExecutor
beforeAllStep JobListener
afterAllStep JobListener
beforeEachStep JobListener
afterEachStep JobListener
}
func (job *defaultJob) Steps(steps ...StepExecutor) {
job.steps = steps
}
func (job *defaultJob) Configure(configs ...JobConfig) {
for _, config := range configs {
config(job)
}
}
func (job *defaultJob) Run() {
log.Printf("Start running job: %v with %d steps\n", job.name, len(job.steps))
ctx := NewJobExecutionContext()
if job.beforeAllStep != nil {
job.beforeAllStep(ctx)
}
for _, step := range job.steps {
log.Println("----------------------------")
ctx.CurrentStep = step.name()
if job.beforeEachStep != nil {
job.beforeEachStep(ctx)
}
if err := step.execute(); err != nil {
log.Fatal(err)
}
if job.afterEachStep != nil {
job.afterEachStep(ctx)
}
}
if job.afterAllStep != nil {
job.afterAllStep(ctx)
}
}
func NewJobInline(name string, steps ...StepExecutor) *defaultJob {
return &defaultJob{name: name, steps: steps}
}
func NewJob(name string, configs ...JobConfig) *defaultJob {
job := &defaultJob{name: name}
for _, config := range configs {
config(job)
}
return job
}
func BeforeEachStep(beforeEachListener JobListener) JobConfig {
return func(job *defaultJob) {
job.beforeEachStep = beforeEachListener
}
}
func BeforeAllStep(beforeAllListener JobListener) JobConfig {
return func(job *defaultJob) {
job.beforeAllStep = beforeAllListener
}
}
func AfterAllStep(afterAllListener JobListener) JobConfig {
return func(job *defaultJob) {
job.afterAllStep = afterAllListener
}
}
func AfterEachStep(afterEachListener JobListener) JobConfig {
return func(job *defaultJob) {
job.afterEachStep = afterEachListener
}
}