Skip to content

Commit

Permalink
fix: wait until all the stdout is displayed (#2317)
Browse files Browse the repository at this point in the history
Signed-off-by: Javier López Barba <javier@okteto.com>
  • Loading branch information
jLopezbarb committed Mar 7, 2022
1 parent d89634e commit e8dedf5
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 39 deletions.
79 changes: 48 additions & 31 deletions cmd/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,17 +380,24 @@ func (dc *DeployCommand) deploy(ctx context.Context, opts *Options) error {
}
exit <- nil
}()
select {
case <-stopCmds:
oktetoLog.Infof("CTRL+C received, starting shutdown sequence")
sp := utils.NewSpinner("Shutting down...")
sp.Start()
defer sp.Stop()
dc.Executor.CleanUp(errors.New("interrupt signal received"))
return oktetoErrors.ErrIntSig
case err := <-exit:
if err != nil {
return err
shouldExit := false
for {
select {
case <-stopCmds:
oktetoLog.Infof("CTRL+C received, starting shutdown sequence")
sp := utils.NewSpinner("Shutting down...")
sp.Start()
defer sp.Stop()
dc.Executor.CleanUp(errors.New("interrupt signal received"))
return oktetoErrors.ErrIntSig
case err := <-exit:
if err != nil {
return err
}
shouldExit = true
}
if shouldExit {
break
}
}

Expand All @@ -407,36 +414,46 @@ func (dc *DeployCommand) deploy(ctx context.Context, opts *Options) error {
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt)
d := displayer.NewDisplayer(oktetoLog.GetOutputFormat(), reader, nil)
d.Display("Deploying compose")
go func() {
err := dc.deployStack(ctx, opts)
writer.Close()
exit <- err
}()
select {
case err := <-exit:
d.CleanUp(err)
case <-stop:
d.CleanUp(errors.New("Interrupt signal received"))
exitCompose <- oktetoErrors.ErrIntSig
d.Display("Deploying compose")
shouldExit := false
for {
select {
case err := <-exit:
d.CleanUp(err)
shouldExit = true
case <-stop:
d.CleanUp(errors.New("Interrupt signal received"))
exitCompose <- oktetoErrors.ErrIntSig
return
}
if shouldExit {
break
}
}
oktetoLog.SetOutput(os.Stdout)
}
exitCompose <- nil
}()

select {
case <-stopCmds:
os.Unsetenv(model.OktetoDisableSpinnerEnvVar)
oktetoLog.Infof("CTRL+C received, starting shutdown sequence")
sp := utils.NewSpinner("Shutting down...")
sp.Start()
defer sp.Stop()
dc.Executor.CleanUp(errors.New("Interrupt signal received"))
return oktetoErrors.ErrIntSig
case err := <-exitCompose:
if err != nil {
return err
shouldExit = false
for {
select {
case <-stopCmds:
os.Unsetenv(model.OktetoDisableSpinnerEnvVar)
oktetoLog.Infof("CTRL+C received, starting shutdown sequence")
return oktetoErrors.ErrIntSig
case err := <-exitCompose:
if err != nil {
return err
}
shouldExit = true
}
if shouldExit {
break
}
}

Expand Down
13 changes: 13 additions & 0 deletions cmd/utils/displayer/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"io"
"os"
"sync"

oktetoLog "github.com/okteto/okteto/pkg/log"
)
Expand Down Expand Up @@ -50,6 +51,15 @@ func newJSONDisplayer(stdout, stderr io.Reader) *jsonDisplayer {

func (d *jsonDisplayer) Display(_ string) {
d.commandContext, d.cancel = context.WithCancel(context.Background())
var wg sync.WaitGroup
wgDelta := 0
if d.stdoutScanner != nil {
wgDelta++
}
if d.stderrScanner != nil {
wgDelta++
}
wg.Add(wgDelta)
if d.stdoutScanner != nil {
go func() {
for d.stdoutScanner.Scan() {
Expand All @@ -62,6 +72,7 @@ func (d *jsonDisplayer) Display(_ string) {
}
break
}
wg.Done()
}()
}

Expand All @@ -77,8 +88,10 @@ func (d *jsonDisplayer) Display(_ string) {
}
break
}
wg.Done()
}()
}
wg.Wait()
}

