-
Notifications
You must be signed in to change notification settings - Fork 1
/
entry.go
225 lines (185 loc) · 4.89 KB
/
entry.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package rkasync
import (
"context"
"encoding/json"
"errors"
"github.com/rookie-ninja/rk-entry/v2/entry"
"go.uber.org/zap"
"sync"
)
func init() {
rkentry.RegisterUserEntryRegFunc(RegisterEntriesFromConfig)
}
var (
dbRegFuncM = map[string]func(map[string]string, *zap.Logger) Database{}
)
func GetEntry() *Entry {
res := rkentry.GlobalAppCtx.GetEntry("RkAsyncEntry", "rk-async-entry")
if res == nil {
return nil
}
if v, ok := res.(*Entry); ok {
return v
}
return nil
}
func RegisterDatabaseRegFunc(dbType string, f func(map[string]string, *zap.Logger) Database) {
dbRegFuncM[dbType] = f
}
func RegisterEntriesFromConfig(raw []byte) map[string]rkentry.Entry {
res := make(map[string]rkentry.Entry)
// 1: decode config map into boot config struct
config := &BootConfig{}
rkentry.UnmarshalBootYAML(raw, config)
// 3: construct entry
if config.Async.Enabled {
entry := &Entry{
config: config,
bootstrapOnce: sync.Once{},
}
res[entry.GetName()] = entry
rkentry.GlobalAppCtx.AddEntry(entry)
}
return res
}
type BootConfig struct {
Async struct {
Enabled bool `json:"enabled" yaml:"enabled"`
Logger string `json:"logger" yaml:"logger"`
Event string `json:"event" yaml:"event"`
Database struct {
MySql struct {
Enabled bool `json:"enabled" yaml:"enabled"`
EntryName string `json:"entryName" yaml:"entryName"`
Database string `json:"database" yaml:"database"`
} `yaml:"mySql" json:"mySql"`
Postgres struct {
Enabled bool `json:"enabled" yaml:"enabled"`
EntryName string `json:"entryName" yaml:"entryName"`
Database string `json:"database" yaml:"database"`
} `yaml:"postgres" json:"postgres"`
} `yaml:"database" json:"database"`
Worker struct {
Local struct {
Enabled bool `json:"enabled" yaml:"enabled"`
} `yaml:"local" json:"local"`
} `yaml:"worker" json:"worker"`
} `yaml:"async" json:"async"`
}
type Entry struct {
db Database
config *BootConfig
worker Worker
bootstrapOnce sync.Once
}
func (e *Entry) Bootstrap(ctx context.Context) {
e.bootstrapOnce.Do(func() {
// logger
logger := rkentry.GlobalAppCtx.GetLoggerEntry(e.config.Async.Logger)
if logger == nil {
logger = rkentry.GlobalAppCtx.GetLoggerEntryDefault()
}
// event
event := rkentry.GlobalAppCtx.GetEventEntry(e.config.Async.Event)
if event == nil {
event = rkentry.GlobalAppCtx.GetEventEntryDefault()
}
var db Database
if e.config.Async.Database.MySql.Enabled {
f := dbRegFuncM["MySQL"]
db = f(map[string]string{
"entryName": e.config.Async.Database.MySql.EntryName,
"database": e.config.Async.Database.MySql.Database,
}, logger.Logger)
}
if e.config.Async.Database.Postgres.Enabled {
f := dbRegFuncM["PostgreSQL"]
db = f(map[string]string{
"entryName": e.config.Async.Database.Postgres.EntryName,
"database": e.config.Async.Database.Postgres.Database,
}, logger.Logger)
}
if db == nil {
rkentry.ShutdownWithError(errors.New("db is nil"))
}
e.db = db
// worker
if e.config.Async.Worker.Local.Enabled {
e.worker = NewLocalWorker(db, logger, event)
}
})
}
func (e *Entry) Interrupt(ctx context.Context) {}
func (e *Entry) GetName() string {
return "rk-async-entry"
}
func (e *Entry) GetType() string {
return "RkAsyncEntry"
}
func (e *Entry) GetDescription() string {
return "async job entry"
}
func (e *Entry) String() string {
m := map[string]interface{}{
"dbType": e.db.Type(),
}
b, _ := json.Marshal(m)
return string(b)
}
func (e *Entry) StartWorker() {
if e.worker != nil {
e.worker.Start()
}
}
func (e *Entry) StopWorker(force bool, waitSec int) {
if e.worker != nil {
e.worker.Stop(force, waitSec)
}
}
func (e *Entry) Worker() Worker {
return e.worker
}
func (e *Entry) Database() Database {
return e.db
}
func (e *Entry) AddJob(job *Job) error {
return e.db.AddJob(job)
}
func (e *Entry) DeleteJob(jobId string) error {
return e.db.DeleteJob(jobId)
}
func (e *Entry) StartJob(job *Job) error {
job.State = JobStateRunning
return e.db.UpdateJobState(job)
}
func (e *Entry) FinishJob(job *Job, success bool) error {
if success {
job.State = JobStateSuccess
} else {
job.State = JobStateFailed
}
return e.db.UpdateJobState(job)
}
func (e *Entry) CancelJob(job *Job) error {
job.State = JobStateCanceled
for i := range job.Steps.Data {
step := job.Steps.Data[i]
step.State = JobStateCanceled
}
return e.db.UpdateJobState(job)
}
func (e *Entry) UpdateJobPayloadAndStep(job *Job) error {
return e.db.UpdateJobPayloadAndStep(job)
}
func (e *Entry) ListJobs(filter *JobFilter) ([]*Job, int, error) {
return e.db.ListJobs(filter)
}
func (e *Entry) GetJob(id string) (*Job, error) {
return e.db.GetJob(id)
}
func (e *Entry) CancelJobsOverdue(days int, filter *JobFilter) error {
return e.db.CancelJobsOverdue(days, filter)
}
func (e *Entry) CleanJobs(days int, filter *JobFilter) error {
return e.db.CleanJobs(days, filter)
}