Skip to content

Commit

Permalink
Cleanup (#8)
Browse files Browse the repository at this point in the history
* Cleanup
* go mod tidy

---------

Co-authored-by: Lorenz Kästle <[email protected]>
  • Loading branch information
RincewindsHat and Lorenz Kästle authored Aug 1, 2024
1 parent 2a73627 commit a5ca81e
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 57 deletions.
50 changes: 32 additions & 18 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
run:
timeout: 5m
skip-files:
- '(.+)_test\.go'

tests: false
linters:
disable-all: false
disable:
- gochecknoglobals
- nlreturn
- gomnd
- nakedret
- nolintlint
- godot
- varnamelen
- nonamedreturns
presets:
- bugs
- unused
- style
fast: false
enable-all: true
disable:
- cyclop
- depguard
- exhaustivestruct
- exhaustruct
- forbidigo
- forcetypeassert
- gci
- gochecknoglobals
- gochecknoinits
- godox
- godot
- goerr113
- gofumpt
- gomnd
- lll
- musttag
- nakedret
- nlreturn
- nolintlint
- nonamedreturns
- tagliatelle
- varnamelen
- wrapcheck
- funlen
linters-settings:
estif:
min-complexity: 4
maligned:
suggest-new: true
6 changes: 1 addition & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@ module github.com/NETWAYS/ido-cleanup
go 1.19

require (
github.com/go-sql-driver/mysql v1.8.1
github.com/sirupsen/logrus v1.9.3
github.com/spf13/pflag v1.0.5
)

require (
filippo.io/edwards25519 v1.1.0 // indirect
golang.org/x/sys v0.22.0 // indirect
)
require golang.org/x/sys v0.22.0 // indirect
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
Expand Down
59 changes: 29 additions & 30 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ package main
import (
"database/sql"
"fmt"
log "github.com/sirupsen/logrus"
flag "github.com/spf13/pflag"
"os"
"os/signal"
"strings"
"syscall"
"time"

_ "github.com/go-sql-driver/mysql"
"github.com/sirupsen/logrus"
"github.com/spf13/pflag"
)

const readme = `
Expand Down Expand Up @@ -47,20 +46,20 @@ func main() {
handleArguments()

if debug {
log.SetLevel(log.DebugLevel)
logrus.SetLevel(logrus.DebugLevel)
}

log.Info("starting ido-cleanup")
logrus.Info("starting ido-cleanup")

// Setup database connection
db, err := sql.Open("mysql", dbDsn)
if err != nil {
log.Fatal(err)
logrus.Fatal(err)
}

err = db.Ping()
if err != nil {
log.Fatal("could not connect to database: ", err)
logrus.Fatal("could not connect to database: ", err)
}

db.SetConnMaxLifetime(time.Minute * 15)
Expand All @@ -70,7 +69,7 @@ func main() {
if err != nil {
_ = db.Close()

log.Fatal(err)
logrus.Fatal(err)
}

defer db.Close()
Expand All @@ -86,14 +85,14 @@ func main() {
currentInterval := interval

if runCleanup(db, instanceID) {
log.WithField("interval", fastInterval).Debug("updating interval")
logrus.WithField("interval", fastInterval).Debug("updating interval")

currentInterval = fastInterval
}

// Stop here when only once is requested
if once {
log.Info("stopping after one cleanup")
logrus.Info("stopping after one cleanup")
return
}

Expand All @@ -102,7 +101,7 @@ func main() {
go func() {
sig := <-interrupt

log.Info("received signal ", sig)
logrus.Info("received signal ", sig)
timer.Stop()

done <- true
Expand All @@ -120,37 +119,37 @@ func main() {
}

if currentInterval != nextInterval {
log.WithField("interval", nextInterval).Debug("updating interval")
logrus.WithField("interval", nextInterval).Debug("updating interval")

timer.Reset(nextInterval)
}
}
}

log.Info("stopping ido-cleanup")
logrus.Info("stopping ido-cleanup")
}

func handleArguments() {
if v := os.Getenv("DB_DSN"); v != "" {
dbDsn = v
}

flag.StringVar(&dbDsn, "db", dbDsn, "DB Connecting string (env:DB_DSN)")
flag.StringVar(&instance, "instance", instance, "IDO instance name")
flag.IntVar(&limit, "limit", limit, "Limit deleting rows in one query")
flag.DurationVar(&interval, "interval", interval, "Cleanup every X seconds")
flag.DurationVar(&fastInterval, "fast-interval", fastInterval,
pflag.StringVar(&dbDsn, "db", dbDsn, "DB Connecting string (env:DB_DSN)")
pflag.StringVar(&instance, "instance", instance, "IDO instance name")
pflag.IntVar(&limit, "limit", limit, "Limit deleting rows in one query")
pflag.DurationVar(&interval, "interval", interval, "Cleanup every X seconds")
pflag.DurationVar(&fastInterval, "fast-interval", fastInterval,
"Cleanup every X seconds - when more then 2x limit rows to delete")
flag.BoolVar(&once, "once", false, "Just run once")
flag.BoolVar(&noop, "noop", false, "Just check - don't purge")
flag.BoolVar(&debug, "debug", false, "Enable debug logging")
flag.BoolVarP(&printVersion, "version", "V", false, "Print version and exit")
pflag.BoolVar(&once, "once", false, "Just run once")
pflag.BoolVar(&noop, "noop", false, "Just check - don't purge")
pflag.BoolVar(&debug, "debug", false, "Enable debug logging")
pflag.BoolVarP(&printVersion, "version", "V", false, "Print version and exit")

flag.Usage = func() {
pflag.Usage = func() {
_, _ = fmt.Fprintf(os.Stdout, "%s\n\n", strings.Trim(readme, "\r\n"))
_, _ = fmt.Fprintf(os.Stdout, "Usage of %s:\n", os.Args[0])

flag.PrintDefaults()
pflag.PrintDefaults()
}

for _, table := range knownTables {
Expand All @@ -161,11 +160,11 @@ func handleArguments() {

ages[table.Name] = &age

flag.UintVar(ages[table.Name], table.Name, age, "How long to keep entries of "+table.Name+" in days")
pflag.UintVar(ages[table.Name], table.Name, age, "How long to keep entries of "+table.Name+" in days")
}

flag.CommandLine.SortFlags = false
flag.Parse()
pflag.CommandLine.SortFlags = false
pflag.Parse()

if printVersion {
_, _ = fmt.Fprintf(os.Stdout, "NETWAYS ido-cleanup version %s\n", buildVersion())
Expand All @@ -181,7 +180,7 @@ func runCleanup(db *sql.DB, instanceID int) (busy bool) {
}

start := time.Now()
entry := log.WithField("table", table.Name)
entry := logrus.WithField("table", table.Name)

// Look for the time stamp of the oldest entry and log it
oldest, err := table.OldestTime(db, instanceID)
Expand All @@ -203,7 +202,7 @@ func runCleanup(db *sql.DB, instanceID int) (busy bool) {
continue
}

entry.WithFields(log.Fields{
entry.WithFields(logrus.Fields{
"rows": rows,
"took": time.Since(start),
}).Info("would delete rows")
Expand All @@ -224,7 +223,7 @@ func runCleanup(db *sql.DB, instanceID int) (busy bool) {
busy = true
}

entry = entry.WithFields(log.Fields{
entry = entry.WithFields(logrus.Fields{
"rows": rows,
"took": time.Since(start),
})
Expand Down

0 comments on commit a5ca81e

Please sign in to comment.