-
Notifications
You must be signed in to change notification settings - Fork 0
/
duckron.go
167 lines (137 loc) · 3.69 KB
/
duckron.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
package duckron
import (
"fmt"
"sync"
"time"
"github.com/berkantay/duckron/drivers"
)
var (
// ErrSnapshotManagerFailed is returned when a snapshot manager fails to be created
ErrSnapshotManagerFailed = newError("snapshot", "failed to create snapshot manager")
// ErrRetentionManagerFailed is returned when a retention manager fails to be created
ErrRetentionManagerFailed = newError("retention", "failed to create retention manager")
)
type Duckron struct {
client *drivers.DuckDBClient
config *config
services *Services
alertChan chan int
errChan chan *Error
}
type Services struct {
snapshotManager *snapshotManager
retentionManager *retentionManager
alertManager *alertManager
}
func NewDuckron(config *config) (*Duckron, error) {
errChan := make(chan *Error)
alertChan := make(chan int)
client, err := drivers.NewDuckDBClient(config.Database.Path)
if err != nil {
return nil, err
}
services := &Services{}
duckron := &Duckron{client: client, config: config, services: services, alertChan: alertChan, errChan: errChan}
if duckron.isSnapshotConfigured() {
interval, err := time.ParseDuration(config.Database.Snapshot.IntervalHours)
if err != nil {
return nil, err
}
snapshotOptions := &snapshotOptions{
interval: interval,
format: config.Database.Snapshot.Format,
destination: config.Database.Snapshot.Destination,
}
snapshotManager, cerr := NewSnapshotManager(client, snapshotOptions)
if err != nil {
return nil, *cerr.unwrap()
}
services.snapshotManager = snapshotManager
}
if duckron.isRetentionConfigured() {
interval, err := time.ParseDuration(config.Database.Retention.IntervalHours)
retentionOptions := &retentionOptions{
interval: interval,
path: config.Database.Snapshot.Destination,
}
retentionManager, cerr := NewRetentionManager(client, retentionOptions)
if err != nil {
return nil, *cerr.unwrap()
}
services.retentionManager = retentionManager
}
if duckron.isAlertConfigured() {
alertOptions := &alertOptions{
ramThreshold: config.Alerts.Threshold.Ram,
cpuThreshold: config.Alerts.Threshold.Cpu,
diskThreshold: config.Alerts.Threshold.Disk,
}
alertManager := NewAlertManager(alertOptions, alertChan, errChan)
services.alertManager = alertManager
}
return duckron, nil
}
func (d *Duckron) Start() *Error {
var wg sync.WaitGroup
go func() {
for err := range d.errChan {
if err != nil {
fmt.Println("Error:", err)
}
}
}()
go func() {
for alert := range d.alertChan {
switch alert {
case CPU_THRESHOLD_EXCEEDED:
fmt.Println("CPU threshold exceeded")
case RAM_THRESHOLD_EXCEEDED:
fmt.Println("RAM threshold exceeded")
case DISK_THRESHOLD_EXCEEDED:
fmt.Println("Disk threshold exceeded")
}
}
}()
if d.services != nil {
if d.services.snapshotManager != nil {
wg.Add(1)
go func() {
defer wg.Done()
if err := d.services.snapshotManager.take(d.errChan); err != nil {
d.errChan <- err
}
}()
}
if d.services.retentionManager != nil {
wg.Add(1)
go func() {
defer wg.Done()
if err := d.services.retentionManager.clean(d.errChan); err != nil {
d.errChan <- err
}
}()
}
if d.services.alertManager != nil {
wg.Add(1)
go func() {
defer wg.Done()
d.services.alertManager.monitor()
}()
}
}
wg.Wait()
close(d.errChan)
if len(d.errChan) > 0 {
return <-d.errChan
}
return nil
}
func (d *Duckron) isSnapshotConfigured() bool {
return d.config.Database.Snapshot != SnapshotConfig{}
}
func (d *Duckron) isRetentionConfigured() bool {
return d.config.Database.Retention != RetentionConfig{}
}
func (d *Duckron) isAlertConfigured() bool {
return d.config.Alerts != AlertsConfig{}
}