-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcleanup.go
More file actions
187 lines (152 loc) · 5.04 KB
/
cleanup.go
File metadata and controls
187 lines (152 loc) · 5.04 KB
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
package main
import (
"context"
"fmt"
"log"
"time"
)
// CleanupConfig holds configuration for the cleanup job
type CleanupConfig struct {
Enabled bool
CheckInterval time.Duration // How often to run cleanup
StuckAfterHours int // Consider "installing" as stuck after X hours
RetentionDays int // Delete records older than X days (0 = disabled)
RetentionEnabled bool // Enable automatic data retention/deletion
}
// Cleaner handles cleanup of stuck installations
type Cleaner struct {
cfg CleanupConfig
ch *CHClient
}
// NewCleaner creates a new cleaner instance
func NewCleaner(cfg CleanupConfig, ch *CHClient) *Cleaner {
return &Cleaner{
cfg: cfg,
ch: ch,
}
}
// Start begins the cleanup loop
func (c *Cleaner) Start() {
if !c.cfg.Enabled {
log.Println("INFO: cleanup job disabled")
return
}
go c.cleanupLoop()
log.Printf("INFO: cleanup job started (interval: %v, stuck after: %d hours)", c.cfg.CheckInterval, c.cfg.StuckAfterHours)
// Start retention job if enabled
if c.cfg.RetentionEnabled && c.cfg.RetentionDays > 0 {
go c.retentionLoop()
log.Printf("INFO: data retention job started (delete after: %d days)", c.cfg.RetentionDays)
}
}
func (c *Cleaner) cleanupLoop() {
// Run immediately on start
c.runCleanup()
ticker := time.NewTicker(c.cfg.CheckInterval)
defer ticker.Stop()
for range ticker.C {
c.runCleanup()
}
}
// runCleanup finds and updates stuck installations
func (c *Cleaner) runCleanup() {
ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
defer cancel()
stuckRecords, err := c.ch.FindStuckInstallations(ctx, c.cfg.StuckAfterHours)
if err != nil {
log.Printf("WARN: cleanup - failed to find stuck installations: %v", err)
return
}
if len(stuckRecords) == 0 {
log.Printf("INFO: cleanup - no stuck installations found")
return
}
log.Printf("INFO: cleanup - found %d stuck installations (older than %dh)", len(stuckRecords), c.cfg.StuckAfterHours)
updated := 0
for _, record := range stuckRecords {
if err := c.ch.MarkRecordAsUnknown(ctx, record, c.cfg.StuckAfterHours); err != nil {
log.Printf("WARN: cleanup - failed to update record %s (%s): %v", record.ID, record.NSAPP, err)
continue
}
updated++
}
log.Printf("INFO: cleanup - updated %d/%d stuck installations to 'unknown'", updated, len(stuckRecords))
}
// StuckRecord represents a minimal record for cleanup
type StuckRecord struct {
ID string `json:"id"`
NSAPP string `json:"nsapp"`
Created string `json:"created"`
}
// RunNow triggers an immediate cleanup run (for testing/manual trigger)
func (c *Cleaner) RunNow() (int, error) {
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
stuckRecords, err := c.ch.FindStuckInstallations(ctx, c.cfg.StuckAfterHours)
if err != nil {
return 0, fmt.Errorf("failed to find stuck installations: %w", err)
}
updated := 0
for _, record := range stuckRecords {
if err := c.ch.MarkRecordAsUnknown(ctx, record, c.cfg.StuckAfterHours); err != nil {
log.Printf("WARN: cleanup - failed to update record %s: %v", record.ID, err)
continue
}
updated++
}
return updated, nil
}
// GetStuckCount returns the current number of stuck installations
func (c *Cleaner) GetStuckCount(ctx context.Context) (int, error) {
return c.ch.GetStuckCount(ctx, c.cfg.StuckAfterHours)
}
// =============================================
// DATA RETENTION (GDPR Löschkonzept)
// =============================================
// retentionLoop runs the data retention job periodically (once per day)
func (c *Cleaner) retentionLoop() {
// Run once on startup after a delay
time.Sleep(5 * time.Minute)
c.runRetention()
// Run daily at 3:00 AM
ticker := time.NewTicker(24 * time.Hour)
defer ticker.Stop()
for range ticker.C {
c.runRetention()
}
}
// runRetention deletes records older than RetentionDays
func (c *Cleaner) runRetention() {
if c.cfg.RetentionDays <= 0 {
return
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
log.Printf("INFO: retention - starting cleanup of records older than %d days", c.cfg.RetentionDays)
if err := c.ch.DeleteOldRecords(ctx, c.cfg.RetentionDays); err != nil {
log.Printf("WARN: retention - failed to delete old records: %v", err)
return
}
log.Printf("INFO: retention - DELETE mutation submitted for records older than %d days", c.cfg.RetentionDays)
}
// GetRetentionStats returns statistics about records eligible for deletion
func (c *Cleaner) GetRetentionStats(ctx context.Context) (eligible int, oldestDate string, err error) {
if c.cfg.RetentionDays <= 0 {
return 0, "", nil
}
cutoff := time.Now().AddDate(0, 0, -c.cfg.RetentionDays)
var cnt uint64
if err := c.ch.db.QueryRowContext(ctx,
"SELECT count() FROM telemetry_db.telemetry WHERE created < ?", cutoff,
).Scan(&cnt); err != nil {
return 0, "", err
}
var oldest string
_ = c.ch.db.QueryRowContext(ctx,
"SELECT toString(min(created)) FROM telemetry_db.telemetry",
).Scan(&oldest)
if len(oldest) >= 10 {
oldest = oldest[:10]
}
return int(cnt), oldest, nil
}