Skip to content

Commit

Permalink
fix ui race for package count (#2839)
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
  • Loading branch information
wagoodman authored May 6, 2024
1 parent 00ff3ff commit a56eff9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 31 deletions.
22 changes: 17 additions & 5 deletions internal/sbomsync/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,22 @@ type Accessor interface {
}

type sbomBuilder struct {
sbom *sbom.SBOM
lock *sync.RWMutex
sbom *sbom.SBOM
lock *sync.RWMutex
onWrite []func(*sbom.SBOM)
}

func NewBuilder(s *sbom.SBOM) Builder {
func NewBuilder(s *sbom.SBOM, onWrite ...func(*sbom.SBOM)) Builder {
return &sbomBuilder{
sbom: s,
lock: &sync.RWMutex{},
sbom: s,
lock: &sync.RWMutex{},
onWrite: onWrite,
}
}

func (b sbomBuilder) onWriteEvent() {
for _, fn := range b.onWrite {
fn(b.sbom)
}
}

Expand All @@ -52,6 +60,7 @@ func (b sbomBuilder) WriteToSBOM(fn func(*sbom.SBOM)) {
defer b.lock.Unlock()

fn(b.sbom)
b.onWriteEvent()
}

func (b sbomBuilder) ReadFromSBOM(fn func(*sbom.SBOM)) {
Expand All @@ -66,18 +75,21 @@ func (b sbomBuilder) AddPackages(p ...pkg.Package) {
defer b.lock.Unlock()

b.sbom.Artifacts.Packages.Add(p...)
b.onWriteEvent()
}

func (b sbomBuilder) AddRelationships(relationship ...artifact.Relationship) {
b.lock.Lock()
defer b.lock.Unlock()

b.sbom.Relationships = append(b.sbom.Relationships, relationship...)
b.onWriteEvent()
}

func (b sbomBuilder) SetLinuxDistribution(release linux.Release) {
b.lock.Lock()
defer b.lock.Unlock()

b.sbom.Artifacts.LinuxDistribution = &release
b.onWriteEvent()
}
35 changes: 11 additions & 24 deletions syft/create_sbom.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import (
"context"
"fmt"
"sort"
"time"

"github.com/dustin/go-humanize"
"github.com/scylladb/go-set/strset"
"github.com/wagoodman/go-progress"

"github.com/anchore/syft/internal/bus"
"github.com/anchore/syft/internal/sbomsync"
Expand Down Expand Up @@ -63,9 +61,9 @@ func CreateSBOM(ctx context.Context, src source.Source, cfg *CreateSBOMConfig) (
}

catalogingProgress := monitorCatalogingTask(src.ID(), taskGroups)
packageCatalogingProgress := monitorPackageCatalogingTask(s.Artifacts.Packages)
packageCatalogingProgress := monitorPackageCatalogingTask()

builder := sbomsync.NewBuilder(&s)
builder := sbomsync.NewBuilder(&s, monitorPackageCount(packageCatalogingProgress))
for i := range taskGroups {
err := task.NewTaskExecutor(taskGroups[i], cfg.Parallelism).Execute(ctx, resolver, builder, catalogingProgress)
if err != nil {
Expand All @@ -80,7 +78,14 @@ func CreateSBOM(ctx context.Context, src source.Source, cfg *CreateSBOMConfig) (
return &s, nil
}

func monitorPackageCatalogingTask(pkgs *pkg.Collection) *monitor.CatalogerTaskProgress {
func monitorPackageCount(prog *monitor.CatalogerTaskProgress) func(s *sbom.SBOM) {
return func(s *sbom.SBOM) {
count := humanize.Comma(int64(s.Artifacts.Packages.PackageCount()))
prog.AtomicStage.Set(fmt.Sprintf("%s packages", count))
}
}

func monitorPackageCatalogingTask() *monitor.CatalogerTaskProgress {
info := monitor.GenericTask{
Title: monitor.Title{
Default: "Packages",
Expand All @@ -90,25 +95,7 @@ func monitorPackageCatalogingTask(pkgs *pkg.Collection) *monitor.CatalogerTaskPr
ParentID: monitor.TopLevelCatalogingTaskID,
}

prog := bus.StartCatalogerTask(info, -1, "")

go func() {
ticker := time.NewTicker(200 * time.Millisecond)
defer ticker.Stop()

for {
<-ticker.C

count := humanize.Comma(int64(pkgs.PackageCount()))
prog.AtomicStage.Set(fmt.Sprintf("%s packages", count))

if progress.IsCompleted(prog) {
break
}
}
}()

return prog
return bus.StartCatalogerTask(info, -1, "")
}

func monitorCatalogingTask(srcID artifact.ID, tasks [][]task.Task) *monitor.CatalogerTaskProgress {
Expand Down
4 changes: 2 additions & 2 deletions syft/format/spdxjson/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package spdxjson
import (
"bytes"
"flag"
"github.com/anchore/syft/syft/artifact"
"github.com/anchore/syft/syft/file"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/anchore/syft/syft/artifact"
"github.com/anchore/syft/syft/file"
"github.com/anchore/syft/syft/format/internal/spdxutil"
"github.com/anchore/syft/syft/format/internal/testutil"
"github.com/anchore/syft/syft/pkg"
Expand Down

0 comments on commit a56eff9

Please sign in to comment.