@@ -14,6 +14,7 @@ import (
1414 "sort"
1515 "strings"
1616 "sync"
17+ "time"
1718
1819 "github.com/facebookgo/errgroup"
1920 "github.com/facebookgo/jsonpipe"
@@ -32,6 +33,7 @@ type deployCmd struct {
3233 Force bool
3334 Verbose bool
3435 Retries int
36+ wait func (int ) time.Duration
3537}
3638
3739func (d * deployCmd ) getSourceFiles (
@@ -463,34 +465,75 @@ func (d *deployCmd) deploy(
463465 }, nil
464466}
465467
468+ func (d * deployCmd ) handleError (
469+ n int ,
470+ err , prevErr error ,
471+ e * env ,
472+ ) error {
473+ if err == nil {
474+ return nil
475+ }
476+ if n == d .Retries - 1 {
477+ return err
478+ }
479+
480+ var waitTime time.Duration
481+ if d .wait != nil {
482+ waitTime = d .wait (n )
483+ }
484+
485+ errStr := errorString (e , err )
486+ if prevErr != nil {
487+ prevErrStr := errorString (e , prevErr )
488+ if prevErrStr == errStr {
489+ fmt .Fprintf (
490+ e .Err ,
491+ "Sorry, deploy failed again with same error.\n Will retry in %d seconds.\n \n " ,
492+ waitTime / time .Second ,
493+ )
494+ time .Sleep (waitTime )
495+ return nil
496+ }
497+ }
498+
499+ fmt .Fprintf (
500+ e .Err ,
501+ "Deploy failed with error:\n %s\n Will retry in %d seconds.\n \n " ,
502+ errStr ,
503+ waitTime / time .Second ,
504+ )
505+ time .Sleep (waitTime )
506+ return nil
507+ }
508+
466509func (d * deployCmd ) run (e * env , c * client ) error {
467- first := true
510+ var prevErr error
468511 for i := 0 ; i < d .Retries ; i ++ {
469- if ! first {
470- fmt .Fprintf (e .Err , "Deploy failed. Retrying deploy...\n \n " )
471- }
472- config := c .Config
473- parseVersion := config .getProjectConfig ().Parse .JSSDK
512+ parseVersion := c .Config .getProjectConfig ().Parse .JSSDK
474513 newDeployInfo , err := d .deploy (parseVersion , nil , false , e )
475514 if err == nil {
476515 if parseVersion == "" && newDeployInfo != nil && newDeployInfo .ParseVersion != "" {
477- config .getProjectConfig ().Parse .JSSDK = newDeployInfo .ParseVersion
478- return storeProjectConfig (e , config )
516+ c . Config .getProjectConfig ().Parse .JSSDK = newDeployInfo .ParseVersion
517+ return storeProjectConfig (e , c . Config )
479518 }
480519 return nil
481520 }
482-
483- first = false
484- if i == d .Retries - 1 {
521+ if err := d .handleError (i , err , prevErr , e ); err != nil {
485522 return err
486523 }
524+ prevErr = err
487525 }
488526
489527 return nil
490528}
491529
492530func newDeployCmd (e * env ) * cobra.Command {
493- d := deployCmd {Verbose : true , Retries : 5 }
531+ d := deployCmd {
532+ Verbose : true ,
533+ Retries : 3 ,
534+ wait : func (n int ) time.Duration { return time .Duration (n ) * time .Second },
535+ }
536+
494537 cmd := & cobra.Command {
495538 Use : "deploy [app]" ,
496539 Short : "Deploys a Parse App" ,
0 commit comments