Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: wait in parallel #431

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 26 additions & 13 deletions pkg/promote/promote.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path/filepath"
"strconv"
"strings"
"sync"
"time"

"github.com/jenkins-x/jx-helpers/v3/pkg/requirements"
Expand Down Expand Up @@ -575,6 +576,8 @@ func (o *Options) PromoteAll(pred func(*jxcore.EnvironmentConfig) bool) error {
}
groups = append(groups, []*jxcore.EnvironmentConfig{env})
}
var errorChannel = make(chan error, len(groups))
var wg sync.WaitGroup
for _, group := range groups {
firstEnv := group[0]

Expand All @@ -585,14 +588,24 @@ func (o *Options) PromoteAll(pred func(*jxcore.EnvironmentConfig) bool) error {
return err
}
o.ReleaseInfo = releaseInfo
if !o.NoPoll {
err = o.WaitForPromotion(firstEnv, releaseInfo)
if err != nil {
return err
}
// TODO: Add test
if !o.NoPoll && group[0].PromotionStrategy == v1.PromotionStrategyTypeAutomatic {
wg.Add(1)
// Wait for multiple PRs in parallel
go o.WaitForPromotion(firstEnv, releaseInfo, errorChannel, &wg)
}
}
return nil
wg.Wait()
close(errorChannel)
var err error
for err2 := range errorChannel {
if err == nil {
err = err2
} else {
err = fmt.Errorf("%w; %v", err, err2)
}
}
return err
}

// EnvironmentNamespace returns the namespace for the environment
Expand Down Expand Up @@ -833,14 +846,15 @@ func (o *Options) GetTargetNamespace(ns, env string) (string, *jxcore.Environmen
return targetNS, envResource, nil
}

func (o *Options) WaitForPromotion(env *jxcore.EnvironmentConfig, releaseInfo *ReleaseInfo) error {
func (o *Options) WaitForPromotion(env *jxcore.EnvironmentConfig, releaseInfo *ReleaseInfo, errorChannel chan<- error, wg *sync.WaitGroup) {
defer wg.Done()
if o.TimeoutDuration == nil {
log.Logger().Infof("No --%s option specified on the 'jx promote' command so not waiting for the promotion to succeed", optionTimeout)
return nil
return
}
if o.PullRequestPollDuration == nil {
log.Logger().Infof("No --%s option specified on the 'jx promote' command so not waiting for the promotion to succeed", optionPullRequestPollTime)
return nil
return
}
duration := *o.TimeoutDuration
end := time.Now().Add(duration)
Expand All @@ -856,12 +870,11 @@ func (o *Options) WaitForPromotion(env *jxcore.EnvironmentConfig, releaseInfo *R
// TODO based on if the PR completed or not fail the PR or the Promote?
err2 := promoteKey.OnPromotePullRequest(kubeClient, jxClient, o.Namespace, activities.FailedPromotionPullRequest)
if err2 != nil {
return err2
errorChannel <- err2
}
return err
errorChannel <- err
}
}
return nil
}

// TODO This could do with a refactor and some tests...
Expand Down Expand Up @@ -985,7 +998,7 @@ func (o *Options) waitForGitOpsPullRequest(env *jxcore.EnvironmentConfig, releas
}
}
}
if !pr.Mergeable {
if pr.MergeableState == scm.MergeableStateConflicting {
log.Logger().Info("Rebasing PullRequest due to conflict")

err = o.PromoteViaPullRequest([]*jxcore.EnvironmentConfig{env}, releaseInfo, false)
Expand Down
Loading