-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoption.go
391 lines (346 loc) · 10 KB
/
option.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
package gta
import (
"context"
"fmt"
"time"
"github.com/panjf2000/ants/v2"
"github.com/sirupsen/logrus"
"gorm.io/gorm"
)
func newOptions(db *gorm.DB, table string, opts ...Option) (*options, error) {
options, err := newDefaultOptions().apply(append(optionGroup{withDB(db), withTable(table)}, opts...))
if err != nil {
return nil, err
}
return options, nil
}
// options contains all options of a TaskManager.
type options struct {
// must provide, db for async task table
db *gorm.DB
// must provide, async task table name
table string
// optional, context for the task mansger
context context.Context
// optional, logger factory
loggerFactory func(ctx context.Context) Logger
// optional, grouped time options
groupedTimeOptions
// optional, wait timeout in Stop() process
waitTimeout time.Duration
// optional, context marshaler to store or recover a context
ctxMarshaler CtxMarshaler
// optional, callback function for abnormal tasks
checkCallback func(logger Logger, abnormalTasks []Task)
// optional, flag for dry run mode
dryRun bool
// optional, goroutine pool size for scheduling tasks
poolSize int
// optional, task register
taskRegister taskRegister
// global cancel function
cancelFunc context.CancelFunc
}
func (s *options) apply(opts ...Option) (*options, error) {
for _, opt := range opts {
opt.apply(s)
}
for _, opt := range opts {
if err := opt.verify(s); err != nil {
return s, err
}
}
return s, nil
}
func (s *options) logger() Logger {
return s.loggerFactory(s.context)
}
func (s *options) done() <-chan struct{} {
return s.context.Done()
}
func (s *options) cancel() {
s.cancelFunc()
}
func (s *options) getDB() *gorm.DB {
return s.db
}
// Logger is a logging interface for logging necessary messages.
type Logger interface {
Printf(format string, args ...interface{})
Infof(format string, args ...interface{})
Warnf(format string, args ...interface{})
Errorf(format string, args ...interface{})
}
// CtxMarshaler is used to marshal or unmarshal context.
type CtxMarshaler interface {
MarshalCtx(ctx context.Context) ([]byte, error)
UnmarshalCtx(bytes []byte) (context.Context, error)
}
type groupedTimeOptions struct {
// optional, determine when a normal task can be cleaned
storageTimeout time.Duration
// optional, determine whether a initialized task is abnormal
initializedTimeout time.Duration
// optional, determine whether a running task is abnormal
runningTimeout time.Duration
// optional, scan interval
scanInterval time.Duration
// optional, instant scan interval
instantScanInterval time.Duration
}
func (t groupedTimeOptions) verify() error {
if t.storageTimeout <= 0 || t.initializedTimeout <= 0 || t.runningTimeout <= 0 || t.scanInterval <= 0 || t.instantScanInterval <= 0 {
return fmt.Errorf("%w: groupedTimeOptions #1", ErrOption)
}
if t.runningTimeout > t.storageTimeout {
return fmt.Errorf("%w: groupedTimeOptions #2", ErrOption)
}
if t.initializedTimeout > t.storageTimeout {
return fmt.Errorf("%w: groupedTimeOptions #3", ErrOption)
}
if t.scanInterval > t.storageTimeout || t.scanInterval > t.initializedTimeout || t.scanInterval > t.runningTimeout {
return fmt.Errorf("%w: groupedTimeOptions #4", ErrOption)
}
if t.instantScanInterval > t.scanInterval {
return fmt.Errorf("%w: groupedTimeOptions #5", ErrOption)
}
return nil
}
// Option is a interface.
type Option interface {
apply(opts *options)
verify(opts *options) error
}
type option struct {
applyFunc func(opts *options)
verifyFunc func(opts *options) error
}
func (o option) apply(opts *options) {
o.applyFunc(opts)
}
func (o option) verify(opts *options) error {
if o.verifyFunc == nil {
return nil
}
return o.verifyFunc(opts)
}
type optionGroup []Option
func (g optionGroup) apply(opts *options) {
for _, opt := range g {
opt.apply(opts)
}
}
func (g optionGroup) verify(opts *options) error {
for _, opt := range g {
if err := opt.verify(opts); err != nil {
return err
}
}
return nil
}
// WithContext set the context option.
func WithContext(ctx context.Context) Option {
return &option{
applyFunc: func(opts *options) { opts.context, opts.cancelFunc = context.WithCancel(ctx) },
}
}
// WithLoggerFactory set the loggerFactory option.
func WithLoggerFactory(f func(ctx context.Context) Logger) Option {
return &option{
applyFunc: func(opts *options) { opts.loggerFactory = f },
verifyFunc: func(opts *options) error {
if opts.loggerFactory == nil {
return fmt.Errorf("%w: loggerFactory", ErrOption)
}
return nil
},
}
}
// WithStorageTimeout set the storageTimeout option.
func WithStorageTimeout(d time.Duration) Option {
return &option{
applyFunc: func(opts *options) { opts.storageTimeout = d },
verifyFunc: func(opts *options) error {
return opts.groupedTimeOptions.verify()
},
}
}
// WithInitializedTimeout set the initializedTimeout option.
func WithInitializedTimeout(d time.Duration) Option {
return &option{
applyFunc: func(opts *options) { opts.initializedTimeout = d },
verifyFunc: func(opts *options) error {
return opts.groupedTimeOptions.verify()
},
}
}
// WithRunningTimeout set the runningTimeout option.
func WithRunningTimeout(d time.Duration) Option {
return &option{
applyFunc: func(opts *options) { opts.runningTimeout = d },
verifyFunc: func(opts *options) error {
return opts.groupedTimeOptions.verify()
},
}
}
// WithScanInterval set the scanInterval option.
func WithScanInterval(d time.Duration) Option {
return &option{
applyFunc: func(opts *options) { opts.scanInterval = d },
verifyFunc: func(opts *options) error {
return opts.groupedTimeOptions.verify()
},
}
}
// WithInstantScanInterval set the instantScanInterval option.
func WithInstantScanInterval(d time.Duration) Option {
return &option{
applyFunc: func(opts *options) { opts.instantScanInterval = d },
verifyFunc: func(opts *options) error {
return opts.groupedTimeOptions.verify()
},
}
}
// WithWaitTimeout set the waitTimeout option.
func WithWaitTimeout(d time.Duration) Option {
return &option{
applyFunc: func(opts *options) { opts.waitTimeout = d },
verifyFunc: func(opts *options) error {
if opts.waitTimeout <= 0 {
return fmt.Errorf("%w: waitTimeout", ErrOption)
}
return nil
},
}
}
// WithCtxMarshaler set the ctxMarshaler option.
func WithCtxMarshaler(m CtxMarshaler) Option {
return &option{
applyFunc: func(opts *options) { opts.ctxMarshaler = m },
verifyFunc: func(opts *options) error {
if opts.ctxMarshaler == nil {
return fmt.Errorf("%w: ctxMarshaler", ErrOption)
}
return nil
},
}
}
// WithCheckCallback set the checkCallback option.
func WithCheckCallback(f func(logger Logger, abnormalTasks []Task)) Option {
return &option{
applyFunc: func(opts *options) { opts.checkCallback = f },
verifyFunc: func(opts *options) error {
if opts.checkCallback == nil {
return fmt.Errorf("%w: checkCallback", ErrOption)
}
return nil
},
}
}
// WithDryRun set the dryRun option.
func WithDryRun(flag bool) Option {
return &option{
applyFunc: func(opts *options) { opts.dryRun = flag },
}
}
// WithPoolSize set the poolSize option.
func WithPoolSize(size int) Option {
return &option{
applyFunc: func(opts *options) { opts.poolSize = size },
verifyFunc: func(opts *options) error {
if opts.poolSize <= 0 {
return fmt.Errorf("%w: poolSize", ErrOption)
}
return nil
},
}
}
func withDB(db *gorm.DB) Option {
return &option{
applyFunc: func(opts *options) { opts.db = db },
verifyFunc: func(opts *options) error {
if opts.db == nil {
return fmt.Errorf("%w: db", ErrOption)
}
return nil
},
}
}
func withTable(table string) Option {
return &option{
applyFunc: func(opts *options) { opts.table = table },
verifyFunc: func(opts *options) error {
if opts.table == "" {
return fmt.Errorf("%w: table", ErrOption)
}
return nil
},
}
}
func withTaskRegister(tr taskRegister) Option {
return &option{
applyFunc: func(opts *options) { opts.taskRegister = tr },
verifyFunc: func(opts *options) error {
if opts.taskRegister == nil {
return fmt.Errorf("%w: taskRegister", ErrOption)
}
return nil
},
}
}
// newDefaultOptions generate default options
func newDefaultOptions() *options {
ctx, cancelFunc := context.WithCancel(defaultContextFactory())
return &options{
db: nil,
table: "",
context: ctx,
loggerFactory: defaultLoggerFactory,
groupedTimeOptions: groupedTimeOptions{
storageTimeout: defaultStorageTimeout,
initializedTimeout: defaultInitializedTimeout,
runningTimeout: defaultRunningTimeout,
scanInterval: defaultScanInterval,
instantScanInterval: defaultInstantScanInterval,
},
waitTimeout: defaultWaitTimeout,
ctxMarshaler: &defaultCtxMarshaler{},
checkCallback: defaultCheckCallback,
dryRun: false,
poolSize: defaultPoolSize,
taskRegister: &taskRegisterImp{},
cancelFunc: cancelFunc,
}
}
var (
defaultStorageTimeout = time.Hour * 7 * 24
defaultWaitTimeout = time.Second * 0
defaultScanInterval = time.Second * 5
defaultInstantScanInterval = time.Millisecond * 100
defaultRunningTimeout = time.Minute * 30
defaultInitializedTimeout = time.Minute * 5
defaultPoolSize = ants.DefaultAntsPoolSize
defaultRetryInterval = time.Second
)
type defaultCtxMarshaler struct{}
func (s *defaultCtxMarshaler) MarshalCtx(ctx context.Context) ([]byte, error) {
return nil, nil
}
func (s *defaultCtxMarshaler) UnmarshalCtx(bytes []byte) (context.Context, error) {
return context.Background(), nil
}
func defaultContextFactory() context.Context {
return context.Background()
}
func defaultLoggerFactory(ctx context.Context) Logger {
return logrus.NewEntry(logrus.New())
}
func defaultCheckCallback(logger Logger, abnormalTasks []Task) {
if len(abnormalTasks) == 0 {
return
}
logger.Errorf("[defaultCheckCallback] abnormal tasks found, total[%v]", len(abnormalTasks))
for _, at := range abnormalTasks {
logger.Warnf("[defaultCheckCallback] abnormal task found, id[%v], task_key[%v], task_status[%v]", at.ID, at.TaskKey, at.TaskStatus)
}
}