-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjob.go
128 lines (97 loc) · 2.93 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
package rkasync
import (
"context"
"fmt"
"gorm.io/datatypes"
"sync"
"time"
)
const (
JobStateCreated = "created"
JobStateRunning = "running"
JobStateCanceled = "canceled"
JobStateSuccess = "success"
JobStateFailed = "failed"
)
type Job struct {
// do not edit
Id string `json:"id" yaml:"id" gorm:"primaryKey"`
InvokedRole string `json:"invokedRole" yaml:"invokedRole" gorm:"index"`
InvokedInstance string `json:"invokedInstance" yaml:"invokedInstance" gorm:"index"`
State string `json:"state" yaml:"state" gorm:"index"`
CreatedAt time.Time `yaml:"createdAt" json:"createdAt" attr:"-"`
UpdatedAt time.Time `yaml:"updatedAt" json:"updatedAt"`
// edit
Type string `json:"type" yaml:"type" gorm:"index"`
UserId string `json:"userId" yaml:"userId" gorm:"index"`
Filter string `json:"filter" yaml:"filter" gorm:"text"`
Steps datatypes.JSONType[[]*Step] `json:"steps" yaml:"steps"`
Payload datatypes.JSONType[interface{}] `json:"payload" yaml:"payload"`
}
func (j *Job) TableName() string {
return "rk_async_job"
}
type NewRecorderF func() *Recorder
type Step struct {
Index int `json:"index" yaml:"index"`
Name string `json:"id" yaml:"id"`
State string `json:"state" yaml:"state"`
StartedAt time.Time `yaml:"startedAt" json:"startedAt"`
ElapsedSec float64 `yaml:"elapsedSec" json:"elapsedSec"`
Output []string `yaml:"output" json:"output"`
PersistFunc func() `json:"-" yaml:"-"`
Lock sync.Mutex `json:"-" yaml:"-"`
}
func (s *Step) NewRecorder() *Recorder {
s.Lock.Lock()
defer s.Lock.Unlock()
s.Output = append(s.Output, "")
return &Recorder{
index: len(s.Output) - 1,
startTime: time.Now(),
step: s,
}
}
func (s *Step) Finish(state string) {
s.Lock.Lock()
defer s.Lock.Unlock()
s.State = state
s.ElapsedSec = time.Now().Sub(s.StartedAt).Seconds()
s.PersistFunc()
}
type UpdateJobFunc func(j *Job) error
type Processor interface {
Process(context.Context, *Job, UpdateJobFunc) error
}
type Recorder struct {
index int
startTime time.Time
step *Step
}
func (r *Recorder) Title(s string) {
output := fmt.Sprintf("👉🏻 %s", s)
r.step.Lock.Lock()
defer r.step.Lock.Unlock()
if r.step != nil && len(r.step.Output) > r.index {
r.step.Output[r.index] = output
}
r.step.PersistFunc()
}
func (r *Recorder) Warn(s string) {
output := fmt.Sprintf("⚠️️ [%s] %s", time.Duration(time.Now().Sub(r.startTime).Seconds())*time.Second, s)
r.step.Lock.Lock()
defer r.step.Lock.Unlock()
if r.step != nil && len(r.step.Output) > r.index {
r.step.Output[r.index] = output
}
r.step.PersistFunc()
}
func (r *Recorder) Info(s string) {
output := fmt.Sprintf("[%s] %s", time.Duration(time.Now().Sub(r.startTime).Seconds())*time.Second, s)
r.step.Lock.Lock()
defer r.step.Lock.Unlock()
if r.step != nil && len(r.step.Output) > r.index {
r.step.Output[r.index] = output
}
r.step.PersistFunc()
}