Skip to content

Commit b0ba2f2

Browse files
committed
libvuln: rework constuctor
This change exposes a method to check if the backing store is initialized, and does not block the constructor on running updaters initially. To facilitate a caller having the previous behavior, a method is added to allow a caller to run updaters on demand. Signed-off-by: Hank Donnay <[email protected]>
1 parent 1f4717f commit b0ba2f2

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

libvuln/libvuln.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type Libvuln struct {
2828
pool *pgxpool.Pool
2929
matchers []driver.Matcher
3030
updateRetention int
31+
updaters *updates.Manager
3132
}
3233

3334
// New creates a new instance of the Libvuln library
@@ -59,7 +60,7 @@ func New(ctx context.Context, opts *Opts) (*Libvuln, error) {
5960
}
6061

6162
// create update manager
62-
updateMgr, err := updates.NewManager(ctx,
63+
l.updaters, err = updates.NewManager(ctx,
6364
l.store,
6465
pool,
6566
opts.Client,
@@ -70,20 +71,23 @@ func New(ctx context.Context, opts *Opts) (*Libvuln, error) {
7071
updates.WithOutOfTree(opts.Updaters),
7172
updates.WithGC(opts.UpdateRetention),
7273
)
73-
74-
// perform initial update
75-
if err := updateMgr.Run(ctx); err != nil {
76-
zlog.Error(ctx).Err(err).Msg("encountered error while updating")
74+
if err != nil {
75+
return nil, err
7776
}
7877

7978
// launch background updater
8079
if !opts.DisableBackgroundUpdates {
81-
go updateMgr.Start(ctx)
80+
go l.updaters.Start(ctx)
8281
}
8382
zlog.Info(ctx).Msg("libvuln initialized")
8483
return l, nil
8584
}
8685

86+
// FetchUpdates runs configured updaters.
87+
func (l *Libvuln) FetchUpdates(ctx context.Context) error {
88+
return l.updaters.Run(ctx)
89+
}
90+
8791
// Scan creates a VulnerabilityReport given a manifest's IndexReport.
8892
func (l *Libvuln) Scan(ctx context.Context, ir *claircore.IndexReport) (*claircore.VulnerabilityReport, error) {
8993
return matcher.Match(ctx, ir, l.matchers, l.store)
@@ -162,3 +166,8 @@ func (l *Libvuln) GCFull(ctx context.Context) (int64, error) {
162166

163167
return i, err
164168
}
169+
170+
// Initialized reports whether the backing vulnerability store is initialized.
171+
func (l *Libvuln) Initialized(ctx context.Context) (bool, error) {
172+
return l.store.Initialized(ctx)
173+
}

0 commit comments

Comments
 (0)