mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-26 00:43:09 +03:00
b75ab7233e
#### Issue: https://github.com/hasura/graphql-engine-mono/issues/2179 ### Problem: 1. There are few warnings when tests are executed with cli if it is built with `race` flag It is because of a race condition between stdin in migrate_delete for the prompt and stdout for other commands. 2. The integration test of the console used to behave as follows: ``` - Initially there won't be any wg.adds in wait-group so the second go-routine created in console-test (integration-test) - It will send the interrupt signal to the channel before the server has been started - So practically the server won't start ``` 3. The read and write for consoleopts.WG has been happening in different go-routines without control or lock system(reading and writing into same memory location without lock system). So there is a chance of data-race(warning for data-race while integration tests are executed) 4. Data-race errors from `promptui` package ### Solution: 1. Use `--force` flag to avoid getting the input from the prompt in `migrate_delete_test.go` 2. Introduced two fields in server and console opts to let know other go-routines whether the server has been started or not 3. To avoid data-race above which are shared b/w go-routines to know the status of servers has been operated atomically. 4. using new package https://github.com/AlecAivazis/survey https://github.com/hasura/graphql-engine-mono/pull/2231 GitOrigin-RevId: 387eb1be74f24dda34bb3588314b5a909adac227
108 lines
2.7 KiB
Go
108 lines
2.7 KiB
Go
package console
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"sync"
|
|
|
|
"github.com/hasura/graphql-engine/cli/v2"
|
|
|
|
"github.com/fatih/color"
|
|
"github.com/pkg/errors"
|
|
"github.com/skratchdot/open-golang/open"
|
|
)
|
|
|
|
type ServeOpts struct {
|
|
APIServer *APIServer
|
|
ConsoleServer *ConsoleServer
|
|
EC *cli.ExecutionContext
|
|
DontOpenBrowser bool
|
|
Browser string
|
|
ConsolePort string
|
|
APIPort string
|
|
Address string
|
|
|
|
SignalChanAPIServer chan os.Signal
|
|
SignalChanConsoleServer chan os.Signal
|
|
WG sync.WaitGroup
|
|
}
|
|
|
|
// Server console and API Server
|
|
func Serve(opts *ServeOpts) error {
|
|
// get HTTP servers
|
|
apiHTTPServer := opts.APIServer.GetHTTPServer()
|
|
consoleHTTPServer, err := opts.ConsoleServer.GetHTTPServer()
|
|
if err != nil {
|
|
return errors.Wrap(err, "cannot create console server")
|
|
}
|
|
|
|
go func() {
|
|
<-opts.SignalChanAPIServer
|
|
if err := apiHTTPServer.Close(); err != nil {
|
|
opts.EC.Logger.Debugf("unable to close server running on port %s", opts.APIPort)
|
|
}
|
|
}()
|
|
|
|
go func() {
|
|
<-opts.SignalChanConsoleServer
|
|
if err := consoleHTTPServer.Close(); err != nil {
|
|
opts.EC.Logger.Debugf("unable to close server running on port %s", opts.ConsolePort)
|
|
}
|
|
}()
|
|
|
|
// Create WaitGroup for running 2 servers
|
|
wg := opts.WG
|
|
wg.Add(1)
|
|
go func() {
|
|
if err := apiHTTPServer.ListenAndServe(); err != nil {
|
|
if err == http.ErrServerClosed {
|
|
opts.EC.Logger.Infof("server closed on port %s under signal", opts.APIPort)
|
|
} else {
|
|
opts.EC.Logger.WithError(err).Errorf("error listening on port %s", opts.APIPort)
|
|
}
|
|
}
|
|
wg.Done()
|
|
}()
|
|
wg.Add(1)
|
|
go func() {
|
|
if err := consoleHTTPServer.ListenAndServe(); err != nil {
|
|
if err == http.ErrServerClosed {
|
|
opts.EC.Logger.Infof("server closed on port %s under signal", opts.ConsolePort)
|
|
} else {
|
|
opts.EC.Logger.WithError(err).Errorf("error listening on port %s", opts.ConsolePort)
|
|
}
|
|
}
|
|
wg.Done()
|
|
}()
|
|
|
|
consoleURL := fmt.Sprintf("http://%s:%s/", opts.Address, opts.ConsolePort)
|
|
|
|
if !opts.DontOpenBrowser {
|
|
if opts.Browser != "" {
|
|
opts.EC.Spin(color.CyanString("Opening console on: %s", opts.Browser))
|
|
defer opts.EC.Spinner.Stop()
|
|
err = open.RunWith(consoleURL, opts.Browser)
|
|
if err != nil {
|
|
opts.EC.Logger.WithError(err).Warnf("failed opening console in '%s', try to open the url manually", opts.Browser)
|
|
}
|
|
} else {
|
|
opts.EC.Spin(color.CyanString("Opening console using default browser..."))
|
|
defer opts.EC.Spinner.Stop()
|
|
|
|
err = open.Run(consoleURL)
|
|
if err != nil {
|
|
opts.EC.Logger.WithError(err).Warn("Error opening browser, try to open the url manually?")
|
|
}
|
|
}
|
|
}
|
|
|
|
opts.EC.Spinner.Stop()
|
|
opts.EC.Logger.Infof("console running at: %s", consoleURL)
|
|
|
|
opts.EC.Telemetry.Beam()
|
|
|
|
wg.Wait()
|
|
return nil
|
|
}
|