-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
79 lines (63 loc) · 1.49 KB
/
config.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
package easyworker
import "fmt"
var (
// use to store last id of supervisor. id is auto_increment.
taskLastId int
)
/*
Config is shared between EasyTask & EasyStream struct.
It's store options input by user.
*/
type Config struct {
// fun stores function add by user.
fun any
// number of workers (goroutines)
worker int
// retry times, if function was failed, worker will try again.
retry int
// sleep time before rerun
retrySleep int
}
/*
Make a configuration holder for EasyTask or EasyStream.
fun: This is func you need to run task.
numWorkers: Number of goroutine you want to run task.
retryTimes: Number of retry if func is failed.
Example:
fn = func(n int, prefix string) string {
return log.Sprintf("%s_%d", prefix, n)
}
config,_ := NewConfig(fn, 3, 0, 0)
*/
func NewConfig(fun any, numWorkers int, retryTimes int, retrySleep int) (ret Config, err error) {
if err = verifyFunc(fun); err != nil {
return
}
if numWorkers < 1 {
err = fmt.Errorf("number of workers is incorrect, %d", numWorkers)
return
}
if retryTimes < 0 {
err = fmt.Errorf("retryTimes is incorrect, %d", numWorkers)
return
}
if retryTimes > 0 && retrySleep < 0 {
err = fmt.Errorf("retrySleep is incorrect, %d", numWorkers)
return
}
ret = Config{
fun: fun,
worker: numWorkers,
retry: retryTimes,
retrySleep: retrySleep,
}
return
}
/*
Turn on/off print log to logger.
enable = true, print log.
= false, no print log.
*/
func EnableLog(enable bool) {
printLog = enable
}