github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+incompatible/command/v6/shared/poll_start.go (about)

     1  package shared
     2  
     3  import (
     4  	"context"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	"code.cloudfoundry.org/cli/actor/sharedaction"
     8  	"code.cloudfoundry.org/cli/actor/v2action"
     9  	"code.cloudfoundry.org/cli/command"
    10  	"code.cloudfoundry.org/cli/command/translatableerror"
    11  )
    12  
    13  func PollStart(ui command.UI, config command.Config, messages <-chan sharedaction.LogMessage, logErrs <-chan error, appState <-chan v2action.ApplicationStateChange, apiWarnings <-chan string, apiErrs <-chan error, stopStreaming context.CancelFunc) (apiError error) {
    14  
    15  	handleMessage := func(message sharedaction.LogMessage) {
    16  		if message.Staging() {
    17  			ui.DisplayLogMessage(message, false)
    18  		}
    19  	}
    20  	handleLogErr := func(logErr error) {
    21  		switch logErr.(type) {
    22  		case actionerror.NOAATimeoutError:
    23  			ui.DisplayWarning("timeout connecting to log server, no log will be shown")
    24  		default:
    25  			ui.DisplayWarning(logErr.Error())
    26  		}
    27  	}
    28  	for appState != nil || apiWarnings != nil || apiErrs != nil {
    29  		select {
    30  		case message, ok := <-messages:
    31  			if !ok {
    32  				messages = nil
    33  				continue
    34  			}
    35  			handleMessage(message)
    36  
    37  		case state, ok := <-appState:
    38  			if !ok {
    39  				appState = nil
    40  				continue
    41  			}
    42  
    43  			switch state {
    44  			case v2action.ApplicationStateStopping:
    45  				ui.DisplayNewline()
    46  				ui.DisplayText("Stopping app...")
    47  
    48  			case v2action.ApplicationStateStaging:
    49  				ui.DisplayNewline()
    50  				ui.DisplayText("Staging app and tracing logs...")
    51  
    52  			case v2action.ApplicationStateStarting:
    53  				defer func() {
    54  					ui.DisplayNewline()
    55  					ui.DisplayText("Waiting for app to start...")
    56  				}()
    57  			}
    58  
    59  		case warning, ok := <-apiWarnings:
    60  			if !ok {
    61  				apiWarnings = nil
    62  				continue
    63  			}
    64  
    65  			ui.DisplayWarning(warning)
    66  
    67  		case logErr, ok := <-logErrs:
    68  			if !ok {
    69  				logErrs = nil
    70  				continue
    71  			}
    72  			handleLogErr(logErr)
    73  
    74  		case e, ok := <-apiErrs:
    75  			if !ok {
    76  				apiErrs = nil
    77  				continue
    78  			}
    79  			switch err := e.(type) {
    80  			case actionerror.StagingFailedError:
    81  				apiError = translatableerror.StagingFailedError{Message: err.Error()}
    82  			case actionerror.StagingFailedNoAppDetectedError:
    83  				apiError = translatableerror.StagingFailedNoAppDetectedError{BinaryName: config.BinaryName(), Message: err.Error()}
    84  			case actionerror.StagingTimeoutError:
    85  				apiError = translatableerror.StagingTimeoutError{AppName: err.AppName, Timeout: err.Timeout}
    86  			case actionerror.ApplicationInstanceCrashedError:
    87  				apiError = translatableerror.ApplicationUnableToStartError{AppName: err.Name, BinaryName: config.BinaryName()}
    88  			case actionerror.ApplicationInstanceFlappingError:
    89  				apiError = translatableerror.ApplicationUnableToStartError{AppName: err.Name, BinaryName: config.BinaryName()}
    90  			case actionerror.StartupTimeoutError:
    91  				apiError = translatableerror.StartupTimeoutError{AppName: err.Name, BinaryName: config.BinaryName()}
    92  			default:
    93  				apiError = err
    94  			}
    95  			// if an api error occurred, exit immediately
    96  			stopStreaming()
    97  			return apiError
    98  		}
    99  	}
   100  	stopStreaming()
   101  
   102  	// Consume any pending streamed messages
   103  	for messages != nil || logErrs != nil {
   104  		select {
   105  		case message, ok := <-messages:
   106  			if !ok {
   107  				messages = nil
   108  				continue
   109  			}
   110  			handleMessage(message)
   111  
   112  		case logErr, ok := <-logErrs:
   113  			if !ok {
   114  				logErrs = nil
   115  				continue
   116  			}
   117  			handleLogErr(logErr)
   118  		}
   119  	}
   120  	return nil
   121  }