add BeforeRestart hook

This commit is contained in:
c9s 2021-02-05 13:01:07 +08:00
parent ba9c8693af
commit 0803d6bae0
4 changed files with 24 additions and 15 deletions

View File

@ -78,6 +78,11 @@ func main() {
Context: ctx, Context: ctx,
Cancel: cancel, Cancel: cancel,
Token: "", Token: "",
BeforeRestart: func() {
if err := ui.Close() ; err != nil {
log.WithError(err).Errorf("ui close error")
}
},
} }
userConfig = &bbgo.Config{ userConfig = &bbgo.Config{
Notifications: nil, Notifications: nil,

View File

@ -95,9 +95,8 @@ func runSetup(baseCtx context.Context, userConfig *bbgo.Config, enableApiServer
return nil return nil
} }
func BootstrapEnvironment(ctx context.Context, environ *bbgo.Environment, userConfig *bbgo.Config) error { func BootstrapEnvironment(ctx context.Context, environ *bbgo.Environment, userConfig *bbgo.Config) error {
if dsn, ok := os.LookupEnv("MYSQL_URL") ; ok { if dsn, ok := os.LookupEnv("MYSQL_URL"); ok {
if err := environ.ConfigureDatabase(ctx, dsn); err != nil { if err := environ.ConfigureDatabase(ctx, dsn); err != nil {
return err return err
} }
@ -267,12 +266,12 @@ func runConfig(basectx context.Context, userConfig *bbgo.Config, enableApiServer
defer cancelTrading() defer cancelTrading()
environ := bbgo.NewEnvironment() environ := bbgo.NewEnvironment()
if err := BootstrapEnvironment(ctx, environ, userConfig) ; err != nil { if err := BootstrapEnvironment(ctx, environ, userConfig); err != nil {
return err return err
} }
trader := bbgo.NewTrader(environ) trader := bbgo.NewTrader(environ)
if err := ConfigureTrader(trader, userConfig) ; err != nil { if err := ConfigureTrader(trader, userConfig); err != nil {
return err return err
} }
@ -320,7 +319,7 @@ func run(cmd *cobra.Command, args []string) error {
return err return err
} }
if _, err := os.Stat(dotenvFile) ; err == nil { if _, err := os.Stat(dotenvFile); err == nil {
if err := godotenv.Load(dotenvFile); err != nil { if err := godotenv.Load(dotenvFile); err != nil {
return errors.Wrap(err, "error loading dotenv file") return errors.Wrap(err, "error loading dotenv file")
} }
@ -347,22 +346,21 @@ func run(cmd *cobra.Command, args []string) error {
return err return err
} }
var userConfig *bbgo.Config var userConfig = &bbgo.Config{}
if setup { if setup {
log.Infof("running in setup mode, skip reading config file") log.Infof("running in setup mode, skip reading config file")
enableApiServer = true enableApiServer = true
userConfig = &bbgo.Config{
Notifications: nil,
Persistence: nil,
Sessions: nil,
ExchangeStrategies: nil,
}
} else { } else {
// if it's not setup, then the config file option is required.
if len(configFile) == 0 { if len(configFile) == 0 {
return errors.New("--config option is required") return errors.New("--config option is required")
} }
if _, err := os.Stat(configFile); err != nil {
return err
}
userConfig, err = bbgo.Load(configFile, false) userConfig, err = bbgo.Load(configFile, false)
if err != nil { if err != nil {
return err return err

View File

@ -32,6 +32,8 @@ type Setup struct {
// Token is used for setup api authentication // Token is used for setup api authentication
Token string Token string
BeforeRestart func()
} }
type Server struct { type Server struct {

View File

@ -119,6 +119,10 @@ func (s *Server) setupRestart(c *gin.Context) {
logrus.Info("server shutdown completed") logrus.Info("server shutdown completed")
if s.Setup.BeforeRestart != nil {
s.Setup.BeforeRestart()
}
bin := os.Args[0] bin := os.Args[0]
args := os.Args[0:] args := os.Args[0:]