From acee382610b41bf2c4112ad7abe16fb1b9cc0833 Mon Sep 17 00:00:00 2001 From: marco Date: Tue, 4 Jun 2024 16:38:40 +0200 Subject: [PATCH] cscli bouncers prune: query creation date if they never connected --- cmd/crowdsec-cli/bouncers.go | 2 +- pkg/database/bouncers.go | 13 +++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/cmd/crowdsec-cli/bouncers.go b/cmd/crowdsec-cli/bouncers.go index d01008386f9..2bb734396d3 100644 --- a/cmd/crowdsec-cli/bouncers.go +++ b/cmd/crowdsec-cli/bouncers.go @@ -264,7 +264,7 @@ func (cli *cliBouncers) prune(duration time.Duration, force bool) error { } } - bouncers, err := cli.db.QueryBouncersLastPulltimeLT(time.Now().UTC().Add(-duration)) + bouncers, err := cli.db.QueryBouncersInactiveSince(time.Now().UTC().Add(-duration)) if err != nil { return fmt.Errorf("unable to query bouncers: %w", err) } diff --git a/pkg/database/bouncers.go b/pkg/database/bouncers.go index 2cc6b9dcb47..03a3227301d 100644 --- a/pkg/database/bouncers.go +++ b/pkg/database/bouncers.go @@ -115,6 +115,15 @@ func (c *Client) UpdateBouncerTypeAndVersion(bType string, version string, id in return nil } -func (c *Client) QueryBouncersLastPulltimeLT(t time.Time) ([]*ent.Bouncer, error) { - return c.Ent.Bouncer.Query().Where(bouncer.LastPullLT(t)).All(c.CTX) +func (c *Client) QueryBouncersInactiveSince(t time.Time) ([]*ent.Bouncer, error) { + return c.Ent.Bouncer.Query().Where( + // poor man's coalesce + bouncer.Or( + bouncer.LastPullLT(t), + bouncer.And( + bouncer.LastPullIsNil(), + bouncer.CreatedAtLT(t), + ), + ), + ).All(c.CTX) }