-
Notifications
You must be signed in to change notification settings - Fork 14
/
job.go
185 lines (176 loc) · 6.69 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
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
package gobatch
import (
"context"
"github.com/chararch/gobatch/status"
"reflect"
"runtime/debug"
"time"
)
//Job job interface used by GoBatch
type Job interface {
Name() string
Start(ctx context.Context, execution *JobExecution) BatchError
Stop(ctx context.Context, execution *JobExecution) BatchError
GetSteps() []Step
}
type simpleJob struct {
name string
steps []Step
listeners []JobListener
}
func newSimpleJob(name string, steps []Step, listeners []JobListener) *simpleJob {
return &simpleJob{
name: name,
steps: steps,
listeners: listeners,
}
}
func (job *simpleJob) Name() string {
return job.name
}
func (job *simpleJob) Start(ctx context.Context, execution *JobExecution) (err BatchError) {
defer func() {
if er := recover(); er != nil {
logger.Error(ctx, "panic in job executing, jobName:%v, jobExecutionId:%v, err:%v, stack:%v", job.name, execution.JobExecutionId, er, string(debug.Stack()))
execution.JobStatus = status.FAILED
execution.FailError = NewBatchError(ErrCodeGeneral, "panic in job execution", er)
execution.EndTime = time.Now()
}
if err != nil {
execution.JobStatus = status.FAILED
execution.FailError = err
execution.EndTime = time.Now()
}
if err = saveJobExecution(execution); err != nil {
logger.Error(ctx, "save job execution failed, jobName:%v, JobExecution:%+v, err:%v", job.name, execution, err)
}
}()
logger.Info(ctx, "start running job, jobName:%v, jobExecutionId:%v", job.name, execution.JobExecutionId)
for _, listener := range job.listeners {
err = listener.BeforeJob(execution)
if err != nil {
logger.Error(ctx, "job listener execute err, jobName:%v, jobExecutionId:%+v, listener:%v, err:%v", job.name, execution.JobExecutionId, reflect.TypeOf(listener).String(), err)
execution.JobStatus = status.FAILED
execution.FailError = err
execution.EndTime = time.Now()
return nil
}
}
execution.JobStatus = status.STARTED
execution.StartTime = time.Now()
if err = saveJobExecution(execution); err != nil {
logger.Error(ctx, "save job execution failed, jobName:%v, JobExecution:%+v, err:%v", job.name, execution, err)
return err
}
jobStatus := status.COMPLETED
for _, step := range job.steps {
e := execStep(ctx, step, execution)
if e != nil {
logger.Error(ctx, "execute step failed, jobExecutionId:%v, step:%v, err:%v", execution.JobExecutionId, step.Name(), err)
if e.Code() == ErrCodeStop {
jobStatus = status.STOPPED
} else {
jobStatus = status.FAILED
}
break
}
if execution.JobStatus == status.FAILED || execution.JobStatus == status.UNKNOWN {
jobStatus = execution.JobStatus
break
}
}
execution.JobStatus = jobStatus
execution.EndTime = time.Now()
for _, listener := range job.listeners {
err = listener.AfterJob(execution)
if err != nil {
logger.Error(ctx, "job listener execute err, jobName:%v, jobExecutionId:%+v, listener:%v, err:%v", job.name, execution.JobExecutionId, reflect.TypeOf(listener).String(), err)
execution.JobStatus = status.FAILED
execution.FailError = err
execution.EndTime = time.Now()
break
}
}
logger.Info(ctx, "finish job execution, jobName:%v, jobExecutionId:%v, jobStatus:%v", job.name, execution.JobExecutionId, execution.JobStatus)
return nil
}
func execStep(ctx context.Context, step Step, execution *JobExecution) (err BatchError) {
defer func() {
if err != nil && err.Code() != ErrCodeStop {
logger.Error(ctx, "error in step executing, jobExecutionId:%v, stepName:%v, err:%v", execution.JobExecutionId, step.Name(), err)
}
}()
lastStepExecution, er := findLastStepExecution(execution.JobInstanceId, step.Name())
if er != nil {
err = er
logger.Error(ctx, "find last StepExecution failed, jobExecutionId:%v, stepName:%v, err:%v", execution.JobExecutionId, step.Name(), er)
return er
}
if lastStepExecution != nil && lastStepExecution.StepStatus == status.COMPLETED {
logger.Info(ctx, "skip completed step, jobExecutionId:%v, stepName:%v", execution.JobExecutionId, step.Name())
return nil
}
if lastStepExecution != nil && (lastStepExecution.StepStatus == status.STARTING || lastStepExecution.StepStatus == status.STARTED || lastStepExecution.StepStatus == status.STOPPING) {
logger.Error(ctx, "last StepExecution is in progress, jobExecutionId:%v, stepName:%v", execution.JobExecutionId, step.Name())
return NewBatchError(ErrCodeConcurrency, "last StepExecution of the Step:%v is in progress", step.Name())
}
stepExecution := &StepExecution{
StepName: step.Name(),
StepStatus: status.STARTING,
StepContext: NewBatchContext(),
StepExecutionContext: NewBatchContext(),
JobExecution: execution,
CreateTime: time.Now(),
}
if lastStepExecution != nil {
stepExecution.StepContext.Merge(lastStepExecution.StepContext)
stepExecution.StepContextId = lastStepExecution.StepContextId
stepExecution.StepExecutionContext.Merge(lastStepExecution.StepExecutionContext)
}
e := saveStepExecution(ctx, stepExecution)
if e != nil {
logger.Error(ctx, "save step execution failed, jobExecutionId:%v, stepName:%v, err:%v", execution.JobExecutionId, step.Name(), e)
err = e
return err
}
execution.AddStepExecution(stepExecution)
err = step.Exec(ctx, stepExecution)
if err != nil || stepExecution.StepStatus != status.COMPLETED {
logger.Error(ctx, "step executing failed, jobExecutionId:%v, stepName:%v, stepStatus:%v, err:%v", execution.JobExecutionId, step.Name(), stepExecution.StepStatus, e)
execution.JobStatus = stepExecution.StepStatus
if err != nil && stepExecution.StepStatus != status.FAILED {
stepExecution.StepStatus = status.FAILED
stepExecution.FailError = err
stepExecution.EndTime = time.Now()
execution.JobStatus = status.FAILED
}
execution.JobStatus = status.FAILED
execution.FailError = err
execution.EndTime = time.Now()
e = saveStepExecution(ctx, stepExecution)
if e != nil {
logger.Error(ctx, "save step execution failed, jobExecutionId:%v, stepName:%v, err:%v", execution.JobExecutionId, step.Name(), e)
err = e
return err
}
} else {
stepExecution.StepStatus = status.COMPLETED
stepExecution.EndTime = time.Now()
e = saveStepExecution(ctx, stepExecution)
if e != nil {
logger.Error(ctx, "save step execution failed, jobExecutionId:%v, stepName:%v, err:%v", execution.JobExecutionId, step.Name(), e)
err = e
return err
}
}
return nil
}
func (job *simpleJob) Stop(ctx context.Context, execution *JobExecution) BatchError {
logger.Info(ctx, "stop job, jobName:%v, jobExecutionId:%v, jobStatus:%v", job.name, execution.JobExecutionId, execution.JobStatus)
execution.JobStatus = status.STOPPING
execution.EndTime = time.Now()
return saveJobExecution(execution)
}
func (job *simpleJob) GetSteps() []Step {
return job.steps
}