Skip to content

Commit

Permalink
refactor(module/git): tighten cache usage logic
Browse files Browse the repository at this point in the history
Signed-off-by: hainenber <[email protected]>
  • Loading branch information
hainenber committed Nov 11, 2023
1 parent 55cd198 commit 6cd7029
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions component/module/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,16 @@ func New(o component.Options, args Arguments) (*Component, error) {
argsChanged: make(chan struct{}, 1),
}

// Only acknowledge the error from Update if it's not a
// vcs.UpdateFailedError; vcs.UpdateFailedError means that the Git repo
// exists but we were just unable to update it.
if err := c.Update(args); err != nil {
return nil, err
if errors.As(err, &vcs.UpdateFailedError{}) {
level.Error(c.log).Log("msg", "failed to update repository", "err", err)
c.updateHealth(err)
} else {
return nil, err
}
}
return c, nil
}
Expand Down Expand Up @@ -195,11 +203,10 @@ func (c *Component) Update(args component.Arguments) (err error) {

// Create or update the repo field.
// Failure to update repository makes the module loader temporarily use cached contents on disk
var updateVcsErr vcs.UpdateFailedError
if c.repo == nil || !reflect.DeepEqual(repoOpts, c.repoOpts) {
r, err := vcs.NewGitRepo(context.Background(), repoPath, repoOpts)
if err != nil {
if errors.As(err, &updateVcsErr) {
if errors.As(err, &vcs.UpdateFailedError{}) {
level.Error(c.log).Log("msg", "failed to update repository", "err", err)
c.updateHealth(err)
} else {
Expand Down Expand Up @@ -228,14 +235,8 @@ func (c *Component) Update(args component.Arguments) (err error) {
// controller. pollFile must only be called with c.mut held.
func (c *Component) pollFile(ctx context.Context, args Arguments) error {
// Make sure our repo is up-to-date.
var updateVcsErr *vcs.UpdateFailedError
if err := c.repo.Update(ctx); err != nil {
if errors.As(err, &updateVcsErr) {
level.Error(c.log).Log("msg", "failed to update repository", "err", err)
c.updateHealth(err)
} else {
return err
}
return err
}

// Finally, configure our controller.
Expand Down

0 comments on commit 6cd7029

Please sign in to comment.