-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
111 lines (88 loc) · 2.99 KB
/
options.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
package kiroku
import (
"fmt"
"time"
"github.com/hatchify/errors"
)
const (
// ErrEmptyDirectory is returned when a directory is empty
ErrEmptyDirectory = errors.Error("invalid directory, cannot be empty")
// ErrEmptyName is returned when a name is empty
ErrEmptyName = errors.Error("invalid name, cannot be empty")
)
const (
// DefaultEndOfResultsDelay is the default value for EndOfResultsDelay
DefaultEndOfResultsDelay = time.Second * 10
// DefaultErrorDelay is the default value for ErrorDelay
DefaultErrorDelay = time.Second * 30
// DefaultBatchDuration is the default value for BatchDuration
DefaultBatchDuration = time.Second * 10
)
// MakeOptions will create new Options
func MakeOptions(dir, name string) (o Options) {
o.Dir = dir
o.Name = name
return
}
// Options represent Kiroku options
type Options struct {
Dir string `toml:"dir" json:"dir"`
Name string `toml:"name" json:"name"`
Namespace string `toml:"namespace" json:"namespace"`
OnLog func(message string)
OnError func(err error)
OnResume func()
AvoidExportOnClose bool `toml:"avoid_export_on_close" json:"avoidExportOnClose"`
AvoidProcessOnClose bool `toml:"avoid_merge_on_close" json:"avoidMergeOnClose"`
ConsumerFileLimit int64 `toml:"consumer_file_limit" json:"consumerFileLimit"`
// BatchDuration represents the amount of time to keep a transaction open for a
// Batch operation
BatchDuration time.Duration `toml:"batch_duration" json:"batchDuration"`
// EndOfResultsDelay represents the amount of time to wait before pulling "Next" after
// receiving empty results (Default is 10 seconds).
EndOfResultsDelay time.Duration `toml:"end_of_results_delay" json:"endOfResultsDelay"`
// ErrorDelay represents the amount of time to wait before pulling "Next" after
// receiving an error
ErrorDelay time.Duration `toml:"error_delay" json:"errorDelay"`
// RangeStart will determine the moment in time from which syncs will begin
RangeStart time.Time `toml:"range_start" json:"rangeStart"`
// RangeEnd will determine the moment in time from which syncs will end
// Note: This feature is slated to be implemented within the following
// release. As of now, this will act as a field placeholder
RangeEnd time.Time `toml:"range_end" json:"rangeEnd"`
}
// Validate ensures that the Options have all the required fields set
func (o *Options) Validate() (err error) {
var errs errors.ErrorList
if len(o.Dir) == 0 {
errs.Push(ErrEmptyDirectory)
}
if len(o.Name) == 0 {
errs.Push(ErrEmptyName)
}
o.fill()
return errs.Err()
}
func (o *Options) FullName() string {
if len(o.Namespace) == 0 {
return o.Name
}
return fmt.Sprintf("%s_%s", o.Namespace, o.Name)
}
func (o *Options) fill() {
if o.EndOfResultsDelay == 0 {
o.EndOfResultsDelay = DefaultEndOfResultsDelay
}
if o.ErrorDelay == 0 {
o.ErrorDelay = DefaultErrorDelay
}
if o.BatchDuration == 0 {
o.BatchDuration = DefaultBatchDuration
}
if o.OnLog == nil {
o.OnLog = func(string) {}
}
if o.OnError == nil {
o.OnError = func(error) {}
}
}