Skip to content

Commit eba48af

Browse files
authored
feat: add documentation URL for database lock errors (#9531)
1 parent 92ebc7e commit eba48af

File tree

5 files changed

+37
-20
lines changed

5 files changed

+37
-20
lines changed

cmd/trivy/main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,5 @@ func run() error {
4747
// Set up signal handling for graceful shutdown
4848
ctx := commands.NotifyContext(context.Background())
4949

50-
app := commands.NewApp()
51-
return app.ExecuteContext(ctx)
50+
return commands.Run(ctx)
5251
}

pkg/cache/fs.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/hashicorp/go-multierror"
1111
bolt "go.etcd.io/bbolt"
12+
bberrors "go.etcd.io/bbolt/errors"
1213
"golang.org/x/xerrors"
1314

1415
"github.com/aquasecurity/trivy/pkg/fanal/types"
@@ -34,7 +35,7 @@ func NewFSCache(cacheDir string) (FSCache, error) {
3435
})
3536
if err != nil {
3637
// Check if the error is due to timeout (database locked by another process)
37-
if errors.Is(err, bolt.ErrTimeout) {
38+
if errors.Is(err, bberrors.ErrTimeout) {
3839
return FSCache{}, xerrors.Errorf("cache may be in use by another process: %w", err)
3940
}
4041
return FSCache{}, xerrors.Errorf("unable to open cache DB: %w", err)

pkg/commands/artifact/run.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -367,13 +367,6 @@ func Run(ctx context.Context, opts flag.Options, targetKind TargetKind) (err err
367367
ctx, cancel := context.WithTimeout(ctx, opts.Timeout)
368368
defer cancel()
369369

370-
defer func() {
371-
if errors.Is(err, context.DeadlineExceeded) {
372-
// e.g. https://trivy.dev/latest/docs/configuration/
373-
log.WarnContext(ctx, fmt.Sprintf("Provide a higher timeout value, see %s", doc.URL("/docs/configuration/", "")))
374-
}
375-
}()
376-
377370
if opts.GenerateDefaultConfig {
378371
log.Info("Writing the default config to trivy-default.yaml...")
379372

pkg/commands/run.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package commands
2+
3+
import (
4+
"context"
5+
"errors"
6+
"fmt"
7+
8+
bberrors "go.etcd.io/bbolt/errors"
9+
10+
"github.com/aquasecurity/trivy/pkg/log"
11+
"github.com/aquasecurity/trivy/pkg/version/doc"
12+
)
13+
14+
const (
15+
troubleshootingDocPath = "/docs/references/troubleshooting/"
16+
lockDocFragment = "database-and-cache-lock-errors"
17+
timeoutDocFragment = "timeout"
18+
)
19+
20+
// Run builds the CLI application and executes it with centralized error handling.
21+
func Run(ctx context.Context) error {
22+
app := NewApp()
23+
if err := app.ExecuteContext(ctx); err != nil {
24+
if errors.Is(err, context.DeadlineExceeded) {
25+
log.WarnContext(ctx, fmt.Sprintf("Provide a higher timeout value, see %s", doc.URL(troubleshootingDocPath, timeoutDocFragment)))
26+
}
27+
if errors.Is(err, bberrors.ErrTimeout) {
28+
log.ErrorContext(ctx, fmt.Sprintf("Failed to acquire cache or database lock, see %s for troubleshooting", doc.URL(troubleshootingDocPath, lockDocFragment)))
29+
}
30+
return err
31+
}
32+
return nil
33+
}

pkg/k8s/commands/run.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package commands
33
import (
44
"context"
55
"errors"
6-
"fmt"
76

87
"github.com/spf13/viper"
98
"golang.org/x/xerrors"
@@ -19,7 +18,6 @@ import (
1918
"github.com/aquasecurity/trivy/pkg/k8s/scanner"
2019
"github.com/aquasecurity/trivy/pkg/log"
2120
"github.com/aquasecurity/trivy/pkg/types"
22-
"github.com/aquasecurity/trivy/pkg/version/doc"
2321
)
2422

2523
// Run runs a k8s scan
@@ -37,14 +35,7 @@ func Run(ctx context.Context, args []string, opts flag.Options) error {
3735
return xerrors.Errorf("failed getting k8s cluster: %w", err)
3836
}
3937
ctx, cancel := context.WithTimeout(ctx, opts.Timeout)
40-
41-
defer func() {
42-
cancel()
43-
if errors.Is(err, context.DeadlineExceeded) {
44-
// e.g. https://trivy.dev/latest/docs/configuration
45-
log.WarnContext(ctx, fmt.Sprintf("Provide a higher timeout value, see %s", doc.URL("/docs/configuration/", "")))
46-
}
47-
}()
38+
defer cancel()
4839
opts.K8sVersion = cluster.GetClusterVersion()
4940
return clusterRun(ctx, opts, cluster)
5041
}

0 commit comments

Comments
 (0)