diff options
-rw-r--r-- | api/graphql/graphql_test.go | 5 | ||||
-rw-r--r-- | api/http/git_file_handlers_test.go | 3 | ||||
-rw-r--r-- | cache/multi_repo_cache.go | 37 | ||||
-rw-r--r-- | cache/repo_cache.go | 47 | ||||
-rw-r--r-- | commands/execenv/env.go | 8 | ||||
-rw-r--r-- | commands/webui.go | 5 |
6 files changed, 51 insertions, 54 deletions
diff --git a/api/graphql/graphql_test.go b/api/graphql/graphql_test.go index a8dfad3f..b9ced703 100644 --- a/api/graphql/graphql_test.go +++ b/api/graphql/graphql_test.go @@ -19,8 +19,7 @@ func TestQueries(t *testing.T) { random_bugs.FillRepoWithSeed(repo, 10, 42) mrc := cache.NewMultiRepoCache() - _, events, err := mrc.RegisterDefaultRepository(repo) - require.NoError(t, err) + _, events := mrc.RegisterDefaultRepository(repo) for event := range events { require.NoError(t, event.Err) } @@ -217,6 +216,6 @@ func TestQueries(t *testing.T) { } } - err = c.Post(query, &resp) + err := c.Post(query, &resp) assert.NoError(t, err) } diff --git a/api/http/git_file_handlers_test.go b/api/http/git_file_handlers_test.go index 7be4aff6..8319da32 100644 --- a/api/http/git_file_handlers_test.go +++ b/api/http/git_file_handlers_test.go @@ -22,8 +22,7 @@ func TestGitFileHandlers(t *testing.T) { repo := repository.CreateGoGitTestRepo(t, false) mrc := cache.NewMultiRepoCache() - repoCache, events, err := mrc.RegisterDefaultRepository(repo) - require.NoError(t, err) + repoCache, events := mrc.RegisterDefaultRepository(repo) for event := range events { require.NoError(t, event.Err) } diff --git a/cache/multi_repo_cache.go b/cache/multi_repo_cache.go index 007737ad..da9544cf 100644 --- a/cache/multi_repo_cache.go +++ b/cache/multi_repo_cache.go @@ -21,25 +21,30 @@ func NewMultiRepoCache() *MultiRepoCache { } // RegisterRepository register a named repository. Use this for multi-repo setup -func (c *MultiRepoCache) RegisterRepository(name string, repo repository.ClockedRepo) (*RepoCache, chan BuildEvent, error) { - r, events, err := NewNamedRepoCache(repo, name) - if err != nil { - return nil, nil, err - } +func (c *MultiRepoCache) RegisterRepository(repo repository.ClockedRepo, name string) (*RepoCache, chan BuildEvent) { + r, events := NewNamedRepoCache(repo, name) - c.repos[name] = r - return r, events, nil -} + // intercept events to make sure the cache building process succeed properly + out := make(chan BuildEvent) + go func() { + defer close(out) -// RegisterDefaultRepository register an unnamed repository. Use this for mono-repo setup -func (c *MultiRepoCache) RegisterDefaultRepository(repo repository.ClockedRepo) (*RepoCache, chan BuildEvent, error) { - r, events, err := NewRepoCache(repo) - if err != nil { - return nil, nil, err - } + for event := range events { + out <- event + if event.Err != nil { + return + } + } + + c.repos[name] = r + }() + + return r, out +} - c.repos[defaultRepoName] = r - return r, events, nil +// RegisterDefaultRepository register an unnamed repository. Use this for single-repo setup +func (c *MultiRepoCache) RegisterDefaultRepository(repo repository.ClockedRepo) (*RepoCache, chan BuildEvent) { + return c.RegisterRepository(repo, defaultRepoName) } // DefaultRepo retrieve the default repository diff --git a/cache/repo_cache.go b/cache/repo_cache.go index 6e7396ed..edfe1baf 100644 --- a/cache/repo_cache.go +++ b/cache/repo_cache.go @@ -72,17 +72,17 @@ type RepoCache struct { userIdentityId entity.Id } -// NewRepoCache create or open an unnamed (aka default) cache on top of a raw repository. -// If the returned BuildEvent channel is not nil, the caller is expected to read all events before the cache is considered +// NewRepoCache create or open a cache on top of a raw repository. +// The caller is expected to read all returned events before the cache is considered // ready to use. -func NewRepoCache(r repository.ClockedRepo) (*RepoCache, chan BuildEvent, error) { +func NewRepoCache(r repository.ClockedRepo) (*RepoCache, chan BuildEvent) { return NewNamedRepoCache(r, defaultRepoName) } // NewNamedRepoCache create or open a named cache on top of a raw repository. -// If the returned BuildEvent channel is not nil, the caller is expected to read all events before the cache is considered +// The caller is expected to read all returned events before the cache is considered // ready to use. -func NewNamedRepoCache(r repository.ClockedRepo, name string) (*RepoCache, chan BuildEvent, error) { +func NewNamedRepoCache(r repository.ClockedRepo, name string) (*RepoCache, chan BuildEvent) { c := &RepoCache{ repo: r, name: name, @@ -102,35 +102,36 @@ func NewNamedRepoCache(r repository.ClockedRepo, name string) (*RepoCache, chan } // small buffer so that below functions can emit an event without blocking - events := make(chan BuildEvent, 10) - defer close(events) + events := make(chan BuildEvent) - err := c.lock(events) - if err != nil { - return &RepoCache{}, events, err - } + go func() { + defer close(events) - err = c.load() - if err == nil { - return c, events, nil - } + err := c.lock(events) + if err != nil { + events <- BuildEvent{Err: err} + return + } + + err = c.load() + if err == nil { + return + } - // Cache is either missing, broken or outdated. Rebuilding. - c.buildCache(events) + // Cache is either missing, broken or outdated. Rebuilding. + c.buildCache(events) + }() - return c, events, nil + return c, events } func NewRepoCacheNoEvents(r repository.ClockedRepo) (*RepoCache, error) { - cache, events, err := NewRepoCache(r) - if err != nil { - return nil, err - } + cache, events := NewRepoCache(r) for event := range events { if event.Err != nil { for range events { } - return nil, err + return nil, event.Err } } return cache, nil diff --git a/commands/execenv/env.go b/commands/execenv/env.go index d30a8db4..9b71a45a 100644 --- a/commands/execenv/env.go +++ b/commands/execenv/env.go @@ -129,15 +129,11 @@ func LoadBackend(env *Env) func(*cobra.Command, []string) error { } var events chan cache.BuildEvent - env.Backend, events, err = cache.NewRepoCache(env.Repo) - if err != nil { - return err - } + env.Backend, events = cache.NewRepoCache(env.Repo) for event := range events { if event.Err != nil { - env.Err.Printf("Cache building error [%s]: %v\n", event.Typename, event.Err) - continue + return event.Err } switch event.Event { case cache.BuildEventCacheIsBuilt: diff --git a/commands/webui.go b/commands/webui.go index eca40b91..0b3b24a6 100644 --- a/commands/webui.go +++ b/commands/webui.go @@ -106,11 +106,8 @@ func runWebUI(env *execenv.Env, opts webUIOptions) error { } mrc := cache.NewMultiRepoCache() - _, events, err := mrc.RegisterDefaultRepository(env.Repo) - if err != nil { - return err - } + _, events := mrc.RegisterDefaultRepository(env.Repo) for event := range events { if event.Err != nil { env.Err.Printf("Cache building error [%s]: %v\n", event.Typename, event.Err) |