// CleanUp stops displaying
Expand Down
13 changes: 13 additions & 0 deletions cmd/utils/displayer/plain.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"io"
"os"
"sync"

oktetoLog "github.com/okteto/okteto/pkg/log"
)
Expand Down Expand Up @@ -50,6 +51,15 @@ func newPlainDisplayer(stdout, stderr io.Reader) *plainDisplayer {

func (d *plainDisplayer) Display(_ string) {
d.commandContext, d.cancel = context.WithCancel(context.Background())
var wg sync.WaitGroup
wgDelta := 0
if d.stdoutScanner != nil {
wgDelta++
}
if d.stderrScanner != nil {
wgDelta++
}
wg.Add(wgDelta)
if d.stdoutScanner != nil {
go func() {
for d.stdoutScanner.Scan() {
Expand All @@ -62,6 +72,7 @@ func (d *plainDisplayer) Display(_ string) {
}
break
}
wg.Done()
}()
}

Expand All @@ -77,8 +88,10 @@ func (d *plainDisplayer) Display(_ string) {
}
break
}
wg.Done()
}()
}
wg.Wait()
}

// CleanUp stops displaying
Expand Down
40 changes: 32 additions & 8 deletions cmd/utils/displayer/tty.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os"
"runtime"
"strings"
"sync"
"text/template"
"time"

Expand Down Expand Up @@ -84,17 +85,32 @@ func (d *TTYDisplayer) Display(commandName string) {

d.hideCursor()
d.commandContext, d.cancel = context.WithCancel(context.Background())
go d.displayCommand()
wg := &sync.WaitGroup{}
wgDelta := 0
if d.stdoutScanner != nil {
go d.displayStdout()
wgDelta++
}
if d.stderrScanner != nil {
go d.displayStderr()
wgDelta++
}
wg.Add(wgDelta)

commandChan := make(chan bool, 1)
go d.displayCommand(commandChan)
if d.stdoutScanner != nil {
go d.displayStdout(wg)
}
if d.stderrScanner != nil {
go d.displayStderr(wg)
}
wg.Wait()
d.cancel()
<-commandChan
}

func (d *TTYDisplayer) displayCommand() {
func (d *TTYDisplayer) displayCommand(commandChan chan bool) {
t := time.NewTicker(50 * time.Millisecond)
shouldExit := false
for {
for i := 0; i < len(spinnerChars); i++ {
select {
Expand All @@ -110,14 +126,20 @@ func (d *TTYDisplayer) displayCommand() {
}
d.screenbuf.Flush()
case <-d.commandContext.Done():
return
shouldExit = true
}

if shouldExit {
break
}
}
if shouldExit {
break
}
}
commandChan <- true
}

func (d *TTYDisplayer) displayStdout() {
func (d *TTYDisplayer) displayStdout(wg *sync.WaitGroup) {
for d.stdoutScanner.Scan() {
select {
case <-d.commandContext.Done():
Expand Down Expand Up @@ -148,6 +170,7 @@ func (d *TTYDisplayer) displayStdout() {
if d.stdoutScanner.Err() != nil {
oktetoLog.Infof("Error reading command output: %s", d.stdoutScanner.Err().Error())
}
wg.Done()
}

func checkIfIsBuildingLine(line string) bool {
Expand All @@ -157,7 +180,7 @@ func checkIfIsBuildingLine(line string) bool {
return false
}

func (d *TTYDisplayer) displayStderr() {
func (d *TTYDisplayer) displayStderr(wg *sync.WaitGroup) {
for d.stderrScanner.Scan() {
select {
case <-d.commandContext.Done():
Expand All @@ -178,6 +201,7 @@ func (d *TTYDisplayer) displayStderr() {
if d.stderrScanner.Err() != nil {
oktetoLog.Infof("Error reading command output: %s", d.stderrScanner.Err().Error())
}
wg.Done()
}

func isTopDisplay(line string) bool {
Expand Down

0 comments on commit e8dedf5

Please sign in to comment.