Commit 796d41e1 authored by Andy Cobaugh's avatar Andy Cobaugh

wait: make retry/delay/timeout configurable via flags, clean up logging

parent 35f70a53
......@@ -14,6 +14,7 @@ import (
"github.com/apex/log/handlers/cli"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// buildCmd represents the build command
......@@ -21,6 +22,33 @@ var ciWaitCmd = &cobra.Command{
Use: "wait",
Short: "Wait for a deployment to be deployed by watching the live version endpoint",
RunE: runCiWaitCmd,
PreRunE: func(cmd *cobra.Command, args []string) error {
err := viper.BindPFlag("url-max-tries", cmd.PersistentFlags().Lookup("url-max-tries"))
if err != nil {
return err
}
err = viper.BindPFlag("url-delay", cmd.PersistentFlags().Lookup("url-delay"))
if err != nil {
return err
}
err = viper.BindPFlag("update-max-tries", cmd.PersistentFlags().Lookup("update-max-tries"))
if err != nil {
return err
}
err = viper.BindPFlag("update-delay", cmd.PersistentFlags().Lookup("update-delay"))
if err != nil {
return err
}
err = viper.BindPFlag("timeout", cmd.PersistentFlags().Lookup("timeout"))
if err != nil {
return err
}
return nil
},
}
var ciWaitCommitFields = []string{"scm-commit-id", "Commit", "commit"}
......@@ -37,15 +65,12 @@ type ciWaitBaseURLData struct {
Env string
}
const ciWaitURLMaxTries = 10
const ciWaitURLDelay = 10 * time.Second
const ciWaitUpdateMaxTries = 30
const ciWaitUpdateDelay = 10 * time.Second
const ciWaitTimeout = 10 * time.Second
func init() {
ciWaitCmd.PersistentFlags().Int("url-max-tries", 10, "Max tries to locate the version endpoint")
ciWaitCmd.PersistentFlags().Duration("url-delay", 10*time.Second, "Time to wait between attempts to locate version endpoint")
ciWaitCmd.PersistentFlags().Int("update-max-tries", 30, "Max tries to check version endpoint for the update")
ciWaitCmd.PersistentFlags().Duration("update-delay", 10*time.Second, "Time to wait between checking version endpoint for update")
ciWaitCmd.PersistentFlags().Duration("timeout", 2*time.Second, "Time to wait for every HTTP request")
ciCmd.AddCommand(ciWaitCmd)
}
......@@ -60,7 +85,7 @@ func runCiWaitCmd(cmd *cobra.Command, args []string) error {
var url string
var initialBody []byte
var newBody []byte
hc := &http.Client{Timeout: ciWaitTimeout}
hc := &http.Client{Timeout: viper.GetDuration("timeout")}
log.WithFields(log.Fields{
"commit": newCommit,
......@@ -70,13 +95,13 @@ func runCiWaitCmd(cmd *cobra.Command, args []string) error {
}).Info("Waiting for new version to be available")
log.WithFields(log.Fields{
"maxtries": ciWaitURLMaxTries,
"delay": ciWaitURLDelay.String(),
"maxtries": viper.GetInt("url-max-tries"),
"delay": viper.GetDuration("url-delay").String(),
}).Info("Looking for version endpoint")
// loop over possible urls
cli.Default.Padding = 2 * InitialPadding
for i := 1; i <= ciWaitURLMaxTries; i++ {
for i := 1; i <= viper.GetInt("url-max-tries"); i++ {
for _, u := range ciWaitURLs {
tmpl, err := template.New("URL").Parse(u)
if err != nil {
......@@ -98,37 +123,43 @@ func runCiWaitCmd(cmd *cobra.Command, args []string) error {
}
defer resp.Body.Close()
log.WithFields(log.Fields{"code": resp.StatusCode}).Info("Received response")
if resp.StatusCode == http.StatusOK {
log.WithFields(log.Fields{"code": resp.StatusCode}).Info("Found version endpoint")
url = urlBuf.String()
initialBody, err = ioutil.ReadAll(resp.Body)
if err != nil {
log.WithError(err).Error("Could not read body")
}
break
} else {
log.WithFields(log.Fields{"code": resp.StatusCode}).Warn("No version endpoint here")
}
}
if url == "" {
log.Infof("Sleeping for %s ...", ciWaitURLDelay.String())
time.Sleep(ciWaitURLDelay)
log.Warnf("Sleeping for %s ...", viper.GetDuration("url-delay").String())
time.Sleep(viper.GetDuration("url-delay"))
} else {
break
}
}
cli.Default.Padding = InitialPadding
if url == "" {
log.Fatal("Could not locate version endpoint")
}
log.WithFields(log.Fields{
"project": data.Project,
"env": data.Env,
"url": url,
"maxtries": ciWaitUpdateMaxTries,
"delay": ciWaitUpdateDelay.String(),
"maxtries": viper.GetInt("update-max-tries"),
"delay": viper.GetDuration("update-delay").String(),
}).Infof("Waiting for version update")
cli.Default.Padding = 2 * InitialPadding
// wait for version to update
for i := 1; i <= ciWaitUpdateMaxTries; i++ {
for i := 1; i <= viper.GetInt("update-max-tries"); i++ {
log.WithField("try", i).Infof("Checking URL: %s", url)
// check version url and compare
resp, err := hc.Get(url)
......@@ -138,8 +169,8 @@ func runCiWaitCmd(cmd *cobra.Command, args []string) error {
}
defer resp.Body.Close()
log.WithFields(log.Fields{"code": resp.StatusCode}).Info("Received response")
if resp.StatusCode == http.StatusOK {
log.WithFields(log.Fields{"code": resp.StatusCode}).Info("Received response")
newBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.WithError(err).Error("Could not read body")
......@@ -149,12 +180,13 @@ func runCiWaitCmd(cmd *cobra.Command, args []string) error {
log.Info("New version is live")
return nil
} else {
log.Info("No update to version endpoint")
log.Warn("No update to version endpoint")
}
}
log.WithFields(log.Fields{"code": resp.StatusCode}).Warn("No OK response")
log.Infof("Sleeping for %s ...", ciWaitUpdateDelay.String())
time.Sleep(ciWaitUpdateDelay)
log.Warnf("Sleeping for %s ...", viper.GetDuration("update-delay").String())
time.Sleep(viper.GetDuration("update-delay"))
}
cli.Default.Padding = InitialPadding
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment