Skip to content

Commit

Permalink
Errgroup
Browse files Browse the repository at this point in the history
  • Loading branch information
majst01 committed Aug 25, 2023
1 parent 17e9489 commit 2171467
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
26 changes: 20 additions & 6 deletions cmd/internal/database/meilisearch/meilisearch.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package meilisearch

import (
"context"
"errors"
"fmt"
"io/fs"
Expand All @@ -15,6 +16,7 @@ import (
"github.com/Masterminds/semver/v3"
"github.com/avast/retry-go/v4"
"github.com/meilisearch/meilisearch-go"
"golang.org/x/sync/errgroup"

"github.com/metal-stack/backup-restore-sidecar/cmd/internal/constants"
"github.com/metal-stack/backup-restore-sidecar/cmd/internal/utils"
Expand Down Expand Up @@ -169,8 +171,12 @@ func (db *Meilisearch) Upgrade() error {
return fmt.Errorf("unable to rename dbdir: %w", err)
}

var cmd *exec.Cmd
go func() {
var (
cmd *exec.Cmd
g, _ = errgroup.WithContext(context.Background())
)

g.Go(func() error {
args := []string{"--import-dump", path.Join(db.dumpdir, latestStableDump), "--master-key", db.apikey}
db.log.Infow("execute meilisearch", "args", args)

Expand All @@ -179,21 +185,29 @@ func (db *Meilisearch) Upgrade() error {
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
db.log.Errorw("unable import latest dump, skipping upgrade", "error", err)
return fmt.Errorf("unable import latest dump, skipping upgrade %w", err)
}
}()
db.log.Info("import of dump finished")
return nil
})

// TODO big databases might take longer, not sure if 100 attempts are enough
// must check how long it take max with backoff ?
err = retry.Do(func() error {
v, err := db.client.Version()
if err != nil {
return err
}
db.log.Infow("meilisearch started after upgrade, killing it", "version", v)
return cmd.Process.Signal(syscall.SIGTERM)
})
}, retry.Attempts(100))
if err != nil {
return err
}
err = g.Wait()
if err != nil {
return fmt.Errorf("database upgrade failed with %w", err)
}

db.log.Infow("upgrade done and new data in place", "took", time.Since(start))
return nil
Expand All @@ -216,7 +230,6 @@ func (db *Meilisearch) moveDumpsToBackupDir() error {

dst := path.Join(constants.BackupDir, d.Name())
src := basepath
db.log.Infow("move dump", "from", src, "to", dst)

latestStableDst := path.Join(db.dumpdir, latestStableDump)
db.log.Infow("create latest dump", "from", src, "to", latestStableDst)
Expand All @@ -225,6 +238,7 @@ func (db *Meilisearch) moveDumpsToBackupDir() error {
return fmt.Errorf("unable create latest stable dump: %w", err)
}

db.log.Infow("move dump", "from", src, "to", dst)
copy := exec.Command("mv", "-v", src, dst)
copy.Stdout = os.Stdout
copy.Stderr = os.Stderr
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.16.0
go.uber.org/zap v1.25.0
golang.org/x/sync v0.3.0
google.golang.org/api v0.138.0
google.golang.org/grpc v1.57.0
google.golang.org/protobuf v1.31.0
Expand Down Expand Up @@ -73,7 +74,6 @@ require (
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/oauth2 v0.11.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
Expand Down

0 comments on commit 2171467

Please sign in to comment.