Skip to content
This repository was archived by the owner on Dec 15, 2020. It is now read-only.

Commit 57f65e7

Browse files
austburnzwass
authored andcommitted
Allow host expiry window to be altered (#2121)
Fixes a bug in which the event expiry window would not be properly updated. Fixes #2122
1 parent 9782ffa commit 57f65e7

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

server/datastore/mysql/app_configs.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,29 @@ func (d *Datastore) isEventSchedulerEnabled() (bool, error) {
4242
}
4343

4444
func (d *Datastore) ManageHostExpiryEvent(hostExpiryEnabled bool, hostExpiryWindow int) error {
45-
if !hostExpiryEnabled {
46-
_, err := d.db.Exec("DROP EVENT IF EXISTS host_expiry")
47-
return err
45+
var err error
46+
hostExpiryConfig := struct {
47+
Window int `db:"host_expiry_window"`
48+
}{}
49+
if err = d.db.Get(&hostExpiryConfig, "SELECT host_expiry_window from app_configs LIMIT 1"); err != nil {
50+
return errors.Wrap(err, "get expiry window setting")
4851
}
4952

50-
_, err := d.db.Exec(fmt.Sprintf("CREATE EVENT IF NOT EXISTS host_expiry ON SCHEDULE EVERY 1 HOUR ON COMPLETION PRESERVE DO DELETE FROM hosts WHERE seen_time < DATE_SUB(NOW(), INTERVAL %d DAY)", hostExpiryWindow))
51-
return err
53+
shouldUpdateWindow := hostExpiryEnabled && hostExpiryConfig.Window != hostExpiryWindow
54+
55+
if !hostExpiryEnabled || shouldUpdateWindow {
56+
if _, err := d.db.Exec("DROP EVENT IF EXISTS host_expiry"); err != nil {
57+
return errors.Wrap(err, "drop existing host_expiry event")
58+
}
59+
}
60+
61+
if shouldUpdateWindow {
62+
sql := fmt.Sprintf("CREATE EVENT IF NOT EXISTS host_expiry ON SCHEDULE EVERY 1 HOUR ON COMPLETION PRESERVE DO DELETE FROM hosts WHERE seen_time < DATE_SUB(NOW(), INTERVAL %d DAY)", hostExpiryWindow)
63+
if _, err := d.db.Exec(sql); err != nil {
64+
return errors.Wrap(err, "create new host_expiry event")
65+
}
66+
}
67+
return nil
5268
}
5369

5470
func (d *Datastore) SaveAppConfig(info *kolide.AppConfig) error {

0 commit comments

Comments
 (0)